Class: Condo::Strata::MicrosoftAzure

Inherits:
Object
  • Object
show all
Defined in:
lib/condo/strata/microsoft_azure.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ MicrosoftAzure

Returns a new instance of MicrosoftAzure.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/condo/strata/microsoft_azure.rb', line 15

def initialize(options)
    @options = {
        :name => :MicrosoftAzure
    }.merge!(options)

    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_connectionObject



127
128
129
130
131
132
133
134
135
136
# File 'lib/condo/strata/microsoft_azure.rb', line 127

def azure_connection
    options = {
        storage_account_name: @options[:account_name],
        storage_access_key: @options[:access_key]
    }
    options[:storage_blob_host] = @options[:location].to_s if @options[:location]

    client = Azure.client(options)
    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(options)
    options = {}.merge!(options)    # Need to deep copy here
    options[:object_options] = {
        verb: :get,
        headers: {},
        permission: 'r',
        expires: 5.minutes.from_now
    }.merge!(options[:object_options] || {})
    options.merge!(@options)

    sign_request(options)[: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(options)
    {
        type: :parts,
        current_part: options[:resumable_id]
    }
end

#locationObject



33
34
35
# File 'lib/condo/strata/microsoft_azure.rb', line 33

def location
    @options[:location]
end

#nameObject



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(options)
    options = build_request(options)
    options[:object_options][:headers]['x-ms-blob-type'] = 'BlockBlob'

    # Decide what type of request is being sent
    if options[:file_size] > 2.megabytes
        return {
            signature: sign_request(options),
            type: :chunked_upload
        }
    else
        return {
            signature: sign_request(options),
            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(options)
    options = build_request(options)

    if options[:part] == 'finish'
        options[:object_options][:headers]['Content-Type'] = 'application/xml; charset=UTF-8'

        return {
            signature: sign_request(options),
            type: :finish
        }
    else
        options = build_request(options)
        options[:object_options][:headers]['x-ms-blob-type'] = 'BlockBlob'
        # MaxID for a part is 100_000, hence rjust 6
        options[:partId] = Base64.encode64(options[:part].to_s.rjust(6, '0')).gsub("\n", '')

        return {
            signature: sign_request(options),
            type: :part_upload
        }
    end
end