Leem.fin
Leem.fin

Reputation: 42582

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

Upvotes: 2

Views: 2378

Answers (1)

Mark B
Mark B

Reputation: 200446

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?

Use the S3 Object #exists? method like:

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

Upvotes: 5

Related Questions