当前位置: 首页 > 工具软件 > aws-sdk-ruby > 使用案例 >

列出AWS S3桶密钥下的所有项目

南门鸿畴
2023-12-01
 

在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存在?

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

List all the files in an AWS S3 bucket

 类似资料: