Active Storage 服务
充当具体服务的接口的抽象类。
可用服务包括
-
Disk
,用于管理直接保存在硬盘上的附件。 -
GCS
,用于通过 Google Cloud Storage 管理附件。 -
S3
,用于通过 Amazon S3 管理附件。 -
AzureStorage
,用于通过 Microsoft Azure Storage 管理附件。 -
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::AzureStorageService
- 类 ActiveStorage::Service::DiskService
- 类 ActiveStorage::Service::GCSService
- 类 ActiveStorage::Service::MirrorService
- 类 ActiveStorage::Service::S3Service
- C
- D
- E
- H
- O
- P
- U
属性
[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 到 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