2012-06-18 31 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' 

希望这会有所帮助。

相关问题