简体   繁体   English

Rails回形针Heroku AWS DIsplay图像问题

[英]Rails Paperclip Heroku AWS DIsplay Image issue

I set up paperclip in a rails app and this worked fine locally and on heroku, however the images uploaded in posts were only saved in heroku for a short space of time. 我在Rails应用程序中设置了回形针,在本地和heroku上都可以正常工作,但是帖子中上传的图像只保存了很短的时间在heroku中。 I set up an AWS account and created a bucket, and followed through the documentation to link my rails app to AWS to display images uploaded with paperclip. 我设置了一个AWS账户并创建了一个存储桶,然后按照说明文档将Rails应用程序链接到AWS,以显示使用回形针上传的图像。 I have attached code snippets below. 我在下面附加了代码片段。 The images seem to be uploaded to AWS fine, however when I create a post, it says my post has been created successfully, however the image does not display, it just displays as a broken link. 这些图像似乎可以上传到AWS上,但是,当我创建帖子时,它表示我的帖子已成功创建,但是该图像没有显示,只是显示为断开的链接。 When I click image properties, it hs the AWS S3 url and upon looking in heroku logs, there are no known issues. 当我单击图像属性时,它具有AWS S3 URL,并且在heroku日志中查找时,没有已知问题。 I do not know why the image is not displaying. 我不知道为什么无法显示图像。

config/environments/production.rb file: config / environments / production.rb文件:

 # sets paperclip to upload images to Amazon S3 # Variables directed to heroku via the command line for pw etc config.paperclip_defaults = { storage: :s3, s3_credentials: { bucket: ENV.fetch('S3_BUCKET_NAME'), access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'), secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'), s3_region: ENV.fetch('AWS_REGION'), } } 

post.rb model file: post.rb模型文件:

 class Post < ApplicationRecord extend FriendlyId friendly_id :title, use: [:slugged, :finders] has_attached_file :image, styles: { medium: "600x", thumb: "100x" } validates_attachment_content_type :image, content_type: /\\Aimage\\/.*\\z/ end 

show.html.erb file: show.html.erb文件:

 <div class="image"> <%= image_tag @post.image.url(:medium) %> </div> 

I have also set up heroku correctly using the following: 我还使用以下命令正确设置了heroku:

$ heroku config:set S3_BUCKET_NAME=your_bucket_name
$ heroku config:set AWS_ACCESS_KEY_ID=your_access_key_id
$ heroku config:set AWS_SECRET_ACCESS_KEY=your_secret_access_key
$ heroku config:set AWS_REGION=your_aws_region

Any help would be appreciated. 任何帮助,将不胜感激。

Many thanks 非常感谢

As per Judd's answer for Paperclip, S3, Heroku: Missing Image , it was because I needed to state the s3_host_name in the paperclip default settings: 根据Judd对Paperclip,S3,Heroku:缺少图像的回答,这是因为我需要在回形针的默认设置中声明s3_host_name:

 # sets paperclip to upload images to Amazon S3 # Variables directed to heroku via the command line for pw etc config.paperclip_defaults = { storage: :s3, s3_host_name: 's3-eu-west-1.amazonaws.com', s3_credentials: { bucket: ENV.fetch('S3_BUCKET_NAME'), access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'), secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'), s3_region: ENV.fetch('AWS_REGION'), } 

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

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