我試圖從我的AWS存儲桶中下載文件並將其存儲在我的臨時文件夾中。Rails AWS S3:下載文件並存儲在臨時文件夾中
以下代碼返回此錯誤:OpenURI :: HTTPError(301永久移動(無效位置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)
第一桶調用返回正確的對象,第二個返回此:
https://ekohotstorage.s3-us-west-2.amazonaws.com/location_info
當你去到url這是什麼返回
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)
然而,從下面的代碼創建的網址確實會導致聲音文件。
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
思考在哪裏?