[GO] Prepare a URL to display the image uploaded by Active Storage by yourself

What I wanted to do

I wanted to save the HTML including the image URL as it is in the DB and display it in raw with an editor such as WYSIWY.

problem

If you save the service_url directly, the access token will expire and you will get the following error:

https://storage.googleapis.com/xxx/6qk30zadasnfkansfalksj34gq?GoogleAccessId=...

<Error>
  <Code>ExpiredToken</Code>
  <Message>The provided token has expired.</Message>
  <Details>Request signature expired at: 2020-09-30T19:07:20+00:00</Details>
</Error>

スクリーンショット 2020-10-05 2.29.58.png

Prepare the URL for generation

Create a model for assets to prepare a URL to generate and display a service_url with an access token.

asset.rb


class Asset < ApplicationRecord
  has_one_attached :image
end

Changed to send image data with show of controller.

assets_controller.rb


class AssetsController < ActionController::Base
  # GET /assets:id
  def show
    asset = Asset.find(params[:id])
    send_data asset.image.download, filename: asset.image.filename.to_s, content_type: asset.image.content_type
  end
end

By saving the following URL in the DB, it was possible to display the image via the application server without expiring.

...
<img src="http://localhost:3000/assets/2" alt="Image">
...

Recommended Posts

Prepare a URL to display the image uploaded by Active Storage by yourself
Mouse over Matplotlib to display the corresponding image
Upload the image downloaded by requests directly to S3
Save the graph drawn by pyqtgraph to an image
Python OpenCV tried to display the image in text.
[python] Change the image file name to a serial number
Convert the image data (png) at hand to a .pbm image
Dot according to the image
The image is a slug
Do a search by image from the camera roll using Pythonista3
Display the image of the camera connected to the personal computer on the GUI.
Visualize by adding "a bite" to the "boxplot" (boxen / swarm / violin)
[Cloudian # 2] Try to display the object storage bucket in Python (boto3)
Try to edit a new image using the trained StyleGAN2 model
[Go] Create a CLI command to change the extension of the image
Raspberry Pi --1 --First time (Connect a temperature sensor to display the temperature)
I tried to cut out a still image from the video
I made a command to display a colorful calendar in the terminal
Prepare a high-speed analysis environment by hitting mysql from the data analysis environment
I want to be healed by Mia Nanasawa's image. In such a case, hit the Twitter API ♪