Active Storage 服务¶ ↑
作为具体服务接口的抽象类。
可用的服务有:
- 
Disk,用于管理直接保存在硬盘上的附件。
- 
GCS,用于通过 Google Cloud Storage 管理附件。
- 
S3,用于通过 Amazon S3 管理附件。
- 
Mirror,用于同时使用多个服务来管理附件。
在 Rails 应用程序中,您可以通过生成的 config/storage.yml 文件来设置您的服务,并在 service 键下引用上述常量之一。例如:
local:
  service: Disk
  root: <%= Rails.root.join("storage") %>
您可以查看服务的构造函数以了解所需键。
然后,在您的应用程序配置中,您可以像这样指定要使用的服务:
config.active_storage.service = :local
如果您在 Ruby on Rails 应用程序之外使用 Active Storage,您可以像这样配置要使用的服务:
ActiveStorage::Blob.service = ActiveStorage::Service.configure( :local, { local: {service: "Disk", root: Pathname("/tmp/foo/storage") } } )
- 类 ActiveStorage::Service::DiskService
- 类 ActiveStorage::Service::GCSService
- 类 ActiveStorage::Service::MirrorService
- 类 ActiveStorage::Service::S3Service
- C
- D
- E
- H
- O
- P
- U
Attributes
| [RW] | name | 
类公共方法
configure(service_name, configurations) 链接
根据一组配置(通常从 YAML 文件加载)按名称配置 Active Storage 服务。Active Storage 引擎在应用程序启动时使用此方法设置全局 Active Storage 服务。
来源: 显示 | 在 GitHub 上
# File activestorage/lib/active_storage/service.rb, line 52 def configure(service_name, configurations) Configurator.build(service_name, configurations) end
实例公共方法
compose(source_keys, destination_key, filename: nil, content_type: nil, disposition: nil, custom_metadata: {}) 链接
将多个文件连接成一个“组合”文件。
来源: 显示 | 在 GitHub 上
# File activestorage/lib/active_storage/service.rb, line 96 def compose(source_keys, destination_key, filename: nil, content_type: nil, disposition: nil, custom_metadata: {}) raise NotImplementedError end
delete(key) 链接
删除 key 处的文件。
来源: 显示 | 在 GitHub 上
# File activestorage/lib/active_storage/service.rb, line 101 def delete(key) raise NotImplementedError end
delete_prefixed(prefix) 链接
删除以 prefix 开头的键的文件。
来源: 显示 | 在 GitHub 上
# File activestorage/lib/active_storage/service.rb, line 106 def delete_prefixed(prefix) raise NotImplementedError end
download(key) 链接
返回 key 处文件的内容。
来源: 显示 | 在 GitHub 上
# File activestorage/lib/active_storage/service.rb, line 82 def download(key) raise NotImplementedError end
download_chunk(key, range) 链接
返回 key 处文件 range(字节范围)的部分内容。
来源: 显示 | 在 GitHub 上
# File activestorage/lib/active_storage/service.rb, line 87 def download_chunk(key, range) raise NotImplementedError end
exist?(key) 链接
如果 key 处存在文件,则返回 true。
来源: 显示 | 在 GitHub 上
# File activestorage/lib/active_storage/service.rb, line 111 def exist?(key) raise NotImplementedError end
headers_for_direct_upload(key, filename:, content_type:, content_length:, checksum:, custom_metadata: {}) 链接
返回用于 url_for_direct_upload 请求的 Hash 头部信息。
来源: 显示 | 在 GitHub 上
# File activestorage/lib/active_storage/service.rb, line 143 def headers_for_direct_upload(key, filename:, content_type:, content_length:, checksum:, custom_metadata: {}) {} end
open(*args, **options, &block) 链接
来源: 显示 | 在 GitHub 上
# File activestorage/lib/active_storage/service.rb, line 91 def open(*args, **options, &block) ActiveStorage::Downloader.new(self).open(*args, **options, &block) end
public?() 链接
来源: 显示 | 在 GitHub 上
# File activestorage/lib/active_storage/service.rb, line 147 def public? @public end
update_metadata(key, **metadata) 链接
更新服务中由 key 标识的文件的元数据。仅当服务需要存储在识别后需要更新的特定元数据时,才在子类中覆盖此方法。
来源: 显示 | 在 GitHub 上
# File activestorage/lib/active_storage/service.rb, line 78 def update_metadata(key, **metadata) end
upload(key, io, checksum: nil, **options) 链接
将 io 上传到指定的 key。如果提供了 checksum,服务将在上传完成后确保匹配,否则将引发 ActiveStorage::IntegrityError。
来源: 显示 | 在 GitHub 上
# File activestorage/lib/active_storage/service.rb, line 71 def upload(key, io, checksum: nil, **options) raise NotImplementedError end
url(key, **options) 链接
返回 key 处文件的 URL。对于公共文件,这将返回一个永久 URL,对于私有文件,则返回一个短暂 URL。对于私有文件,您可以提供希望文件在请求时使用的 disposition(:inline 或 :attachment)、filename 和 content_type。此外,您还可以提供 URL 的有效秒数,在 expires_in 中指定。
来源: 显示 | 在 GitHub 上
# File activestorage/lib/active_storage/service.rb, line 119 def url(key, **options) instrument :url, key: key do |payload| generated_url = if public? public_url(key, **options) else private_url(key, **options) end payload[:url] = generated_url generated_url end end
url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {}) 链接
返回一个已签名、临时的 URL,可以直接上传的文件可以通过 PUT 请求将内容上传到此 URL(位于 key 处)。该 URL 在 expires_in 指定的秒数内有效。您还必须提供要上传的文件的 content_type、content_length 和 checksum。在上传时,服务将验证所有这些属性。
来源: 显示 | 在 GitHub 上
# File activestorage/lib/active_storage/service.rb, line 138 def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {}) raise NotImplementedError end