Quick way to list all files in Amazon S3 bucket?

前端 未结 28 1821
星月不相逢
星月不相逢 2020-11-28 01:32

I have an amazon s3 bucket that has tens of thousands of filenames in it. What\'s the easiest way to get a text file that lists all the filenames in the bucket?

28条回答
  •  一向
    一向 (楼主)
    2020-11-28 01:40

    Be carefull, amazon list only returns 1000 files. If you want to iterate over all files you have to paginate the results using markers :

    In ruby using aws-s3

    bucket_name = 'yourBucket'
    marker = ""
    
    AWS::S3::Base.establish_connection!(
      :access_key_id => 'your_access_key_id',
      :secret_access_key => 'your_secret_access_key'
    )
    
    loop do
      objects = Bucket.objects(bucket_name, :marker=>marker, :max_keys=>1000)
      break if objects.size == 0
      marker = objects.last.key
    
      objects.each do |obj|
          puts "#{obj.key}"
      end
    end
    

    end

    Hope this helps, vincent

提交回复
热议问题