2012-06-18 42 views
0

我正在編寫一個應用程序來在Amazon S3上存儲文件。我很接近,我能夠保存和檢索使用URL的文件。但是,因爲這些鏈接是公開的我試圖在我的assetscontroller中使用以下方法從S3中檢索存儲的文件。這個錯誤在Rails中意味着什麼:「Errno :: ENOENT in AssetsController#get ...沒有這樣的文件或目錄....」

的鏈接,這些文件可以查看/瀏覽器訪問,但如果我用這個代碼:

#This action will let the users download the files (after a simple authorization check) 
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 

else 
    flash[:error]="Access Violation" 
    redirect_to assets_path 
end 

即時得到這個錯誤在我的瀏覽器:

Errno::ENOENT in AssetsController#get 
No such file or directory - http://s3.amazonaws.com/BUCKETNAME/assets/29/FILENAME.jpeg? 1339979591 

當我登錄到S3管理控制檯時,當點擊S3站點上的資源時,該文件顯示在我的瀏覽器中,其鏈接是

https://s3.amazonaws.com/BUCKETNAME/assets/29/FILENAME.jpeg?  AWSAccessKeyId=XXXXXXXXXXXExpires=1340003832&Signature=XXXXXXXXXXXXXXXXXX-amz-security-  token=XXXXXXXXX//////////XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 

所以它確實存在,但無法通過我的應用程序

這裏訪問是從我的瀏覽器我的應用程序跟蹤:

app/controllers/assets_controller.rb:99:in `initialize' 
app/controllers/assets_controller.rb:99:in `open' 
app/controllers/assets_controller.rb:99:in `get' 

上怎麼回事任何線索?

謝謝

回答

0

您也可以將用戶重定向到S3上的文件。

試試看

redirect_to asset.uploaded_file.url 

,而不是send_filesend_file需要一個到本地文件的路徑,然後由web服務器使用。

如果您已設置s3_permissions => :private,那麼你需要調用

redirect_to asset.uploaded_file.expiring_url(10) 

這也是有趣的是,在你的錯誤消息,則是http針對S3 HTTPS - 你也可以嘗試下面的選項添加到您的模型的has_attached_file

:s3_protocol => 'https' 

希望這會有所幫助。

相關問題