Search code examples
javascalaamazon-s3jets3taws-java-sdk

Way in Java to check if S3 bucket has objects with keys matching a wildcard patterrn


What is a good way from a Scala or Java program to check if an S3 bucket has objects matching a certain key pattern? That is, if i have a bucket named "CsvBucket" how can i check if it contains an object where the key matches the pattern "processed/files/2015/8/*.csv" ?

Thanks


Solution

  • Since S3 object keys are just Strings you can just iterate over them and test each using a regular expression. Perhaps something like this (using jets3t library):

    Pattern pattern = Pattern.compile(".*\\.csv");
    // 'service' is an instance of S3Service
    S3Bucket bucket = service.getBucket(bucketName);
    S3Object[] files = service.listObjects(bucket, "processed/files/2015/8", null);
    for (int i = 0; i < files.length; i++)
    {
        if (pattern.matches(files[i].getKey()))
        {
            // ... work with the file ...
        }
    }