Aws-sdk-ruby: How to list objects inside one directory level inside a bucket

Created on 12 Oct 2017  路  3Comments  路  Source: aws/aws-sdk-ruby

Issue description

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:

  • file1
  • file2
  • folder1/

    • file3

    • file4

requesting: Aws::S3::Bucket.new().objects should return only { file1, file2, folder1 }
instead I am getting all of the files inside the bucket.

Gem name ('aws-sdk', 'aws-sdk-resources' or service gems like 'aws-sdk-s3') and its version

gem 'aws-sdk', '~> 2.7.1'

Version of Ruby, OS environment

ruby-2.3.5, OSX Sierra and linux

Code snippets / steps to reproduce

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.

closing-soon service-api

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_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/">],
 ...

All 3 comments

@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.

Was this page helpful?
0 / 5 - 0 ratings