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

Lee*_*fin 2 ruby amazon-s3 amazon-web-services aws-sdk

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

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
Run Code Online (Sandbox Code Playgroud)

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

my-bucket/
    customers/
         products/
              - data1.txt
              - data2.txt
              ...
Run Code Online (Sandbox Code Playgroud)

我的问题是:

一季度。使用AWS Ruby SDK,如何列出 下的所有项目my-bucket/customers/products/

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

Mar*_*k B 5

一季度。使用 AWS Ruby SDK,如何列出 my-bucket/customers/products/ 下的所有项目?

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

Q2。如何检查例如 my-bucket/customers/products/data3.txt 是否存在?

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

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