简体   繁体   中英

list all items under a key of my bucket of AWS S3

In AWS S3, I have a bucket named my-bucket , with AWS Ruby SDK , I can list all the items under my-bucket by ruby code:

require 'aws-sdk'

s3 = Aws::S3::Resource.new(region: 'us-west-2')

bucket = s3.bucket('my-bucket')

# Show only the first 50 items
bucket.objects.limit(50).each do |item|
  puts "Name:  #{item.key}"
  puts "URL:   #{item.presigned_url(:get)}"
end

This is fine. But under my-bucket I have the following file structure in S3:

my-bucket/
    customers/
         products/
              - data1.txt
              - data2.txt
              ...

My questions are:

Q1. With AWS Ruby SDK , how can I list all the items under my-bucket/customers/products/ ?

Q2. How can I check eg my-bucket/customers/products/data3.txt exists?

Q1. With AWS Ruby SDK, how can I list all the items under my-bucket/customers/products/?

bucket.objects({prefix: "customers/products/"})

Q2. How can I check eg my-bucket/customers/products/data3.txt exists?

Use the S3 Object #exists? method like:

bucket.objects["customers/products/data3.txt"].exists?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM