【发布时间】:2014-04-23 22:44:28
【问题描述】:
我正在尝试从我的 AWS 存储桶下载一个文件并将其存储在我的临时文件夹中。
以下代码返回此错误:OpenURI::HTTPError (301 Moved Permanently (Invalid Location URI)):
@filename 是存储桶中的文件名,包括扩展名,为字符串。
S3 = AWS::S3.new(
:access_key_id => ENV["AWS_ACCESS_KEY_ID"],
:secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"]
)
BUCKET = S3.buckets["name_of_bucket"]
File.open(Rails.root.join("tmp",@filename), "wb") do |file|
file.write open(BUCKET.objects[@filename].url_for(:read)).read
end
这是我目前发现的:
puts BUCKET.objects['name_of_bucket']
puts BUCKET.objects['name_of_bucket'].url_for(:read)
第一个 BUCKET 调用返回正确的对象,第二个返回这个:
https://ekohotstorage.s3-us-west-2.amazonaws.com/location_info
当您转到该网址时,这就是返回的内容
The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
此 (http://www.sitefinity.com/developer-network/forums/set-up-installation/amazon-s3---must-be-addressed-using-the-specified-endpoint) 声明“S3 中存在限制,即如果您创建的存储桶不是“美国标准”,则不能在存储桶地址中使用路径样式语法。”
但是,我检查了这个存储桶,它是 def 的。地区美国标准。
编辑:
我很愚蠢;我读过 AWS 默认是美国标准。但是西方不是标准,东方是所以我猜它不默认为美国标准。
在用
修复了这种愚蠢之后S3 = AWS::S3.new(
:access_key_id => ENV["AWS_ACCESS_KEY_ID"],
:secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"],
:region => "us-east-1"
)
我现在收到此错误:
OpenURI::HTTPError (404 Not Found)
但是,从以下代码创建的 url 确实会指向声音文件。
BUCKET.objects['name_of_bucket'].url_for(:read)
所以问题一定出在
File.open(Rails.root.join("tmp",@filename), "wb") do |file|
file.write open(BUCKET.objects[@filename].url_for(:read)).read
end
想法?
【问题讨论】:
标签: ruby-on-rails amazon-web-services amazon-s3