How would I check if there's a key that starts with a particular prefix, similar to "folders"?
Required: aws-java-sdk
jar
credentials = new BasicAWSCredentials(accessKey, secretKey);
config = new ClientConfiguration();
client = new AmazonS3Client(credentials, config );
client.doesBucketExist(bucketName+"/prefix");
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With