aws-sdk for Ruby access folder within bucket

后端 未结 5 1113
[愿得一人]
[愿得一人] 2020-12-25 08:09

I have a bucket on the Amazon S3 with the folder in it. I\'m trying to access it the following way via aws-sdk gem:

s3 = AWS::S3.new(
    :         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-25 09:03

    You need to configure your region specific endpoint for the bucket (where it was created). You can do this with:

    AWS.config(:s3_endpoint => '...')
    s3 = AWS::S3.new
    

    or

    s3 = AWS::S3.new(:s3_endpoint => '...')
    

    You can avoid this in the future by using DNS comptible bucket names (also avoid dots in bucket names). If a bucket name is a valid subdomain, then you can address your bucket without configuring the region specific endpoint. Consider the following:

    http:://bucket-name.s3.amazonaws.com/path/to/object.txt
    

    Where the bucket is named "bucket-name" and the object key is "path/to/object.txt". This bucket could exist in any region, and yet you can access it using the "default" region. When the bucket name is not dns-compatible, then the url looks like:

    http://s3.amazon.com/bucket/name/path/to/object.txt
    

    In the example above, the bucket is "bucket/name", which is not dns compatible. It becomes part of the path, and now s3.amazon.com must be replaced with the region specific endpoint (if the bucket was not created in the classic region).

    As someone else mentioned, paths should be part of the object key, not bucket name. This allows you to group objects by a common prefix. The '/' is used as a virtual folder (by convention only).

    # print the key of every object with the given prefix
    s3.buckets['bucket-name'].objects.with_prefix('path/to/').each do |object|
      puts object.key
    end
    

提交回复
热议问题