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
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.
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.In order to have a custom filename on S3, you should update both
blob.key
and the name on S3.Active storage uploads images on S3 using
blob.key
as remote image path and name.For my usage, I only changed the name for 'images variants' with a Monkey Patch that allows to generate a
key
terminating by thefilename
:config/initializers/active_storate_variant.rb
:So when I need the public url for an image variant, I just call
image.url('400x400')
This is how my Image model is customized :
If someone has a better way to do that, I would be happy to see it :)
You can still reference the name with filename accessor.