I am trying to get the list of files/folders inside a bucket, using the #objects method I get all the objects inside the bucket. But I am expecting to get only the content in the current level.
example:
Bucket:
requesting: Aws::S3::Bucket.new().objects should return only { file1, file2, folder1 }
instead I am getting all of the files inside the bucket.
gem 'aws-sdk', '~> 2.7.1'
ruby-2.3.5, OSX Sierra and linux
1/ Get a bucket with multiple nested folders that have at least one file inside each one of them.
2/ Call Aws::S3::Bucket.new().objects on that bucket resource
3/ Iterate on the result using map or each and you will get a list of all the content of the bucket, not only the ones in the current directory or root level.
Final thoughts: Maybe this is the design or the purpose the method #objects was made for. In that case, please let me know of a way to get the result I'm looking for, and Thank you.
@thegreyfellow Sorry for the late reply. What you have observed is the current behavior for current resource design. Instead, you can call #list_objects_v2 API directly. As for top level objects, you can try:
s3.list_objects_v2(bucket: "bucket", delimiter: '/')
# sample response
=>#<struct Aws::S3::Types::ListObjectsV2Output
is_truncated=false,
contents=
[#<struct Aws::S3::Types::Object ...>,
#<struct Aws::S3::Types::Object ...>,
#<struct Aws::S3::Types::Object ...>],
...
common_prefixes=
[#<struct Aws::S3::Types::CommonPrefix prefix="folder1/">,
#<struct Aws::S3::Types::CommonPrefix prefix="folder2/">,
#<struct Aws::S3::Types::CommonPrefix prefix="folder3/">],
...
Thanks for the reply, I don't know how I missed this method.
Closing : ) feel free to reopen with further comments.
Most helpful comment
@thegreyfellow Sorry for the late reply. What you have observed is the current behavior for current resource design. Instead, you can call
#list_objects_v2API directly. As for top level objects, you can try: