【问题标题】:What does this error mean in Rails: "Errno::ENOENT in AssetsController#get ...No such file or directory...."这个错误在 Rails 中是什么意思:“Errno::ENOENT in AssetsController#get ...No such file or directory....”
【发布时间】:2012-06-20 03:42:33
【问题描述】:

我正在编写一个应用程序来在 Amazon S3 上存储文件。我非常接近,我能够使用 url 保存和检索文件。但是,由于这些链接是公开的,我试图在我的 assetscontroller 中使用以下方法从 S3 检索存储的文件。

作为链接,可以在浏览器中查看/访问这些文件,但如果我使用此代码:

#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'

有什么线索吗?

谢谢

【问题讨论】:

    标签: ruby-on-rails file-upload amazon-s3 paperclip


    【解决方案1】:

    您也可以只将用户重定向到 S3 上的文件。

    试试看

    redirect_to asset.uploaded_file.url
    

    而不是send_file。 send_file 需要一个本地文件的路径,然后由网络服务器使用。

    如果你设置了s3_permissions => :private,那么你需要调用

    redirect_to asset.uploaded_file.expiring_url(10)
    

    有趣的是,在您的错误消息中它是 http 对 s3 中的 https - 您也可以尝试将以下选项添加到模型的 has_attached_file 中

    :s3_protocol => 'https' 
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多