Rails 4, Paperclip, Amazon S3 Amazon Path Configuration

I am trying to configure the endpoint that comes back from a clip when my object is successfully uploaded to Amazon S3. The download and everything works correctly, but the return URL is incorrect to display the download.

The currently returned URL is http://s3.amazonaws.com/path/to/my/items (as shown in the image below).

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

enter image description here

Where can I try and configure a different url (from s3.amazonaws.com to something else)? I tried adding the URL with the above path to the configuration file, for example:

  #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, apparently, has no effect. Please advise where I should set this parameter!

Thanks in advance!

+6
source share
3 answers

If you intend to use S3, we find that you need to include the S3 credentials in your actual model (and not just the configuration files). Here is 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" 

configurations /application.rb

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

config / s 3.yml

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

Hope this helps?

+11
source

I also had the same problem when porting to Spree 2.2, and I still don't know how to solve it correctly. It seems that Paperclip should have updated the path from the configuration, but that is not the case.

Without a better solution, I redefined the Spree :: Image class as follows:

 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 
0
source

After some experimentation, I found that setting :s3_host_name globally is sufficient. I ran into the same problem because I installed :s3_region , which was used by Paperclip (post-4.3.1, with aws-sdk 2) to store attachments, but not when creating URLs.

It may also be of interest to readers who ultimately run into this problem: https://github.com/thoughtbot/paperclip/wiki/Restricting-Access-to-Objects-Stored-on-Amazon-S3

0
source

Source: https://habr.com/ru/post/955879/


All Articles