【问题标题】:ActionText image not stored in Azure (or any other service)ActionText 图像未存储在 Azure(或任何其他服务)中
【发布时间】:2019-12-07 11:23:51
【问题描述】:

我有一个带有 ActionText 和 ActiveStorage 的 Rails 应用程序。所以我有一个带有描述字段的工单表单,其中使用了 ActionText。当我将开发环境配置为将附件保存到本地磁盘时,一切正常。

但是当我将它配置为使用 Azure 存储时,我在日志中看到它似乎已经上传(它返回一个 URL),但是当我在 Azure 中查看时,没有存储图像。这是日志:

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)

这是config/development.rb:

config.active_storage.service = :local

这是storage.yml:

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

并且使用了正确的宝石:

gem 'azure-storage', require: false

那么有什么问题吗?我对数字海洋空间进行了同样的尝试,但结果完全相同,因此它与 Azure 无关。

【问题讨论】:

    标签: ruby-on-rails rails-activestorage actiontext


    【解决方案1】:

    就我个人而言,我从未使用过 AzureStorage,但我使用过 DigitalOcean Spaces。为了通过 ActiveStorage 启用直接上传,您需要确保设置的一件事是 CORS。我可能是错的,但我认为这可能是你的问题。

    我在 Google 上搜索了“AzureStorage CORS”,并在他们的 CORS 设置中找到了 MicroSoft's documentation

    使用 Digital Ocean 时,您需要使用 their documentation to setup s3cmd 并使用它来设置 CORS 以进行直接上传。

    设置完成后,您将在项目中创建一个 CORS 文件 (cors.xml),如下所示:

    <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>
    

    然后,您将使用 s3cmd 工具通过 Digital Ocean 设置它,方法是在您的项目目录中运行以下命令:

    s3cmd setcors cors.xml s3://yourbucketname
    

    GoRails 有一个 great video 告诉你如何做到这一点。

    Digital Ocean 也有 CORS user interface,但这通常不足以满足我的需求。

    【讨论】:

    • 啊啊CORS,完全忘记了!谢谢,我会调查的。
    猜你喜欢
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 2015-03-31
    • 2021-10-12
    • 1970-01-01
    • 2016-11-25
    相关资源
    最近更新 更多