get broken pipe while uploading image to s3 amazon using paper clip - ruby-on-rails

I get a broken pipe error while uploading image to s3 amazon using paper clip
My Model:
has_attached_file :avatar, :styles => { :small => "100x100#", :large => "500x500>" },
:processors => [:cropper],
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/:style/:id/:filename",
:bucket => "shahbunder"
My s3.yml:
development:
bucket: xxx
access_key_id: xxx
secret_access_key: xxx
test:
bucket: xxx
access_key_id: xxx
secret_access_key: xxx
production:
bucket: xxx
access_key_id: xxx
secret_access_key: xxx

Note for people searching for a solution to an error that crosses between this and ERRCONNRESET - Response time skewed - your server clock is not synchronized with Amazon correctly.

This error occurs if you type your bucket name using "/" (ex: "bucket_name/"), use only the name (ex: "bucket_name").

I believe this is usually because your s3 credentials are wrong. But here are 2 different things you can try:
script/plugin install git://github.com/thoughtbot/paperclip.git (installing paperclip as a plugin instead of a gem has helped some)
gem install right_aws right_http_connection (make sure you are firing off your request correctly)

Try using Fog instead, I don't know if it's still undocumented or what:
Example (fit to your needs):
has_attached_file :media,
storage: :fog,
hash_secret: Settings.aws.uploader.hash_secret,
use_timestamp: Settings.aws.uploader.use_timestamps_in_url,
fog_credentials: Settings.aws.uploader.fog.to_hash,
fog_public: Settings.aws.uploader.public_files,
fog_directory: Settings.aws.s3.bucket_cname,
fog_host: "http://s.my.com",
default_url: "media/system/not_available.mp3",
hash_data: ":class/:attachment/:id/:style/:updated_at",
path: ":root_path/:id_partition",
#only_process:
processors: [:audio_thumbnail],
styles: { small: ['36x36#', :jpg], medium: ['72x72#', :jpg], large: ['115x115#', :jpg] },
skip_updated_at: true

Related

Paperclip does not show image but instead shows title with amazon s3 in rails?

I am using paperclip and amazon s3 to store images for my website. Everything is working correctly without s3 in development mode but when I try to use s3 it shows me the title of the image and not the image. I have copied what is in the heroku guide https://devcenter.heroku.com/articles/paperclip-s3.
This is the gemfile
gem 'paperclip'
gem 'aws-sdk', '~> 2.3'
this is in profile.rb in class profile
has_attached_file :avatar,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => ":style/missing.jpg"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
this is in production.rb
config.paperclip_defaults = {
storage: :s3,
s3_host_name: "s3-#{ENV['AWS_REGION']}.amazonaws.com",
:s3_protocol => :https,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
}
}
views
<%= image_tag #user.profile.avatar.url(:medium) %>
i have copied the heroku guide and added the host name and s3 protocol bit. whats wrong here?
I have followed the heroku guide and was successfully able to upload the user image to S3 using paperclip in development env and I can also see the image in users's profile.
My Gemfile:
gem 'paperclip'
gem 'aws-sdk-s3'
My development.rb
config.paperclip_defaults = {
storage: :s3,
s3_host_name: "s3.#{ENV['AWS_REGION']}.amazonaws.com",
s3_protocol: :https,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
}
}
you have this (this might have caused an issue):
s3_host_name: "s3-#{ENV['AWS_REGION']}.amazonaws.com",
I have this (a dot after s3):
s3_host_name: "s3.#{ENV['AWS_REGION']}.amazonaws.com",
My user.rb is same as yours
has_attached_file :avatar,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => ":style/missing.jpg"
# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
My show.html.erb
<%= image_tag #user.avatar.url(:medium) %>
I am using rails 5.2 version and with above code it works for me. Also do check whether your S3 bucket has public access to objects.

File upload to Amazon S3 using paperclip

I'm uploading files to S3 using paperclip so would like to know if i can set the path something like,
:path => "/advertisements/:username/:filename”
the thing is that :usename is from other model; i'm uploading files on model_2 and :username comes from model_1. How can i set the path to indicate the :username
Sample:
:path => "/advertisements/#model_1.username/:filename”
Any ideas?
Thanks in advance!
Here is nice explanation:
please view the answer.
Rails 4, Paperclip, Amazon S3 Config Amazon Path
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: ****
I hope this is what you're looking for :)

Custom URL with Paperclip and AWS S3

