跳至內容 跳至搜尋

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") } }
)
命名空間
方法
C
D
E
H
O
P
U

屬性

[讀寫] name (名稱)

類別公開方法

configure(service_name, configurations)

從一組設定(通常從 YAML 檔案載入)中,按名稱設定 Active Storage 服務。Active Storage 引擎在應用程式啟動時使用它來設定全域 Active Storage 服務。

# 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: {})

將多個檔案合併成單個「組合」檔案。

# 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 的檔案。

# File activestorage/lib/active_storage/service.rb, line 101
def delete(key)
  raise NotImplementedError
end

delete_prefixed(prefix)

刪除以 prefix 開頭的檔案。

# File activestorage/lib/active_storage/service.rb, line 106
def delete_prefixed(prefix)
  raise NotImplementedError
end

download(key)

傳回位於 key 的檔案內容。

# File activestorage/lib/active_storage/service.rb, line 82
def download(key)
  raise NotImplementedError
end

download_chunk(key, range)

傳回位於 key 的檔案中,位元組範圍 range 的部分內容。

# File activestorage/lib/active_storage/service.rb, line 87
def download_chunk(key, range)
  raise NotImplementedError
end

exist?(key)

如果在 key 處存在檔案,則傳回 true

# 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 請求的標頭 雜湊

# 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)

# File activestorage/lib/active_storage/service.rb, line 91
def open(*args, **options, &block)
  ActiveStorage::Downloader.new(self).open(*args, **options, &block)
end

public?()

# File activestorage/lib/active_storage/service.rb, line 147
def public?
  @public
end

update_metadata(key, **metadata)

更新服務中 key 所識別檔案的詮釋資料。僅當服務需要儲存必須在識別後更新的特定詮釋資料時,才覆寫子類別中的這個方法。

# 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

# 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)、filenamecontent_type。此外,您還可以提供網址的有效秒數,在 expires_in 中指定。

# 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_typecontent_lengthchecksum。所有這些屬性將在上传时由服務驗證。

# 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