【问题标题】:Cannot change Shrine Amazon S3 prefix in Rails app无法在 Rails 应用程序中更改 Shrine Amazon S3 前缀
【发布时间】:2021-05-25 23:58:26
【问题描述】:

我正在使用 Shrine 将图像直接上传到我的应用程序中的 Amazon S3 存储。它适用于默认设置。但是当我尝试更改上传图片的前缀时,前缀并没有改变。当然,当我尝试通过浏览器查看图像时,我得到了这个错误:

<Error>
<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>
<Key>store/cccd4928ef62740b851f7c278a3907ef.png</Key>
....
</Error>

这是我在 config/initializers/shrine.rb 中的当前设置:

require "shrine/storage/s3"
 
s3_options = {
    access_key_id: Rails.application.secrets.access_key_id,
    secret_access_key: Rails.application.secrets.secret_access_key,
    region: Rails.application.secrets.aws_region,  
    bucket: Rails.application.secrets.aws_bucket,  
}

if Rails.env.development?
    prefix = "dev"
else
    prefix = "prod"
end 

Shrine.storages = {
  cache: Shrine::Storage::S3.new(prefix: prefix, **s3_options), #default prefix is "cache"
  store: Shrine::Storage::S3.new(prefix: prefix, **s3_options),   #default prefix is "store"
}

Shrine.plugin :activerecord
Shrine.plugin :upload_endpoint
Shrine.plugin :restore_cached_data

【问题讨论】:

    标签: ruby-on-rails ruby amazon-web-services amazon-s3 ruby-on-rails-5


    【解决方案1】:

    解决了。

    基本上在我引用 Shrine ImageUploader 类的模型中,我需要使用include ImageUploader::Attachment(:image) 而不是include ImageUploader[:image],因为我使用的是Shrine.plugin :activerecord

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 2015-09-10
      • 1970-01-01
      相关资源
      最近更新 更多