Active Storage,指定一个谷歌存储桶目录?
Posted
技术标签:
【中文标题】Active Storage,指定一个谷歌存储桶目录?【英文标题】:Active Storage, specify a Google bucket directory ? 【发布时间】:2018-11-08 15:28:21 【问题描述】:我一直在 Rails 5.2 上实施 Active Storage Google 策略,目前我可以毫无问题地使用 rails 控制台上传文件,我唯一缺少的是是否有办法在其中指定目录一个桶。现在我上传如下
bk.file.attach(io: File.open(bk.source_dir.to_s), filename: "file.tar.gz", content_type: "application/x-tar")
我的 storage.yml 上的配置
google:
service: GCS
project: my-project
credentials: <%= Rails.root.join("config/myfile.json") %>
bucket: bucketname
但在我的存储桶中有不同的目录,例如存储桶名称/部门 1 等。我浏览了文档,但没有找到指定更多目录的方法,我所有的上传都以存储桶名称结尾。
【问题讨论】:
@George Claghorn 我会接受你的回答,希望这可以在未来的版本中得到解决。谢谢。 【参考方案1】:抱歉,恐怕 Active Storage 不支持。您打算为 Active Storage 配置一个可以独占使用的存储桶。
【讨论】:
在另一个 SO 线程中有一个建议的 hack,围绕覆盖 move_to 方法可能会有所帮助:***.com/questions/48389782/…【参考方案2】:也许你可以尝试元编程,像这样:
-
创建 config/initializers/active_storage_service.rb 以将 set_bucket 方法添加到 ActiveStorage::Service
module Methods
def set_bucket(bucket_name)
# update config bucket
config[:bucket] = bucket_name
# update current bucket
@bucket = client.bucket(bucket_name, skip_lookup: true)
end
end
ActiveStorage::Service.class_eval include Methods
-
在上传或下载文件之前更新您的存储桶
ActiveStorage::Blob.service.set_bucket "my_bucket_name"
bk.file.attach(io: File.open(bk.source_dir.to_s), filename: "file.tar.gz", content_type: "application/x-tar")
【讨论】:
您似乎包含了指向您自己的文章或您所属的文章的链接。如果是这种情况,you must disclose your affiliation in the answer itself. @Hoppeduppeanut 谢谢,也许我会删除链接 不需要删除它,您只需要声明您已经撰写了这篇文章,并确保它与您提出的问题以及您在回答中提供的信息相关。以上是关于Active Storage,指定一个谷歌存储桶目录?的主要内容,如果未能解决你的问题,请参考以下文章
Rails Active Storage 设置文件夹来存储文件
如何使用 Active Storage Rails 上传到具有 AES256 加密(服务器端加密)的 AWS S3 存储桶?
将 Paperclip 数据库迁移到 Active Storage Amazon S3
如何使用 Rails 和 Active Storage 实现 AWS S3 分段上传?
无法加载 Rails.config.active_storage.service
Heroku:在 /app/config/storage.yml 中找不到 Active Storage 配置(RuntimeError)