aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/lib/active_storage/service/azure_storage_service.rb
diff options
context:
space:
mode:
authorclaudiob <claudiob@users.noreply.github.com>2017-08-04 16:33:40 -0700
committerclaudiob <claudiob@users.noreply.github.com>2017-08-04 16:40:10 -0700
commitbb7599a6c84aba44cbb5f21486ffdb4a549717dd (patch)
tree26dd9fba5a7f6c09fbfcec9fbf29b7088db88411 /activestorage/lib/active_storage/service/azure_storage_service.rb
parent552840660389e39f3ba8e47dcf35ab817c01cb48 (diff)
downloadrails-bb7599a6c84aba44cbb5f21486ffdb4a549717dd.tar.gz
rails-bb7599a6c84aba44cbb5f21486ffdb4a549717dd.tar.bz2
rails-bb7599a6c84aba44cbb5f21486ffdb4a549717dd.zip
`module ActiveStorage`, not `ActiveStorage::Class`
The reasons for this commit are: - uniformity with the other Rails libraries - (possibly) behave better with respect to autoloading - fix the index in the generated documentation Before this commit, run `rake rdoc` generates this left sidebar (ActiveStorage entries are indexed twice, both inside and outside the module): <img width="308" alt="before" src="https://user-images.githubusercontent.com/10076/28939523-7c087dec-7846-11e7-9289-38ed4a2930cd.png"> After this commit, run `rake rdoc` generates this left sidebar: (ActiveStorage entries are only indexed inside the module): <img width="303" alt="after" src="https://user-images.githubusercontent.com/10076/28939524-7c090be0-7846-11e7-8ee5-29dfecae548e.png">
Diffstat (limited to 'activestorage/lib/active_storage/service/azure_storage_service.rb')
-rw-r--r--activestorage/lib/active_storage/service/azure_storage_service.rb168
1 files changed, 85 insertions, 83 deletions
diff --git a/activestorage/lib/active_storage/service/azure_storage_service.rb b/activestorage/lib/active_storage/service/azure_storage_service.rb
index 527dc57eeb..62b13433fe 100644
--- a/activestorage/lib/active_storage/service/azure_storage_service.rb
+++ b/activestorage/lib/active_storage/service/azure_storage_service.rb
@@ -2,114 +2,116 @@ require "active_support/core_ext/numeric/bytes"
require "azure/storage"
require "azure/storage/core/auth/shared_access_signature"
-# Wraps the Microsoft Azure Storage Blob Service as a Active Storage service.
-# See `ActiveStorage::Service` for the generic API documentation that applies to all services.
-class ActiveStorage::Service::AzureStorageService < ActiveStorage::Service
- attr_reader :client, :path, :blobs, :container, :signer
-
- def initialize(path:, storage_account_name:, storage_access_key:, container:)
- @client = Azure::Storage::Client.create(storage_account_name: storage_account_name, storage_access_key: storage_access_key)
- @signer = Azure::Storage::Core::Auth::SharedAccessSignature.new(storage_account_name, storage_access_key)
- @blobs = client.blob_client
- @container = container
- @path = path
- end
+module ActiveStorage
+ # Wraps the Microsoft Azure Storage Blob Service as a Active Storage service.
+ # See `ActiveStorage::Service` for the generic API documentation that applies to all services.
+ class Service::AzureStorageService < Service
+ attr_reader :client, :path, :blobs, :container, :signer
+
+ def initialize(path:, storage_account_name:, storage_access_key:, container:)
+ @client = Azure::Storage::Client.create(storage_account_name: storage_account_name, storage_access_key: storage_access_key)
+ @signer = Azure::Storage::Core::Auth::SharedAccessSignature.new(storage_account_name, storage_access_key)
+ @blobs = client.blob_client
+ @container = container
+ @path = path
+ end
- def upload(key, io, checksum: nil)
- instrument :upload, key, checksum: checksum do
- begin
- blobs.create_block_blob(container, key, io, content_md5: checksum)
- rescue Azure::Core::Http::HTTPError => e
- raise ActiveStorage::IntegrityError
+ def upload(key, io, checksum: nil)
+ instrument :upload, key, checksum: checksum do
+ begin
+ blobs.create_block_blob(container, key, io, content_md5: checksum)
+ rescue Azure::Core::Http::HTTPError => e
+ raise ActiveStorage::IntegrityError
+ end
end
end
- end
- def download(key)
- if block_given?
- instrument :streaming_download, key do
- stream(key, &block)
- end
- else
- instrument :download, key do
- _, io = blobs.get_blob(container, key)
- io.force_encoding(Encoding::BINARY)
+ def download(key)
+ if block_given?
+ instrument :streaming_download, key do
+ stream(key, &block)
+ end
+ else
+ instrument :download, key do
+ _, io = blobs.get_blob(container, key)
+ io.force_encoding(Encoding::BINARY)
+ end
end
end
- end
- def delete(key)
- instrument :delete, key do
- begin
- blobs.delete_blob(container, key)
- rescue Azure::Core::Http::HTTPError
- false
+ def delete(key)
+ instrument :delete, key do
+ begin
+ blobs.delete_blob(container, key)
+ rescue Azure::Core::Http::HTTPError
+ false
+ end
end
end
- end
- def exist?(key)
- instrument :exist, key do |payload|
- answer = blob_for(key).present?
- payload[:exist] = answer
- answer
+ def exist?(key)
+ instrument :exist, key do |payload|
+ answer = blob_for(key).present?
+ payload[:exist] = answer
+ answer
+ end
end
- end
- def url(key, expires_in:, disposition:, filename:)
- instrument :url, key do |payload|
- base_url = url_for(key)
- generated_url = signer.signed_uri(URI(base_url), false, permissions: "r",
- expiry: format_expiry(expires_in), content_disposition: "#{disposition}; filename=\"#{filename}\"").to_s
+ def url(key, expires_in:, disposition:, filename:)
+ instrument :url, key do |payload|
+ base_url = url_for(key)
+ generated_url = signer.signed_uri(URI(base_url), false, permissions: "r",
+ expiry: format_expiry(expires_in), content_disposition: "#{disposition}; filename=\"#{filename}\"").to_s
- payload[:url] = generated_url
+ payload[:url] = generated_url
- generated_url
+ generated_url
+ end
end
- end
- def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
- instrument :url, key do |payload|
- base_url = url_for(key)
- generated_url = signer.signed_uri(URI(base_url), false, permissions: "rw",
- expiry: format_expiry(expires_in)).to_s
+ def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
+ instrument :url, key do |payload|
+ base_url = url_for(key)
+ generated_url = signer.signed_uri(URI(base_url), false, permissions: "rw",
+ expiry: format_expiry(expires_in)).to_s
- payload[:url] = generated_url
+ payload[:url] = generated_url
- generated_url
+ generated_url
+ end
end
- end
-
- def headers_for_direct_upload(key, content_type:, checksum:, **)
- { "Content-Type" => content_type, "Content-MD5" => checksum, "x-ms-blob-type" => "BlockBlob" }
- end
- private
- def url_for(key)
- "#{path}/#{container}/#{key}"
+ def headers_for_direct_upload(key, content_type:, checksum:, **)
+ { "Content-Type" => content_type, "Content-MD5" => checksum, "x-ms-blob-type" => "BlockBlob" }
end
- def blob_for(key)
- blobs.get_blob_properties(container, key)
- rescue Azure::Core::Http::HTTPError
- false
- end
+ private
+ def url_for(key)
+ "#{path}/#{container}/#{key}"
+ end
- def format_expiry(expires_in)
- expires_in ? Time.now.utc.advance(seconds: expires_in).iso8601 : nil
- end
+ def blob_for(key)
+ blobs.get_blob_properties(container, key)
+ rescue Azure::Core::Http::HTTPError
+ false
+ end
+
+ def format_expiry(expires_in)
+ expires_in ? Time.now.utc.advance(seconds: expires_in).iso8601 : nil
+ end
- # Reads the object for the given key in chunks, yielding each to the block.
- def stream(key, options = {}, &block)
- blob = blob_for(key)
+ # Reads the object for the given key in chunks, yielding each to the block.
+ def stream(key, options = {}, &block)
+ blob = blob_for(key)
- chunk_size = 5.megabytes
- offset = 0
+ chunk_size = 5.megabytes
+ offset = 0
- while offset < blob.properties[:content_length]
- _, io = blobs.get_blob(container, key, start_range: offset, end_range: offset + chunk_size - 1)
- yield io
- offset += chunk_size
+ while offset < blob.properties[:content_length]
+ _, io = blobs.get_blob(container, key, start_range: offset, end_range: offset + chunk_size - 1)
+ yield io
+ offset += chunk_size
+ end
end
- end
+ end
end