Rails Active Storage:直接从模型上传
Rails Active Storage: Direct upload from model
我正在为我的网站创建站点地图,由于 Heroku 临时文件系统,我需要将其上传到 S3。
我已经使用 Amazon S3 配置了 Active Storage,但我不想仅为单个上传创建模型实体。有没有办法:
sitemap.rb
- 使用 Active Storage
将生成的文件 "public/sitemaps/sitemap.xml.gz" 上传到 S3
- 检索 URL
错误的工具。
Active Storage facilitates uploading files to a cloud storage service
like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage and
attaching those files to Active Record objects.
ActiveStorage 是围绕将文件附加到模型而构建的。尝试在没有模型的情况下使用它会非常痛苦,因为您正在对付 ActiveStorage 的整个设计。
如果您只想上传在服务器上创建的文件,请改用 aws-sdk-ruby gem。
require 'aws-sdk-s3'
s3 = Aws::S3::Resource.new(region:'us-west-2')
obj = s3.bucket('bucket-name').object('key')
obj.upload_file('/path/to/source/file', { acl: 'public-read' })
# Returns Public URL to the file
obj.public_url
我正在为我的网站创建站点地图,由于 Heroku 临时文件系统,我需要将其上传到 S3。
我已经使用 Amazon S3 配置了 Active Storage,但我不想仅为单个上传创建模型实体。有没有办法:
sitemap.rb
- 使用 Active Storage 将生成的文件 "public/sitemaps/sitemap.xml.gz" 上传到 S3
- 检索 URL
错误的工具。
Active Storage facilitates uploading files to a cloud storage service like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage and attaching those files to Active Record objects.
ActiveStorage 是围绕将文件附加到模型而构建的。尝试在没有模型的情况下使用它会非常痛苦,因为您正在对付 ActiveStorage 的整个设计。
如果您只想上传在服务器上创建的文件,请改用 aws-sdk-ruby gem。
require 'aws-sdk-s3'
s3 = Aws::S3::Resource.new(region:'us-west-2')
obj = s3.bucket('bucket-name').object('key')
obj.upload_file('/path/to/source/file', { acl: 'public-read' })
# Returns Public URL to the file
obj.public_url