rails api with active storage returns a link for an uploaded to s3 service file to a non existing page

51 Views Asked by At

My rails API uses active storage to upload a given user avatar image to s3 service

storage.yml

amazon:
  service: S3
  access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
  secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
  region: eu-central-1
  bucket: my-bucket-production

It exposes the image in users_controller.rb:

def show
   render json: user_info, status: :ok
 end
 def user_info
   c = @user.as_json(only: %i[id email role username])
   c.merge!(avatar_path: url_for(@user.avatar)) if @user.avatar.attached?
   c
 end

The front-end built with quasar framework (vue3) when calling via axios it gets a link like

https://api.sometestpage.com/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--3ba88d18cd3d08bd920414a885e532ecbbdeca5f/avatar.jpg

witch leads to a 404 error.

Can anybody point out what is wrong, please?

My code (backend and frontend) works fine locally in dev mode (storing to disk, not s3). The problem occurs only when I upload my code to my server and try to run it in production. I tried to use @user.avatar.service_url instead of url_for(@user.avatar) but I get an undefined method error.

Rails version is 7.0.8

0

There are 0 best solutions below