首页 文章

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

提问于
浏览
0

在 生产环境 和开发方面,我似乎无法让AWS-S3使用我的Ruby应用程序 . 我可以将文件上传到我的S3 Bucket,但是如何显示它们?我将此用于用户 Profiles 头像,以便用户可以上传自己的头像 .

这是我目前的图片标签:

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

我将我的头像上传信息放在 Profiles 模型中,这是我拥有头像信息的地方 .

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(编辑 Profiles 表格)

<%= 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 %>

配置/初始化/ 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'

我究竟做错了什么?在使用paperclip的S3中,在Ruby中显示图像的正确方法是什么?

1 回答

  • 0

    解决了它 .

    删除了paperclip.rb初始化程序,并在我的 Profiles 模型中添加了:url =>':s3_domain_url'

相关问题