Rails 4, Paperclip, Amazon S3 Config Amazon Path

2020-02-26 11:07发布

问题:

I'm trying to configure the endpoint which is returned from paperclip when my object is successfully uploaded to Amazon's S3 service. The upload and everything is working correctly, but the URL that is being returned is incorrect for displaying the upload.

Right now, the url that is being returned is http://s3.amazonaws.com/path/to/my/items (as seen in the picture below).

Instead of s3.amazonaws.com, I would like the root to be specific to the bucket's location (e.g. s3-us-west-1.amazonaws.com/path/to/my/items)

Where should I try and configure a different url path (from s3.amazonaws.com to something else)? I've tried to add a url with the above path into my configuration file like:

  #Paperclip Amazon S3
  config.paperclip_defaults = {
      :storage => :s3,
      :url => "https://s3-us-west-1.amazonaws.com/",
      :s3_credentials => {
          :bucket => ENV['S3_BUCKET_NAME'],
          :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
          :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']

      }

  }

Which did not appear to have any effect. Please advise on where I should be setting this option!

Thanks in advance!

回答1:

If you're going to use S3, we've found that you have to include the S3 credentials in your actual model (not just the config files). Here's what we do:

Model

#Image Upload 
Paperclip.options[:command_path] = 'C:\RailsInstaller\ImageMagick'
has_attached_file :image,
        :styles => { :medium => "x300", :thumb => "x100" },
        :default_url => "****",
        :storage => :s3,
        :bucket => '****',
        :s3_credentials => S3_CREDENTIALS,
            :url => "/:image/:id/:style/:basename.:extension",
            :path => ":image/:id/:style/:basename.:extension"

config/application.rb

  # Paperclip (for Amazon) (we use EU servers)
  config.paperclip_defaults = {
    :storage => :s3,
    :s3_host_name => 's3-eu-west-1.amazonaws.com'
  }

config/s3.yml

#Amazon AWS Config
development:
  access_key_id: **********
  secret_access_key: **************
  bucket: ****

production:
  access_key_id: ***********
  secret_access_key: ***********
  bucket: ****

Hope this helps?



回答2:

I also had the same problem when migrating to Spree 2.2 and am still not sure how to solve it the correct way. It seems like Paperclip should have been updating the path from the configuration, but it isn't.

Lacking a better solution, I've overridden the Spree::Image class like this:

1 Spree::Image.class_eval do
2   has_attached_file :attachment, 
3     styles: { mini: '48x48>', small: '100x100>', product: '240x240>', large: '600x600>' },
4     default_style: :product,
5     url: '/spree/products/:id/:style/:basename.:extension',
6     path: 'products/:id/:style/:basename.:extension',
7     convert_options: { all: '-strip -auto-orient -colorspace sRGB' }·
8 end 


回答3:

After some experimentation I have found that setting :s3_host_name globally suffices. I ended up with the same problem because I was setting :s3_region, which was being used by Paperclip (post-4.3.1, with aws-sdk 2) for storing attachments, but not when generating the URLs.

This may also be of interest to readers who end up on this problem: https://github.com/thoughtbot/paperclip/wiki/Restricting-Access-to-Objects-Stored-on-Amazon-S3