简体   繁体   English

Carrierwave / fog正在将文件上传到S3,但是该应用仍在尝试使用本地路径访问图像

[英]Carrierwave/fog are uploading files to S3, but the app is still trying to use the local path to access images

I know this is a broad question, and I'm biting off a little more than I can chew for a first stab at a rails app, but here I am. 我知道这是一个广泛的问题,我的努力超出了我可以尝试在Rails应用程序中首次尝试的能力,但是我在这里。

I tried to add an image upload/crop to a basic status app. 我试图将图片上传/裁剪添加到基本状态应用中。 It was working just fine uploading the images and cropping them with carrierwave, but as soon as I started using Fog to upload to S3, I ran into issues. 上传图像并使用载波裁剪图像效果很好,但是当我开始使用Fog上传到S3时,我遇到了问题。

The image, and it's different sizes, appear to be ending up on S3 just fine, but the app is still trying to access the image as "/assets/uploads/entry/image/65/large_IMG_0035.jpg" 图像及其大小不同,似乎可以在S3上结束,但该应用仍在尝试以“ /assets/uploads/entry/image/65/large_IMG_0035.jpg”访问该图像

Locally, it just shows a broken image, but on Heroku it breaks the whole thing because 在本地,它只是显示一个破碎的图像,但是在Heroku上,它会破坏整个东西,因为

ActionView::Template::Error (uploads/entry/image/1/large_IMG_0035.jpg isn't precompiled ActionView :: Template :: Error(未预编译上载/条目/图像/1/large_IMG_0035.jpg

The heroku error makes sense to me because it shouldn't be there. heroku错误对我来说很有意义,因为它不应该在那里。 I've combed through the app but don't know what's forcing this. 我已经浏览了该应用程序,但不知道是什么在迫使它。 I'll post any code anybody thinks will work? 我将张贴任何人认为可行的代码? Thanks in advance! 提前致谢!

Clarification: 澄清:

Just to clarify, the images are uploading to S3 fine, the problem is how the app is trying to display the image_url 只是为了澄清,图像已上传到S3很好,问题在于应用程序如何尝试显示image_url

The app is using a local path in the asset pipeline, not the S3 path that it's actually uploading to. 该应用程序正在资产管道中使用本地路径,而不是其实际上传到的S3路径。

I was having the same issue. 我有同样的问题。 In my Carrierwave Initializer I was setting host to s3.amazonaws.com but when I removed that line altogether urls started working. 在我的Carrierwave Initializer中,我将host设置为s3.amazonaws.com但是当我删除该行时,URL完全开始工作。

I hope this helps you resolve your issue, I fought this for several hours! 希望这可以帮助您解决问题,我为此奋斗了几个小时!

I believe this issue is related to how you are accessing your image in your view. 我认为此问题与您如何访问视图中的图像有关。

If you have mounted an uploader on the field avatar in the following manner: 如果您通过以下方式在现场头像上安装了一个上传器:

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
end

You would access it in your ERB as follows: 您可以按以下方式在ERB中访问它:

 <%= image_tag(@user.avatar_url) %>

I would also suggest watching the following Railscast on the topic. 我还建议您观看以下有关该主题的Railscast。

http://railscasts.com/episodes/253-carrierwave-file-uploads http://railscasts.com/episodes/253-carrierwave-file-uploads

Re-reading issue, I bet it has to do with Carrierwave using Herkou. 重读问题,我敢打赌这与使用Herkou的Carrierwave有关。

Give this a glance and see if it helps. 扫一眼,看看是否有帮助。

https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Make-Carrierwave-work-on-Heroku https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Make-Carrierwave-work-on-Heroku

I am not clear what exactly do you want to achieve. 我不清楚您到底想实现什么。
But for now I have 2 ideas: 但目前我有两个想法:

  1. For assets host in CDN, you can take a look at this: https://devcenter.heroku.com/articles/cdn-asset-host-rails31 对于CDN中的资产托管者,您可以查看以下内容: https : //devcenter.heroku.com/articles/cdn-asset-host-rails31

  2. If you want the images to be part of a model-relation, here's my rough idea: 如果您希望图像成为模型关系的一部分,这是我的粗略想法:
    Put the images path in a table column. 将图像路径放在表格列中。 For further information about this you can browse carrierwave github site.(It has many docs and tutorial) 有关此的更多信息,您可以浏览carrierwave github网站。(它有许多文档和教程)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM