列出我的 AWS S3 桶的键下的所有项目

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

在 AWS S3 中,我有一个名为 my-bucket 的存储桶,带有 AWS Ruby SDK,我可以通过 ruby 代码列出 my-bucket 下的所有项目:

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

这很好。但是在 my-bucket 下,我在 S3 中有以下文件结构:

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

我的问题是:

Q1. 使用AWS Ruby SDK,如何列出my-bucket/customers/products/下的所有项目?

Q2. 我如何检查例如my-bucket/customers/products/data3.txt 存在吗?

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 e.g. my-bucket/customers/products/data3.txt exists?

使用 S3 对象 #exists? 方法,例如:

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