Class: Condo::Strata::MicrosoftAzure
- Inherits:
-
Object
- Object
- Condo::Strata::MicrosoftAzure
- Defined in:
- lib/condo/strata/microsoft_azure.rb
Instance Method Summary collapse
- #azure_connection ⇒ Object
- #destroy(upload) ⇒ Object
- #enable_cors(origin = 'http://localhost:9000') ⇒ Object
-
#get_object(options) ⇒ Object
Create a signed URL for accessing a private file.
-
#get_parts(options) ⇒ Object
No signing required for this request in Azure.
-
#initialize(options) ⇒ MicrosoftAzure
constructor
A new instance of MicrosoftAzure.
- #location ⇒ Object
- #name ⇒ Object
-
#new_upload(options) ⇒ Object
Creates a new upload request (either single shot or multi-part) => Passed: bucket_name, object_key, object_options, file_size.
-
#set_part(options) ⇒ Object
Returns the requests for uploading parts and completing a resumable upload.
Constructor Details
#initialize(options) ⇒ MicrosoftAzure
Returns a new instance of MicrosoftAzure.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/condo/strata/microsoft_azure.rb', line 15 def initialize() @options = { :name => :MicrosoftAzure }.merge!() raise ArgumentError, 'Azure Account Name missing' if @options[:account_name].nil? raise ArgumentError, 'Azure Access Key missing' if @options[:access_key].nil? @options[:blob_host] = "https://#{@options[:account_name]}.blob.core.windows.net" if @options[:blob_host].nil? @options[:location] = @options[:blob_host].to_sym end |
Instance Method Details
#azure_connection ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/condo/strata/microsoft_azure.rb', line 127 def azure_connection = { storage_account_name: @options[:account_name], storage_access_key: @options[:access_key] } [:storage_blob_host] = @options[:location].to_s if @options[:location] client = Azure.client() client.blobs end |
#destroy(upload) ⇒ Object
139 140 141 142 |
# File 'lib/condo/strata/microsoft_azure.rb', line 139 def destroy(upload) blobs = azure_connection blobs.delete_blob(upload.bucket_name, upload.object_key) end |
#enable_cors(origin = 'http://localhost:9000') ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/condo/strata/microsoft_azure.rb', line 37 def enable_cors(origin = 'http://localhost:9000') origins = origin.class == Array ? origin : [origin] blobs = azure_connection p = blobs.get_service_properties p.cors = Azure::Service::Cors.new do |cors| cors.cors_rules = [] cors.cors_rules.push(Azure::Service::CorsRule.new { |cors_rule| cors_rule.allowed_origins = origins cors_rule.allowed_methods = ["GET", "HEAD", "PUT", "POST", "OPTIONS"] cors_rule.max_age_in_seconds = 60 cors_rule.exposed_headers = ["x-ms-*", "etag", "content-type", "content-md5"] cors_rule.allowed_headers = ["x-ms-blob-type", "x-ms-version", "content-md5", "content-type"] }) end blobs.set_service_properties(p) end |
#get_object(options) ⇒ Object
Create a signed URL for accessing a private file
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/condo/strata/microsoft_azure.rb', line 58 def get_object() = {}.merge!() # Need to deep copy here [:object_options] = { verb: :get, headers: {}, permission: 'r', expires: 5.minutes.from_now }.merge!([:object_options] || {}) .merge!(@options) sign_request()[:url] end |
#get_parts(options) ⇒ Object
No signing required for this request in Azure
94 95 96 97 98 99 |
# File 'lib/condo/strata/microsoft_azure.rb', line 94 def get_parts() { type: :parts, current_part: [:resumable_id] } end |
#location ⇒ Object
33 34 35 |
# File 'lib/condo/strata/microsoft_azure.rb', line 33 def location @options[:location] end |
#name ⇒ Object
29 30 31 |
# File 'lib/condo/strata/microsoft_azure.rb', line 29 def name @options[:name] end |
#new_upload(options) ⇒ Object
Creates a new upload request (either single shot or multi-part)
> Passed: bucket_name, object_key, object_options, file_size
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/condo/strata/microsoft_azure.rb', line 74 def new_upload() = build_request() [:object_options][:headers]['x-ms-blob-type'] = 'BlockBlob' # Decide what type of request is being sent if [:file_size] > 2.megabytes return { signature: sign_request(), type: :chunked_upload } else return { signature: sign_request(), type: :direct_upload } end end |
#set_part(options) ⇒ Object
Returns the requests for uploading parts and completing a resumable upload
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/condo/strata/microsoft_azure.rb', line 103 def set_part() = build_request() if [:part] == 'finish' [:object_options][:headers]['Content-Type'] = 'application/xml; charset=UTF-8' return { signature: sign_request(), type: :finish } else = build_request() [:object_options][:headers]['x-ms-blob-type'] = 'BlockBlob' # MaxID for a part is 100_000, hence rjust 6 [:partId] = Base64.encode64([:part].to_s.rjust(6, '0')).gsub("\n", '') return { signature: sign_request(), type: :part_upload } end end |