我跟着這個tutorial,並建立一個使用回形針和S3的rails文件共享應用程序。上傳文件到S3是可以正常使用,但是當我點擊上傳的文件名,我得到這個錯誤:Rails和亞馬遜S3
OpenURI::HTTPError in AssetsController#get
301 Moved Permanently (Invalid Location URI)
它指向我
app/controllers/assets_controller.rb:15:in `get'
這裏是我的代碼:
資產.controller.rb
def get
asset = current_user.assets.find_by_id(params[:id])
if asset
#Parse the URL for special characters first before downloading
data = open(URI.parse(URI.encode(asset.uploaded_file.url)))
#then again, use the "send_data" method to send the above binary "data" as file.
send_data data, :filename => asset.uploaded_file_file_name
#redirect to amazon S3 url which will let the user download the file automatically
#redirect_to asset.uploaded_file.url, :type => asset.uploaded_file_content_type
else
flash[:error] = "Don't be cheeky! Mind your own assets!"
redirect_to root_url
end
end
assets.controller.rb
attr_accessible :user_id, :uploaded_file
belongs_to :user
#set up "uploaded_file" field as attached_file (using Paperclip)
has_attached_file :uploaded_file,
:path => "assets/:id/:basename.:extension",
:storage => :s3,
:s3_credentials => ::Rails.root.join('config/amazon_s3.yml'),
:bucket => "Sharebox"
validates_attachment_size :uploaded_file, :less_than => 10.megabytes
validates_attachment_presence :uploaded_file
def file_name
uploaded_file_file_name
end
end
任何幫助將不勝感激。謝謝!
你的存儲桶在哪個區域?我認爲如果您嘗試通過不同區域(您的代碼將使用的默認區域是us-east-1)連接「S3」,S3會將您重定向到正確的區域 – 2013-04-30 21:42:59
這固定了S3下載的問題,但現在下載已損壞並無法打開。我收到以下錯誤:無法打開文件「Adobe-PDF-Document-icon(1).png」。 t可能會被損壞或使用Preview無法識別的文件格式。 – user2337569 2013-05-01 01:55:26