[英]Paperclip does not show image but instead shows title with amazon s3 in rails?
I am using paperclip and amazon s3 to store images for my website. 我正在使用回形针和亚马逊S3存储我网站的图像。 Everything is working correctly without s3 in development mode but when I try to use s3 it shows me the title of the image and not the image. 在开发模式下没有s3,一切都可以正常工作,但是当我尝试使用s3时,它将显示图像的标题而不是图像的标题。 I have copied what is in the heroku guide https://devcenter.heroku.com/articles/paperclip-s3 . 我已经复制了heroku指南https://devcenter.heroku.com/articles/paperclip-s3中的内容 。
This is the gemfile 这是gemfile
gem 'paperclip'
gem 'aws-sdk', '~> 2.3'
this is in profile.rb in class profile 这是在类profile中的profile.rb中
has_attached_file :avatar,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => ":style/missing.jpg"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
this is in production.rb 这是在production.rb中
config.paperclip_defaults = {
storage: :s3,
s3_host_name: "s3-#{ENV['AWS_REGION']}.amazonaws.com",
:s3_protocol => :https,
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'),
}
}
views 意见
<%= image_tag @user.profile.avatar.url(:medium) %>
i have copied the heroku guide and added the host name and s3 protocol bit. 我已经复制了heroku指南,并添加了主机名和s3协议位。 whats wrong here? 这怎么了
I have followed the heroku guide and was successfully able to upload the user image to S3 using paperclip in development env and I can also see the image in users's profile. 我遵循了heroku指南,并且能够在开发环境中使用回形针成功地将用户图像上传到S3,并且我还可以在用户的个人资料中看到该图像。
My Gemfile : 我的Gemfile :
gem 'paperclip'
gem 'aws-sdk-s3'
My development.rb 我的development.rb
config.paperclip_defaults = {
storage: :s3,
s3_host_name: "s3.#{ENV['AWS_REGION']}.amazonaws.com",
s3_protocol: :https,
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'),
}
}
you have this (this might have caused an issue): 您有这个(这可能引起了问题):
s3_host_name: "s3-#{ENV['AWS_REGION']}.amazonaws.com",
I have this (a dot after s3): 我有这个(s3之后的点):
s3_host_name: "s3.#{ENV['AWS_REGION']}.amazonaws.com",
My user.rb is same as yours 我的user.rb与您的一样
has_attached_file :avatar,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => ":style/missing.jpg"
# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
My show.html.erb 我的show.html.erb
<%= image_tag @user.avatar.url(:medium) %>
I am using rails 5.2 version and with above code it works for me. 我正在使用rails 5.2版本,并且上面的代码对我有用。 Also do check whether your S3 bucket has public access to objects. 还要检查您的S3存储桶是否对对象具有公共访问权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.