We're using Paperclip with the aws-sdk gem to store and display images in our Rails app:
class User < ActiveRecord::Base
has_attached_file :image,
storage: :s3,
s3_credentials: 'config/s3.yml',
s3_protocol: :https,
styles: {
curriculum: '120x120>',
medium: '600x600>',
thumb: '200x200>'
},
default_url: 'missing_photo.png'
end
If I then use <%= image_tag current_user.image.url %> in an html.erb file, I get the following HTML: <img src="https://s3.amazonaws.com/<my_bucket>/users/images/000/000/001/medium/my_image.png?1419989041">.
How do I get that https://s3.amazonaws.com/<my_bucket> to be a custom URL like https://example.com? I have my domain all setup in Cloudfront along with its SSL certificate.
I looked up in the Paperclip S3 Storage documentation. There's a :url option, but nothing I write for that option seems to work.
I just ran across this problem and here are the settings I had to use
:s3_host_alias => "s3.example.com",
:url => ":s3_alias_url",
:path => ":class/:attachment/:id.:style.:extension"
From this link, I learned that, in addition to :s3_host_alias and :url, you have to specify path so you don't get
Paperclip::InfiniteInterpolationError
Kinda works out well because the default paperclip path is kinda wonky anyways.
Update
I put together an example and was able to get it working with the following:
class User < ActiveRecord::Base
has_attached_file :profile_picture,
styles: { :medium => "300x300>", :thumb => "100x100>" },
path: 'users/:attachment/:style-:hash.:extension',
hash_secret: "94dfda08e2ed473257345563594dfda08e2ed473257345563594dfda08e2ed473257345563594dfda08e2ed4732573455635",
default_url: "/images/:style/missing.png",
storage: :s3,
s3_protocol: 'http',
url: ':s3_alias_url',
s3_host_alias: 'distro1234.cloudfront.net',
s3_credentials: {
access_key_id: 'access_id',
secret_access_key: 's3cr3tK3y!',
acl: 'private',
bucket: 'my-bucket',
bucket_url: 'https://my-bucket.s3.amazonaws.com',
}
validates_attachment_content_type :profile_picture, :content_type => /\Aimage\/.*\Z/
end
And the following Gemfile:
gem 'paperclip'
gem 'aws-sdk', '~> 1.5.7'
Rails console:
=> u.profile_picture.url
=> "http://distro1234.cloudfront.net/users/profile_pictures/original-95eb509f9c81a341945a5a65e59e81880a739d39.jpg?1429638820"
Try something like this:
has_attached_file :image,
storage: :s3,
s3_credentials: 'config/s3.yml',
s3_protocol: :https,
styles: {
curriculum: '120x120>',
medium: '600x600>',
thumb: '200x200>'
},
url: ':s3_alias_url',
s3_host_alias: 'example.com',
default_url: 'missing_photo.png'

Rails 4, Paperclip, Amazon S3 Config Amazon Path

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!
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?
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
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

Paperclip : wrong bucket being used from yml

This has worked before, or so I believe.
For some reason, in my development environment, and even staging, Paperclip is using my production bucket instead of the development bucket.
Here is the part of the user model that relates to it
has_attached_file :avatar,
storage: :s3,
s3_credentials: "#{Rails.root}/config/s3.yml",
s3_permissions: :private,
path: "/:style/:id/:filename",
s3_protocol: "https",
styles: { medium: "300x300#", thumb: "100x100#", icon: "26x26#" },
default_url: ":style/ico_missing_user.png"
And here, my yml file:
common: &common
access_key_id: <%= ENV['S3_KEY'] %>
secret_access_key: <%= ENV['S3_SECRET'] %>
development:
<<: *common
bucket: mydevbucket
staging:
<<: *common
bucket: mystagingbucket
production:
<<: *common
bucket: myprodbucket
What am i doing wrong ?
I added "load_config.rb" file to my initializers directory:
load_config.rb
S3_CONFIG = YAML.load_file("#{::Rails.root}/config/s3.yml")[Rails.env]
and started using S3_CONFIG instead of "#{Rails.root}/config/s3.yml"
has_attached_file :avatar,
storage: :s3,
s3_credentials: S3_CONFIG,
s3_permissions: :private,
path: "/:style/:id/:filename",
s3_protocol: "https",
styles: { medium: "300x300#", thumb: "100x100#", icon: "26x26#" },
default_url: ":style/ico_missing_user.png"
The solution above worked fine to set the bucket, but then I started having a problem with paperclip when uploading files, saying ECONN:Aborted.
The following is what i have understood to work :
has_attached_file :avatar,
storage: :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:bucket => ENV['S3_BUCKET'],
s3_permissions: :private,
path: "/:style/:id/:filename",
s3_protocol: "https",
styles: { medium: "300x300#", thumb: "100x100#", icon: "26x26#" },
default_url: ":style/ico_missing_user.png"
Notice I separated the bucket from the credentials. I did this inspired on the rdocs for paperclip, and on the following sample

Resources