I have to migrate assets(images) from Rails ActiveStorage uploaded file to S3 into my another WordPress website.
However, it seems the filename is they Blob key similar to as below:
5jEWBoezkaNyJtQA1icpnuSs
Is there any way to retrieve the actual file name like “docker-life-cycle” out of above key name?
Note: Someone mentioned a way in this question (see comment) Active Storage with Amazon S3 not saving with filename specified but using file key instead
1
If you have something like the following:
class Asset
has_one_attached :image
end
Then you’d be able to access the filename of an assets image as follows:
asset = Asset.last
asset.image.filename # returns an ActiveStorage::Filename object
asset.image.filename.to_s # i.e. image.png etc
Bear in mind that if asset doesn’t have an image set, then filename
will return nil, and and filename.to_s
will return ""
, so you’ll want to handle that accordingly.
If you mean you only have the blob key and want to work from there to find the filename, it’d be something like this:
blob = ActiveStorage::Blob.find_by(key: 'my_key')
blob.filename # returns an ActiveStorage::Filename object
blob.filename.to_s # again, filename as a string, i.e. image.png