将图像从Rails上传到亚马逊s3时,Paperclip返回错误的URL

时间:2014-06-14 17:13:07

标签: ruby-on-rails amazon-web-services amazon-s3 paperclip

我使用rails将图像上传到亚马逊s3,并且返回的网址不正确。但是,我注意到:key中有正确的url,查看我的dev服务器日志:

  

开始GET“/ product / add?index = 0”for 96.250.13.59 at 2014-06-14 13:04:39 -0400   ProductsController处理#saveProduct为HTML     参数:{“index”=>“0”}   “http://s3-us-west-2.amazonaws.com/vigme/products/image_s3s//original/goods_09_086661?1402765480”      (0.3ms)BEGIN     SQL(4.5ms)INSERT INTO productsbuy_urlftp_transfer_datetimeftp_transfer_deleted_sourceftp_transfer_deleted_source_datetimeftp_transfer_processedimage_s3_content_type,{ {1}},image_s3_file_nameimage_s3_file_sizeimage_s3_updated_at)VALUES('http://www.uniqlo.com/us/women/outerwear/jackets-and-coats/stretch-tailored/women-stretch-tailored-jacket-086661.html#08','2014-06-14 17:04:40',1,'2014 -06-14 17:04:40',1,'image / jpeg','goods_09_086661',19478,'2014-06-14 17:04:40','http://s3-us-west-2.amazonaws.com/vigme/products/image_s3s//original/goods_09_086661?1402765480')   [paperclip] saving / products / image_s3s / 001/855/192 / original / goods_09_086661   [AWS S3 200 0.845202 0重试] put_object(:acl =>:public_read,:bucket_name =>“vigme”,:content_length => 19478,:content_type =>“image / jpeg”,:data => Paperclip :: HttpUrlProxyAdapter:goods_09_086661,:key =>“products / image_s3s / 001/855/192 / original / goods_09_086661”)

最后一个符号:key,如果添加到amazon s3网址,则包含正确的网址。但是,它将返回顶部的URL。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

好的,我自己想出了答案,所以只是贴出来帮助那些发现类似问题的人。问题是,在将图像的URL分配给产品实例后,我没有保存产品。因此,没有要求s3返回图像的URL,因为图像尚未保存在存储桶中。 e.g。

product.image_s3 = url_for_image
product.save

# now i can access it
s3_image_url = product.image_s3.url

而不是之前,我试图做的事情:

product.image_s3 = url_for_image
s3_image_url = product.image_s3.url
product.save