Tom
Tom

Reputation: 173

Active Storage Permanent Image URL

I'm creating a google product listing for a website built in Ruby on Rails. The website has multiple stores with their own front ends so they're wanting a Google Product Feed for each store.

The issue I'm having is if I use url_for(image) then I get the URL for the image on that store. But the way active storage works it's on a temporary URL that lasts 5 minutes by default. So the links wouldn't work on the feed by the it's been processed.

The images are hosted on an S3 bucket so I can get the service_url. But Google doesn't like having the images coming from a separate domain to the store. Is there a way to have a permanent clean url from the stores domain?

Upvotes: 10

Views: 8942

Answers (3)

mehedi
mehedi

Reputation: 466

Try rails_blob_url

rails_blob_url(image, disposition: "attachment")

Update: Link updated.

Upvotes: 2

pastullo
pastullo

Reputation: 4201

You can get the full URL of an Active Storage attachment using the following:

polymorphic_url(@article.image.variant(resize: '800x600'))

This will provide a full url and not just the path and works well on Rails 5.2+

Upvotes: 1

andrewhaines
andrewhaines

Reputation: 186

I think what you're after isn't easily possible. Active Storage doesn't seem to support permanent, non-expiring URLs: "Request has expired" when using S3 with Active Storage

Depending on your setup, there might be a useful and (mostly) hack-free workaround. In my case, I've set a custom show action on the record that owns the file I want to link to:

redirect_to url_for(@record_name.file)

Then, using a path helper for the record show action in my app, as usual, just renders the thing I want via the expiring url_for.

Upvotes: 5

Related Questions