soltex
soltex

Reputation: 3551

Aws S3 connection with specific path

I am trying to list and download folders from a bucket on path eg:"aaa/bbb/" using the aws-sdk gem v2. However I can't figure out how to do it.

This is what I tried:

require 'aws-sdk'

Aws.config.update({
  region: 'us-west-2',
  credentials: Aws::Credentials.new('akid', 'secret')
})        

s3 = Aws::S3::Resource.new

# reference an existing bucket by name
bucket = s3.bucket('aaa')

bucket.objects(prefix: '/bbb/').each do |folder|
  p folder
end

It says: Access Denied (Aws::S3::Errors::AccessDenied)

But, if I use the command line AWS CLI instead, and execute:

aws s3 ls aaa/bbb/

it works...

Any suggestion?

Many thanks.

Upvotes: 1

Views: 748

Answers (2)

Michael - sqlbot
Michael - sqlbot

Reputation: 179194

The convention in S3 is that the "root" of a bucket's keyspace is a zero-length empty string... it is not / as some people naturally assume.

The prefix you are looking for would be expressed as bbb/ rather than /bbb/.

Upvotes: 1

Jan Zeiseweis
Jan Zeiseweis

Reputation: 3738

According to the documentation you've to put in the credentials slightly different:

require 'aws-sdk'

Aws.config.update({
  region: 'us-west-2',
  credentials: Aws::Credentials.new('akid', 'secret')
})

Maybe try this to list the content of the bucket:

s3.list_objects(bucket:'aaa').each do |response|
  puts response.contents.map(&:key)
end

Upvotes: 0

Related Questions