How would I check if there's a key that starts with a particular prefix, similar to "folders"?
Asked
Active
Viewed 1.0k times
12
-
Also see [Check if folder exist in s3 bucket](https://stackoverflow.com/q/38285326/5989200) – nekketsuuu Feb 07 '20 at 02:09
3 Answers
5
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.

vsekhar
- 5,090
- 5
- 22
- 23
2
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

Troy
- 5,319
- 1
- 35
- 41
1
Required: aws-java-sdk
jar
credentials = new BasicAWSCredentials(accessKey, secretKey);
config = new ClientConfiguration();
client = new AmazonS3Client(credentials, config );
client.doesBucketExist(bucketName+"/prefix");

upog
- 4,965
- 8
- 42
- 81