Listing directories at a given level in Amazon S3

前端 未结 2 1567
Happy的楠姐
Happy的楠姐 2021-01-03 05:25

I am storing two million files in an amazon S3 bucket. There is a given root (l1) below, a list of directories under l1 and then each directory contains files. So my bucket

2条回答
  •  半阙折子戏
    2021-01-03 06:03

    right_aws allows to do this as part of their underlying S3Interface class, but you can create your own method for an easier (and nicer) use. Put this at the top of your code:

    module RightAws
      class S3
        class Bucket
          def common_prefixes(prefix, delimiter = '/')
            common_prefixes = []
            @s3.interface.incrementally_list_bucket(@name, { 'prefix' => prefix, 'delimiter' => delimiter }) do |thislist|          
              common_prefixes += thislist[:common_prefixes]
            end
            common_prefixes
          end
        end
      end
    end
    

    This adds the common_prefixes method to the RightAws::S3::Bucket class. Now, instead of calling mybucket.keys to fetch the list of keys in your bucket, you can use mybucket.common_prefixes to get an array of common prefixes. In your case:

    mybucket.common_prefixes("l1/")
    # => ["l1/a1", "l1/a2", ... "l1/a5000"]
    

    I must say I tested it only with a small number of common prefixes; you should check that this works with more than 1000 common prefixes.

提交回复
热议问题