Active Storage 服務
作為具體服務介面的抽象類別。
可用的服務如下:
-
Disk
,用於管理直接儲存在硬碟上的附件。 -
GCS
,用於透過 Google Cloud Storage 管理附件。 -
S3
,用於透過 Amazon S3 管理附件。 -
AzureStorage
,用於透過 Microsoft Azure 儲存體管理附件。 -
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::服務::AzureStorage服務
- 類別 ActiveStorage::服務::Disk服務
- 類別 ActiveStorage::服務::GCS服務
- 類別 ActiveStorage::服務::Mirror服務
- 類別 ActiveStorage::服務::S3服務
- C
- D
- E
- H
- O
- P
- U
屬性
[讀寫] | 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
請求的標頭 雜湊
。
來源:顯示 | 在 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
處檔案的網址。這會傳回公開檔案的永久網址,並傳回私有檔案的短期網址。對於私有檔案,您可以提供您希望檔案在請求時提供的 disposition
(:inline
或 :attachment
)、filename
和 content_type
。此外,您還可以提供網址的有效秒數,在 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: {}) 連結
傳回一個已簽名的臨時網址,可以直接上傳檔案到 key
。網址將在 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