Resource interpreted as Image but transferred with MIME type application/xml aws - ruby-on-rails

I am using the paperclip and aws-sdk gems to upload avatars to AWS, but when I render the photos in my application, the console gives me the following response:
Resource interpreted as Image but transferred with MIME type application/xml aws
Below are my codes:
development.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => "appname-development",
:access_key_id => "##########",
:secret_access_key => "##########"
}
user.rb
attr_accessible :profile_picture
has_attached_file :profile_picture,
:styles => {
:big => '200x200>',
:small => '50x50#'
}
show.html.erb
<%= image_tag #user.profile_picture.url(:small) %>
I went to the S3 console and checked my photos, they all have the content-type of image/jpeg.
As I browsed around the internet, it looks like no one has really encountered this issue before, does anyone know what is going on?
Thank you.

This means, that something you get from S3 is not an image but often an error message in xml.
'puts' link to your photo into console and paste it to browser. You will see, that you have got not photo but an error message.

Related

AWS::S3::Errors::PermanentRedirect on Heroku

I'm getting following error on heroku after uploading file through paperclip.
AWS::S3::Errors::PermanentRedirect (The bucket you are attempting to
access must be addressed using the specified endpoint. Please send all
future requests to this endpoint.)
This is my settings in the model
has_attached_file :profile_image,
:styles => { :myrecipes => "260x180#"},
:storage => :s3,
:s3_region => 'us-west-1',
:s3_credentials => "#{Rails.root}/config/amazon_s3.yml",
:path => "/images/:id/:style.:extension",
:url => ":s3_domain_url"
This is working on development and store image on S3 but while I'm trying on production (Heroku) I'm getting error.
To Provide the endpoint you have to do add this into your paperclip_defaults
:s3_host_name => "s3-eu-west-1.amazonaws.com"
Or you can do like this
s3_host_name: "s3-#{ENV['AWS_REGION']}.amazonaws.com"
Ref: paperclip issue

Paperclip, S3, Heroku: Missing Image

