使用回形针在 Ruby 应用程序上显示 AWS S3 图像



我似乎无法让 AWS-S3 与我的 Ruby 应用程序一起工作,无论是在生产和开发中。我可以将文件上传到我的 S3 存储桶,但如何显示它们? 我将其用于用户个人资料头像,以便用户可以上传自己的头像。

这是我当前的图像标签:

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

我已将我的头像上传信息放在配置文件模型中,这是我拥有头像信息的地方。

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 (编辑个人资料表单)

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

我做错了什么?使用回形针从 S3 在 Ruby 中显示图像的正确方法是什么?

解决了。

删除了 paperclip.rb 初始化器,并将 :url => ':s3_domain_url' 添加到我的配置文件模型中

相关内容

最新更新