Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a key with a certain prefix exists in Amazon S3 bucket

Tags:

amazon-s3

How would I check if there's a key that starts with a particular prefix, similar to "folders"?

like image 860
Ian Warburton Avatar asked Mar 04 '12 00:03

Ian Warburton


3 Answers

Required: aws-java-sdk jar

credentials = new BasicAWSCredentials(accessKey, secretKey);
config = new ClientConfiguration();
client = new AmazonS3Client(credentials, config );
client.doesBucketExist(bucketName+"/prefix");
like image 156
upog Avatar answered Oct 13 '22 20:10

upog


The docs say it is possible to specify a prefix parameter when asking for a list of keys in a bucket. You can set the max-keys parameter to 1 for speed. If the list is non-empty, you know the prefix exists.

Tools like boto's bucket.list() function expose prefixing and paging as well.

like image 24
vsekhar Avatar answered Oct 13 '22 19:10

vsekhar


To iterate over all S3 files in your bucket that start with 'some/prefix/' in ruby, do the following using the aws-sdk gem:

AWS.config :access_key_id => "foo", :secret_access_key => "bar"
s3 = AWS::S3.new
s3.buckets['com.mydomain.mybucket'].objects.with_prefix('some/prefix/').each do |object|
    # Do something with object (an S3 object)
end
like image 30
Troy Avatar answered Oct 13 '22 19:10

Troy