Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Storage with Amazon S3 not saving with filename specified but using file key instead

I am having an issue with Active Storage. When I upload to Amazon S3, instead of saving the file inside the bucket with the original name like myfile.zip it is saving it as the key which is associated with that file. So in Cyberduck I am seeing something like this: 5YE1aJQuFYyWNr6BSHxhQ48t. Without any file extension.

I am not sure if there is some setting in Rails 5 or whether it is within Amazon S3 but I have spent hours Googling around to figure out why this is happening.

Any pointers would be really appreciated!

Best regards, Andrew

like image 322
Drew Potter Avatar asked Jun 01 '18 09:06

Drew Potter


2 Answers

This is by design, from ActiveStorage. The file is stored by it's key and without extension on S3, but when the URL is generated by ActiveStorage, the disposition and filename are set.

def url(key, expires_in:, filename:, disposition:, content_type:)
  instrument :url, key: key do |payload|
    generated_url = object_for(key).presigned_url :get, expires_in: expires_in.to_i,
      response_content_disposition: content_disposition_with(type: disposition, filename: filename),
      response_content_type: content_type

    payload[:url] = generated_url

    generated_url
  end

end

This is probably done to avoid filename escaping issues that you'd run into otherwise.

You can read more about the Content-Disposition headers here.

like image 192
Daniel Westendorf Avatar answered Oct 19 '22 20:10

Daniel Westendorf


You can still reference the name with filename accessor.

class User < ApplicationRecord
  has_one_attached :photo
  ...
end

filename = User.first.photo.filename
like image 27
Arvin Avatar answered Oct 19 '22 22:10

Arvin