From 3179f089be4f631b9c0f8b431567992164f2bdb4 Mon Sep 17 00:00:00 2001 From: Dino Maric Date: Sat, 5 Aug 2017 00:57:02 +0200 Subject: Rename Azure to AzureStorage (#30057) --- activestorage/lib/active_storage/service.rb | 2 +- .../lib/active_storage/service/azure_service.rb | 115 --------------------- .../service/azure_storage_service.rb | 115 +++++++++++++++++++++ .../lib/active_storage/service/configurator.rb | 2 +- .../controllers/direct_uploads_controller_test.rb | 4 +- activestorage/test/service/azure_service_test.rb | 13 --- .../test/service/azure_storage_service_test.rb | 13 +++ activestorage/test/service/configurations.yml | 4 +- 8 files changed, 134 insertions(+), 134 deletions(-) delete mode 100644 activestorage/lib/active_storage/service/azure_service.rb create mode 100644 activestorage/lib/active_storage/service/azure_storage_service.rb delete mode 100644 activestorage/test/service/azure_service_test.rb create mode 100644 activestorage/test/service/azure_storage_service_test.rb (limited to 'activestorage') diff --git a/activestorage/lib/active_storage/service.rb b/activestorage/lib/active_storage/service.rb index f6b4877759..4223295ed8 100644 --- a/activestorage/lib/active_storage/service.rb +++ b/activestorage/lib/active_storage/service.rb @@ -7,7 +7,7 @@ require "active_storage/log_subscriber" # * +Disk+, to manage attachments saved directly on the hard drive. # * +GCS+, to manage attachments through Google Cloud Storage. # * +S3+, to manage attachments through Amazon S3. -# * +Azure+, to manage attachments through Microsoft Azure Storage. +# * +AzureStorage+, to manage attachments through Microsoft Azure Storage. # * +Mirror+, to be able to use several services to manage attachments. # # Inside a Rails application, you can set-up your services through the diff --git a/activestorage/lib/active_storage/service/azure_service.rb b/activestorage/lib/active_storage/service/azure_service.rb deleted file mode 100644 index a505b9a0ee..0000000000 --- a/activestorage/lib/active_storage/service/azure_service.rb +++ /dev/null @@ -1,115 +0,0 @@ -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::AzureService < 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 - - 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 - - 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 - - def delete(key) - instrument :delete, key do - begin - blobs.delete_blob(container, key) - rescue Azure::Core::Http::HTTPError - false - end - end - end - - def exist?(key) - instrument :exist, key do |payload| - answer = blob_for(key).present? - payload[:exist] = answer - answer - 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 - - payload[:url] = generated_url - - generated_url - 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 - - payload[:url] = generated_url - - generated_url - 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}" - 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) - - 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 - end - end -end diff --git a/activestorage/lib/active_storage/service/azure_storage_service.rb b/activestorage/lib/active_storage/service/azure_storage_service.rb new file mode 100644 index 0000000000..527dc57eeb --- /dev/null +++ b/activestorage/lib/active_storage/service/azure_storage_service.rb @@ -0,0 +1,115 @@ +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 + + 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 + + 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 + + def delete(key) + instrument :delete, key do + begin + blobs.delete_blob(container, key) + rescue Azure::Core::Http::HTTPError + false + end + end + end + + def exist?(key) + instrument :exist, key do |payload| + answer = blob_for(key).present? + payload[:exist] = answer + answer + 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 + + payload[:url] = generated_url + + generated_url + 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 + + payload[:url] = generated_url + + generated_url + 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}" + 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) + + 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 + end + end +end diff --git a/activestorage/lib/active_storage/service/configurator.rb b/activestorage/lib/active_storage/service/configurator.rb index 00ae24d251..a0afdaa912 100644 --- a/activestorage/lib/active_storage/service/configurator.rb +++ b/activestorage/lib/active_storage/service/configurator.rb @@ -22,7 +22,7 @@ class ActiveStorage::Service::Configurator #:nodoc: end def resolve(class_name) - require "active_storage/service/#{class_name.to_s.downcase}_service" + require "active_storage/service/#{class_name.to_s.underscore}_service" ActiveStorage::Service.const_get(:"#{class_name}Service") end end diff --git a/activestorage/test/controllers/direct_uploads_controller_test.rb b/activestorage/test/controllers/direct_uploads_controller_test.rb index b9f9bd8cb4..e056b629bb 100644 --- a/activestorage/test/controllers/direct_uploads_controller_test.rb +++ b/activestorage/test/controllers/direct_uploads_controller_test.rb @@ -69,7 +69,7 @@ else end if SERVICE_CONFIGURATIONS[:azure] - class ActiveStorage::AzureDirectUploadsControllerTest < ActionDispatch::IntegrationTest + class ActiveStorage::AzureStorageDirectUploadsControllerTest < ActionDispatch::IntegrationTest setup do @config = SERVICE_CONFIGURATIONS[:azure] @@ -99,7 +99,7 @@ if SERVICE_CONFIGURATIONS[:azure] end end else - puts "Skipping Azure Direct Upload tests because no Azure configuration was supplied" + puts "Skipping Azure Storage Direct Upload tests because no Azure Storage configuration was supplied" end class ActiveStorage::DiskDirectUploadsControllerTest < ActionDispatch::IntegrationTest diff --git a/activestorage/test/service/azure_service_test.rb b/activestorage/test/service/azure_service_test.rb deleted file mode 100644 index fb617db019..0000000000 --- a/activestorage/test/service/azure_service_test.rb +++ /dev/null @@ -1,13 +0,0 @@ -require "service/shared_service_tests" -require "uri" - -if SERVICE_CONFIGURATIONS[:azure] - class ActiveStorage::Service::AzureServiceTest < ActiveSupport::TestCase - SERVICE = ActiveStorage::Service.configure(:azure, SERVICE_CONFIGURATIONS) - - include ActiveStorage::Service::SharedServiceTests - end - -else - puts "Skipping Azure Storage Service tests because no Azure configuration was supplied" -end diff --git a/activestorage/test/service/azure_storage_service_test.rb b/activestorage/test/service/azure_storage_service_test.rb new file mode 100644 index 0000000000..e2be510b60 --- /dev/null +++ b/activestorage/test/service/azure_storage_service_test.rb @@ -0,0 +1,13 @@ +require "service/shared_service_tests" +require "uri" + +if SERVICE_CONFIGURATIONS[:azure] + class ActiveStorage::Service::AzureStorageServiceTest < ActiveSupport::TestCase + SERVICE = ActiveStorage::Service.configure(:azure, SERVICE_CONFIGURATIONS) + + include ActiveStorage::Service::SharedServiceTests + end + +else + puts "Skipping Azure Storage Service tests because no Azure configuration was supplied" +end diff --git a/activestorage/test/service/configurations.yml b/activestorage/test/service/configurations.yml index 90b6baf0db..d7aa672573 100644 --- a/activestorage/test/service/configurations.yml +++ b/activestorage/test/service/configurations.yml @@ -21,9 +21,9 @@ s3: # } # project: # bucket: -# +# # azure: -# service: Azure +# service: AzureStorage # path: "" # storage_account_name: "" # storage_access_key: "" -- cgit v1.2.3