1
votes

I'm using Active Storage with Rails 5.2.

When I create a download link for an image, it generates an URL for the blob that points to the rails application.

Is there any way to create a download link that directory points to a cloud service like S3 without redirecting to the rails application?

2

2 Answers

4
votes

You can get the link of the file by using ActiveStorage service_url method like this:

Model.last.attachment.service_url

It will return the service link for your attachment. You can read more about service_url here

1
votes

service_url method is the correct way but keep in mind that generated url will expire in time. Also generated urls in that way can be slower because of redirects (Active Storage generates URLs which point back to the Rails application. When accessed, these URLs redirect to the actual service endpoint for the file).

If you want to generate permanent direct url to attachement, Active Storage is no go for now without some hackery.

There are couple of proposals to extend this feature in Active Storage: https://github.com/rails/rails/issues/34552#issuecomment-469530719