简体   繁体   English

使用Paperclip在Ruby应用上显示AWS S3图像

[英]Display AWS S3 images on Ruby app with Paperclip

I can't seem to get AWS-S3 working with my Ruby app, in both production, and development. 在生产和开发中,我似乎都无法使AWS-S3与我的Ruby应用程序一起使用。 I can get the files uploaded to my S3 Bucket, but how do I display them? 我可以将文件上传到S3存储桶,但是如何显示它们? I am using this for user profile avatars, so that the users can upload their own avatar. 我将其用于用户个人资料头像,以便用户可以上传自己的头像。

here is my current image tag: 这是我当前的图像标签:

<%= image_tag @user.profile.avatar.url, class: 'user-show-avatar' %>

I've place my avatar upload info in the profiles model which is where I have the avatar information. 我已将我的头像上传信息放置在个人资料模型中,这是我具有头像信息的地方。

profile.rb profile.rb

class Profile < ActiveRecord::Base
  belongs_to :user
  has_attached_file  :avatar, 
                    :styles => { :medium => "460x>", :thumb => "100x100>",:vnice=> "400x" },
                    :storage => :s3,
                    :bucket => 'mybucket',
                    :s3_credentials => "#{Rails.root}/config/aws.yml",
                    :path => "resources/:id/:style/:basename.:extension"

    validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/

end

_form.html.erb (edit profile form) _form.html.erb(编辑配置文件表单)

<%= form_for @profile, url: user_profile_path, :html => { :multipart => true } do |f| %>
        <div class="form-group">
          <%= f.label :avatar %>
          <%= f.file_field :avatar, class: 'form-control' %>
        </div>
<% end %>

config/initializer/paperclip.rb 配置/初始化/paperclip.rb

Paperclip::Attachment.default_options[:storage] = :s3

Paperclip::Attachment.default_options[:s3_credentials] = {
  :bucket => ENV['AWS_BUCKET'],
  :access_key_id => ENV['AWS_KEY'],
  :secret_access_key => ENV['AWS_SECRET_KEY'],
  s3_region: 'us-east-1'
}

Paperclip::Attachment.default_options[:s3_options] = {
  endpoint: 'https://objects-us-east-1.io'
}

Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
Paperclip::Attachment.default_options[:s3_host_name] = 'objects-us-east-1'
Paperclip::Attachment.default_options[:s3_protocol] = 'https'

What am I doing wrong? 我究竟做错了什么? What is the proper way to display images in Ruby, from S3 using paperclip? 使用回形针从S3用Ruby显示图像的正确方法是什么?

Solved it. 解决了。

Deleted the paperclip.rb initialiser, and added :url => ':s3_domain_url' to my profile model 删除了paperclip.rb初始化程序,并将:url =>':s3_domain_url'添加到我的个人资料模型中

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

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