Long time viewer, first time asker. I've searched for this topic but don't believe I've found the answer.
I have a Post model that has an image. I'm using the Paperclip gem, saving to Amazon S3, and hosting on Heroku.
The file upload form works fine, because I can see that images are sent to my S3 bucket.
The issue is that, the images don't actually show in production.
Here's my model:
class Post < ActiveRecord::Base
attr_accessor :image_file_name, :image_content_type, :image_file_size, :image_updated_at
belongs_to :user
has_many :reviews
has_attached_file :image, styles: { medium: "700x500#", small: "350x250>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
And here's my config/production.rb:
# Required for Paperclip / AWS
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
And here's my show.html.haml file:
.clearfix
.post_image_description
= image_tag #post.image.url(:medium)
.description= simple_format(#post.address)
.description= simple_format(#post.description)
Shouldn't the #post.image.url be enough? What may I be missing to properly route to the image?
This is what I see when I pull Heroku Logs:
2015-06-23T15:38:26.181383+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/images/medium/missing.png"):
For reference, here's my repository for the project: https://github.com/lucasvocos/pitstop
Please let me know if there is anything else to provide in the question, too. As this is my first time asking. Thanks everyone.
I had a similar problem that the upload was working but the display was showing a broken link. I checked the source for the link and it was not pointing to the url shown for the image on S3. I had to add the host name to my paperclip config
config.paperclip_defaults = {
:storage => :s3,
:s3_host_name => 's3-us-west-2.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']
}
}
You need to set heroku environment variable for s3 bucket variable:
heroku config:set S3_BUCKET_NAME='Your Bucket Name'
heroku config:set AWS_ACCESS_KEY_ID='Your AWS ID'
heroku config:set AWS_SECRET_ACCESS_KEY='Your AWS Secrete Key'
You have to commit the missing.png in \public\images\medium.(make sure \public\images\medium\missing.png exist, then commit). This images is the default until you upload some valid image.
However, also is recommended to define the url in model, something like this:
has_attached_file :image, styles: {
medium: "700x500#",
small: "350x250>" }, :url => '/:class/:attachment/:id/:style_:basename.:extension'

Rails 4 + Paperclip + S3: Changing endpoint to access an S3 bucket in User model?

Should be a pretty simple fix to this question, I think, but I can't seem to get it to work. I have a Rails 4 app, a User model with a photograph attribute setup with Paperclip, and I have it linked to S3.
Here's the User model:
has_attached_file :photograph,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:storage => :s3,
:bucket => " my-bucket-name ",
:default_url => '/images/:attachment/missing_:style.png',
:s3_credentials => S3_CREDENTIALS
The image gets added to my S3 bucket just fine, but when I try to render the picture with <%= image_tag #user.photograph.url %>, it doesn't show up. Upon further inspection, the image URL is:
http://s3.amazonaws.com/my-bucket-name/users/photographs/000/000/001/original/20121103_132556.jpg?1388619625
If I follow this URL in a browser, I see an XML file as follows:
<Error>
<Code>PermanentRedirect</Code>
<Message>
The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
</Message>
<RequestId> ... </RequestId>
<Bucket>my-bucket-name</Bucket>
<HostId>
...
</HostId>
<Endpoint>my-bucket-name.s3.amazonaws.com</Endpoint>
</Error>
Consequently, when I follow the url http://my-bucket-name.s3.amazonaws.com/actives/photographs/000/000/001/original/20121103_132556.jpg?1388619625 in a browser, I see the picture just fine.
How do I change the endpoint in my User model? What is the normal way to handle this? I must add that my S3 bucket is the Northern California region. Thanks.
Solved! Just added these two lines in my User model:
:url =>':s3_domain_url',
:path => '/:class/:attachment/:id_partition/:style/:filename',
Sometimes the problem is that your bucket is not in the us server (amazon's default).
You can change the endpoint using this
:s3_host_name => 'your_host_name'
You can find your hostname in the following link under the endpoint column Amazon endpoints
If you don't know your bucket region, you can find it in bucket properties.

Rails, PaperClip, S3, Heroku: Model icon fields not being saved

I am using Rails 3.2 + Heroku + S3 + Paperclip to store an icon on my User model. The model is not saving the 4 icon fields though. The images are getting processed and saved on S3 correctly and no errors are occurring. I also have another model that has a document being stored via Paperclip and S3. That model works perfectly in all cases. The User icon works locally but not on Heroku.
production.rb relevant configuration
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
User model code:
class User < ActiveRecord::Base
attr_accessible :icon
has_attached_file :icon, :url => "/system/:rails_env/:attachment/:style/:hash.:extension",
:hash_data => ":class/:attachment/:id",
:hash_secret => "superSecretThing",
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "/blank.png"
...
Controller code: (This code is kind of crazy because I am AJAXing files Base64 encoded.)
params[:user][:icon_data]
decoded_file = Base64.decode64(data)
begin
split_name = params[:user][:icon_file_name].split(".")
file = Tempfile.new([split_name[0..-2].join("."), ".#{split_name[-1]}"])
file.binmode
file.write(decoded_file)
file.close
#user.icon = open(file)
#user.icon_file_name = params[:user][:icon_file_name]
ensure
file.unlink
end
#user.save
I do an almost identical process on another model with a Paperclip attachment and it works flawlessly. In both cases the attachment is being saved correctly to S3 and no errors are being raised. This gist has example output for a controller action from the Heroku logs.
I am pretty baffled because the other model works fine. The only real difference is that the User attachment does image processing but that part appears to be working fine.
The problem is the same as this one, but the solution there does not apply.
Thoughts?
So the problem is that not including the :path argument makes it try to use the :url parameter for both the url and the path. The real fix is to include the :path parameter in addition to the url.
So for example a fixed configuration that works both locally and on Heroku:
has_attached_file :icon,
:url => "/system/:rails_env/:attachment/:style/:hash.:extension",
:path => "public/system/:rails_env/:attachment/:style/:hash.:extension",
:hash_data => ":class/:attachment/:id",
:hash_secret => "superDuperSecret",
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "/blank.png"

How to fix content-type for Aurigma uploads to S3?

Our users have two ways of uploading images. One is through a simple HTML form and the other is through an iPhone app called Aurigma. We use Paperclip to process the images and store them on S3. Images that are uploaded with Aurigma end up having the wrong content-type, which causes them to open as an application.
I tried two solutions:
before_save :set_content_type
def set_content_type
self.image.instance_write(:content_type,"image/png")
end
And:
before_post_process :set_content_type
def set_content_type
self.image.instance_write(:content_type, MIME::Types.type_for(self.image_file_name).to_s)
end
It seems as if both solutions are ignored.
Using paperclip version 3.0.2, Aurigma version 1.3 and I'm uploading a screenshot from my iPhone. This is my paperclip configuration:
has_attached_file :image, {
:convert_options => { :all => '-auto-orient' },
:styles => {
:iphone3 => "150x150",
:web => "300x300"
},
:storage => :s3,
:bucket => ENV['S3_BUCKET'],
:s3_credentials => {
:access_key_id => ENV['S3_KEY'],
:secret_access_key => ENV['S3_SECRET']
},
:path => "/pictures/:id/:style.:extension",
:url => "/pictures/:id/:style.:extension"}
}
I just answered a similar question.
You need to do a copy to itself or use a pre-signed url with the content-type specified in the querystring.
Using the AWS SDK for Ruby and url_for:
object = bucket.objects.myobject
url = object.url_for(:read, :response_content_type => "image/png")
As far as I understand first you upload all the files from client devices to your own server (through Aurigma Up) and then these files are uploaded to Amazon S3. I have similar problem trying to change content-type on client device. This is not possible. You should send files on your server and then change content type before uploading files to S3.

Resources