Active_storage disk blob url returns 404 - not found. What am I getting wrong?

Hi, ActiveStorage::DiskController is returning 404 for an url of an ActiveStorage::Blob

This is the code

  before_action do
    ActiveStorage::Current.url_options = {
      protocol: "http",
      host: "localhost",
      port: "3030"
    }
  end

  def show
    blob = ActiveStorage::Blob.find_by(filename: params[:filename])
    redirect_to blob.url
  end

This does

Redirected to http://localhost:3030/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDVG9JYTJWNVNTSWhibkpsWldOc2JXTXdPV294ZURSdGRHUXhiV1UzTW5NeWFuSm5aQVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpYzJsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SWpnNU1qRmxPR1ZqTnpZNE9UVTFNV0pqT1daaFptTmhNR1F3Tm1Rek9UQTVMbkJ1WnlJN0lHWnBiR1Z1WVcxbEtqMVZWRVl0T0Njbk9Ea3lNV1U0WldNM05qZzVOVFV4WW1NNVptRm1ZMkV3WkRBMlpETTVNRGt1Y0c1bkJqc0dWRG9SWTI5dWRHVnVkRjkwZVhCbFNTSU9hVzFoWjJVdmNHNW5CanNHVkRvUmMyVnlkbWxqWlY5dVlXMWxPZ3BzYjJOaGJBPT0iLCJleHAiOiIyMDIyLTEyLTE2VDA3OjA0OjQwLjEwMFoiLCJwdXIiOiJibG9iX2tleSJ9fQ==--b1c5f0a8988f772b945867eb97126eb0f133f41e/8921e8ec7689551bc9fafca0d06d3909.png
Completed 302 Found in 38ms (ActiveRecord: 13.6ms | Allocations: 13784)


Started GET "/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDVG9JYTJWNVNTSWhibkpsWldOc2JXTXdPV294ZURSdGRHUXhiV1UzTW5NeWFuSm5aQVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpYzJsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SWpnNU1qRmxPR1ZqTnpZNE9UVTFNV0pqT1daaFptTmhNR1F3Tm1Rek9UQTVMbkJ1WnlJN0lHWnBiR1Z1WVcxbEtqMVZWRVl0T0Njbk9Ea3lNV1U0WldNM05qZzVOVFV4WW1NNVptRm1ZMkV3WkRBMlpETTVNRGt1Y0c1bkJqc0dWRG9SWTI5dWRHVnVkRjkwZVhCbFNTSU9hVzFoWjJVdmNHNW5CanNHVkRvUmMyVnlkbWxqWlY5dVlXMWxPZ3BzYjJOaGJBPT0iLCJleHAiOiIyMDIyLTEyLTE2VDA3OjA0OjQwLjEwMFoiLCJwdXIiOiJibG9iX2tleSJ9fQ==--b1c5f0a8988f772b945867eb97126eb0f133f41e/8921e8ec7689551bc9fafca0d06d3909.png" for ::1 at 2022-12-16 08:59:40 +0200
Processing by ActiveStorage::DiskController#show as PNG
  Parameters: {"encoded_key"=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDVG9JYTJWNVNTSWhibkpsWldOc2JXTXdPV294ZURSdGRHUXhiV1UzTW5NeWFuSm5aQVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpYzJsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SWpnNU1qRmxPR1ZqTnpZNE9UVTFNV0pqT1daaFptTmhNR1F3Tm1Rek9UQTVMbkJ1WnlJN0lHWnBiR1Z1WVcxbEtqMVZWRVl0T0Njbk9Ea3lNV1U0WldNM05qZzVOVFV4WW1NNVptRm1ZMkV3WkRBMlpETTVNRGt1Y0c1bkJqc0dWRG9SWTI5dWRHVnVkRjkwZVhCbFNTSU9hVzFoWjJVdmNHNW5CanNHVkRvUmMyVnlkbWxqWlY5dVlXMWxPZ3BzYjJOaGJBPT0iLCJleHAiOiIyMDIyLTEyLTE2VDA3OjA0OjQwLjEwMFoiLCJwdXIiOiJibG9iX2tleSJ9fQ==--b1c5f0a8988f772b945867eb97126eb0f133f41e", "filename"=>"[FILTERED]"}
Completed 404 Not Found in 0ms (ActiveRecord: 0.0ms | Allocations: 573)

Why is ActiveStorage::DiskController returning non found?

Thanks

Update: It is possible that these records were not created correctly. There seems to be an error when creating them

ActiveStorage::IntegrityError (ActiveStorage::IntegrityError):
  
app/controllers/scratch/assets_controller.rb:26:in `create'

This was difficult. Turns out the client was not sending a ContentType. Because the ContentType was empty request.body was reading an empty string even thought there was a binary body.

Because request.body was empty ActiveStorage::Blob.create_and_upload!(io: request.body,filename: filename,content_type: params[:format]) will creating the Blob but will not store the file on the disk.

Because of then when we call the rails_blob_path url for the blob it will return a blob path and it will redirect to it. The blob exists in the DB even though the file is not on the disk.

Because of this there is a 404.

Fixed it by sending an “application/octet-stream” with the post request and from there the objects are correctly stored.