Unable to set Host in Shrine::Storage::FileSystem

Brief Description

We have two environments:

  • demo that uses storage/file_system
  • production that uses storage/s3

And so, we have single code user.avatar_url in both environments of our API that responds with different format of url.

Expected behavior

  • production S3:
    https://s3.eu-central-1.amazonaws.com/example/thumbs/6819bbcb4681118e77961246dd197237.jpg

  • demo file system:
    https://<demo-host>/uploads/thumbs/6819bbcb4681118e77961246dd197237.jpg

Actual behavior

  • production S3:
    https://s3.eu-central-1.amazonaws.com/example/thumbs/6819bbcb4681118e77961246dd197237.jpg

  • demo file system:
    /uploads/thumbs/6819bbcb4681118e77961246dd197237.jpg

There is also parameter that should help us but actually can broke the production because of S3 usage:
user.avatar_url(host: Rails.application.routes.default_url_options[:host]) and need to be passed in all places we are use avatar_url what is really looks sad…

Proposal

Need to add attr host into Shrine::Storage::FileSystem that will be used before prefix and return full form of url if we need it.

Could you post a self-contained script that reproduces the problem? You can use a template from here.

Ofc, https://github.com/elhowm/shrine-bug-reproduce

You’re not passing the :host option to file URLs (only to default_url plugin, which is only applied to the default URL). How is it “unexpected” that file URLs don’t have the URL host, if you’ve not told Shrine to use one?

You probably want to use the url_options plugin to pass the :host option to file URLs:

Shrine.plugin :url_options,
  cache: { host: "https://example.com" },
  thumbs: { host: "https://example.com" },
  store: { host: "https://example.com" }

Sorry, I tried to provide the most suitable topic. And it says that actually API goes in unexpected way and I wanted to ask how I can handle it. Your answer should work for me and I thank you for that. But why I was not able to find it in the docs?

Screenshot%20from%202020-10-26%2023-04-35

The most near thing was there https://shrinerb.com/docs/storage/file-system#host. And it says to use #url with host option in all places.

The actual bug was in the point that I didn’t have the way to “tell” shrine local storage what is my host :slight_smile: