1

I have a Rails app with ActionText and ActiveStorage. So I have a ticket form with a description field, where ActionText is used. When I have the development environment configured to save the attachments to the local disk, everything works fine.

But when I configure it to use Azure storage, I see in the log that it seems to have uploaded (it returns an URL), but when I look in Azure, there is no stored image. This is the log:

Started POST "/rails/active_storage/direct_uploads" for ::1 at 2019-07-30 08:05:37 +0200
Processing by ActiveStorage::DirectUploadsController#create as JSON
  Parameters: {"blob"=>{"filename"=>"schaap.jpg", "content_type"=>"image/jpeg", "byte_size"=>56679, "checksum"=>"xNr4chw64aFTDzzvpmupBg=="}, "direct_upload"=>{"blob"=>{"filename"=>"schaap.jpg", "content_type"=>"image/jpeg", "byte_size"=>56679, "checksum"=>"xNr4chw64aFTDzzvpmupBg=="}}}
   (0.2ms)  BEGIN
  ActiveStorage::Blob Create (0.8ms)  INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["key", "p83x87l66oibofrfmitfiqet120d"], ["filename", "schaap.jpg"], ["content_type", "image/jpeg"], ["byte_size", 56679], ["checksum", "xNr4chw64aFTDzzvpmupBg=="], ["created_at", "2019-07-30 06:05:38.851532"]]
   (0.6ms)  COMMIT
  AzureStorage Storage (0.9ms) Generated URL for file at key: p83x87l66oibofrfmitfiqet120d (https://<blob name>.blob.core.windows.net/<container-name>/p83x87l66oibofrfmitfiqet120d?sp=rw&sv=2016-05-31&se=2019-07-30T06%3A10%3A38Z&sr=b&sig=%2BicDHTsLBXWCIr00m4cbmcg3U6il5LMfhVcKwTq8dns%3D)
Completed 200 OK in 867ms (Views: 0.4ms | ActiveRecord: 5.7ms | Allocations: 70289)

This is in config/development.rb:

config.active_storage.service = :local

And this is in storage.yml:

local:
  service: AzureStorage
  storage_account_name: "<account-name>"
  storage_access_key: "<access-key>"
  container: "<container>"

And the proper gem is used:

gem 'azure-storage', require: false

So what is wrong? I tried the same with Digital Ocean spaces, but with the exact same result, so it is not Azure related.

John
  • 6,404
  • 14
  • 54
  • 106

1 Answers1

2

Personally, I've never used AzureStorage but I have used DigitalOcean Spaces. One thing you need to make sure you setup in order to enable direct uploads via ActiveStorage is CORS. I could be wrong but I think this may be the problem in your case.

I Googled "AzureStorage CORS" and found MicroSoft's documentation on their CORS setup.

When using Digital Ocean, you'll want to use their documentation to setup s3cmd and use that to setup CORS for direct uploads.

After you set it up, you would create a CORS file (cors.xml) like so in your project:

<CORSConfiguration>
  <CORSRule>
    <AllowedOrigin>http://localhost:3000</AllowedOrigin>
    <AllowedOrigin>https://yourdomain.com</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
  </CORSRule>
</CORSConfiguration>

Then, you would use the s3cmd tool to set it up with Digital Ocean, by running the following in your project directory:

s3cmd setcors cors.xml s3://yourbucketname

GoRails has a great video on how to do this.

Digital Ocean also has a CORS user interface but that doesn't normally do enough for my needs.

John
  • 374
  • 2
  • 6