[英]aws-sdk for Ruby access folder within bucket
I have a bucket on the Amazon S3 with the folder in it. 我在Amazon S3上有一个存储桶,其中包含文件夹。 I'm trying to access it the following way via aws-sdk gem:
我试图通过aws-sdk gem以下方式访问它:
s3 = AWS::S3.new(
:access_key_id => "KEY",
:secret_access_key => "SECRET"
)
bucket = s3.buckets["my_bucket/my_folder"]
bucket.do_stuff....
I am getting the following error in return: 我得到以下错误作为回报:
The bucket you are attempting to access must be addressed using the specified endpoint. 您尝试访问的存储区必须使用指定的端点进行寻址。 Please send all future requests to this endpoint.
请将以后的所有请求发送到此端点。
Any ideas what I may be doing wrong? 我有什么想法可能做错了吗?
probably the S3 bucket are trying to use is located outside the US-EAST (default location), so this should help you: 可能S3存储桶正在尝试使用位于US-EAST(默认位置)之外,所以这应该可以帮助您:
s3 = AWS::S3.new(
:access_key_id => "KEY",
:secret_access_key => "SECRET"
:s3_endpoint => 's3-eu-west-1.amazonaws.com'
)
Choose your S3 enpdpoint from the following list : 从以下列表中选择您的S3 enpdpoint:
US Standard * s3.amazonaws.com(default)
US West (Oregon) Region s3-us-west-2.amazonaws.com
US West (Northern California) Region s3-us-west-1.amazonaws.com
EU (Ireland) Region s3-eu-west-1.amazonaws.com
Asia Pacific (Singapore) Region s3-ap-southeast-1.amazonaws.com
Asia Pacific (Tokyo) Region s3-ap-northeast-1.amazonaws.com
South America (Sao Paulo) Region s3-sa-east-1.amazonaws.com
In terms of object access, the bucket name is my_bucket , but my_folder should be a part of object. 在对象访问方面,存储桶名称为my_bucket ,但my_folder应该是对象的一部分。
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). 您可以通过使用DNS可比较的存储桶名称来避免这种情况(也可以避免存储桶名称中的点)。 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". 将存储桶命名为“bucket-name”,对象键为“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:
当存储桶名称与dns不兼容时,则网址如下所示:
http://s3.amazon.com/bucket/name/path/to/object.txt
In the example above, the bucket is "bucket/name", which is not dns compatible. 在上面的示例中,存储桶是“存储桶/名称”,与dns不兼容。 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).
它成为路径的一部分,现在必须用区域特定端点替换s3.amazon.com(如果未在经典区域中创建存储桶)。
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
has_attached_file :photo,
storage: :s3,
styles: { medium: "300x300>", thumb: "100x100>" },
s3_credentials: "#{Rails.root}/config/aws.yml",
bucket: "bucket-name",
s3_host_name: "s3-ap-southeast-1.amazonaws.com",
url: ":s3_domain_url",
path: 'books/:id/photo/:style_:basename.:extension'
worked for me :) 为我工作:)
In case anyone is looking for this, here is how it should work according to https://github.com/aws/aws-sdk-ruby 如果有人正在寻找这个,这是它应该如何工作根据https://github.com/aws/aws-sdk-ruby
creds = JSON.load(File.read('secrets.json'))
Aws.config[:credentials] = Aws::Credentials.new(creds['AccessKeyId'], creds['SecretAccessKey'])
Aws.config[:region] = 'us-east-1'
client = Aws::S3::Client.new(
region: Aws.config[:region],
credentials: Aws.config[:credentials]
)
File.open('/local_directory/picture.jpg', 'rb') do |file|
client.put_object(bucket: 'bucket', key: 'folder_inside_bucket/picture.jpg', body: file)
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.