我可以将 Shrine 上传凭据添加到模型中。

huangapple go评论73阅读模式
英文:

Can I add Shrine upload credentials to the model

问题

我在Rails 5上构建了一个多租户网站,每个租户都会添加他们自己的S3凭据,因此,发生在他们租户网站上的任何上传都会上传到他们自己的S3帐户。

我目前遇到的问题是,Shrine似乎只允许我在初始化器中添加S3凭据。这很好用,但我想将它添加到模型中,以便我可以根据当前使用的租户动态填充S3凭据。有人知道Shrine是否可以帮助我吗?

我设法在paperclip中实现了这一点,但它带来了其他问题,比如后台处理等。

英文:

I have a multi-tenant site built on rails 5, each of the tenants adds their own s3 credentials, therefore, any uploads that happen on their tenant site get uploaded to their own s3 account.

The problem I have at the moment is that Shrine seems to only let me add s3 credentials in the initializer. This works great but I would like to add it to the model so that I can dynamically populate the s3 credentials depending on which tenant is being used at the time. Does anyone know anyway shrine can help me?

I managed to do this with paperclip but it came with other problems such as background processing etc.

答案1

得分: 1

你可以在初始化器中定义所有的存储:

Shrine.storages = {
  first_storage: Shrine::Storage::S3.new(
    bucket: "my-first-bucket", # 必填项 
    region: "eu-west-1", # 必填项 
    access_key_id: "abc",
    secret_access_key: "xyz"),

  second_storage: Shrine::Storage::S3.new(
    bucket: "my-second-bucket", # 必填项 
    region: "eu-east-1", # 必填项 
    access_key_id: "efg",
    secret_access_key: "uvw")
}

注意:这并不是所有存储的代码 - :cache:store 存储也应该被定义。

然后在模型中使用它们:

class Photo
  include ImageUploader::Attachment(:image)
end

photo = Photo.new
photo.image_attacher.upload(io, :first_storage)
photo.image_attacher.upload(other_io, :second_storage)

查看 Shrine attacher 的 文档页面源代码

英文:

You could define all the storages in the initializer:

Shrine.storages = {
  first_storage: Shrine::Storage::S3.new(
  bucket: "my-first-bucket", # required 
  region: "eu-west-1", # required 
  access_key_id: "abc",
  secret_access_key: "xyz"),

  second_storage: Shrine::Storage::S3.new(
  bucket: "my-second-bucket", # required 
  region: "eu-east-1", # required 
  access_key_id: "efg",
  secret_access_key: "uvw")
}

Note: This is not all the storages code - both the :cache and the :store storages should be defined.

And then use them in the models:

class Photo
  include ImageUploader::Attachment(:image)
end

photo = Photo.new
photo.image_attacher.upload(io, :first_storage)
photo.image_attacher.upload(other_io, :second_storage)

See Shrine attacher's doc page and source code

huangapple
  • 本文由 发表于 2020年1月3日 16:05:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定