Class: Fog::Aliyun::Storage::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aliyun/storage.rb,
lib/fog/aliyun/requests/storage/get_bucket.rb,
lib/fog/aliyun/requests/storage/get_object.rb,
lib/fog/aliyun/requests/storage/put_bucket.rb,
lib/fog/aliyun/requests/storage/put_object.rb,
lib/fog/aliyun/requests/storage/copy_object.rb,
lib/fog/aliyun/requests/storage/head_object.rb,
lib/fog/aliyun/requests/storage/list_buckets.rb,
lib/fog/aliyun/requests/storage/list_objects.rb,
lib/fog/aliyun/requests/storage/delete_bucket.rb,
lib/fog/aliyun/requests/storage/delete_object.rb,
lib/fog/aliyun/requests/storage/get_container.rb,
lib/fog/aliyun/requests/storage/put_container.rb,
lib/fog/aliyun/requests/storage/get_containers.rb,
lib/fog/aliyun/requests/storage/delete_container.rb,
lib/fog/aliyun/requests/storage/get_object_http_url.rb,
lib/fog/aliyun/requests/storage/get_object_https_url.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fog/aliyun/storage.rb', line 79

def initialize(options = {})
  # initialize the parameters
  @aliyun_region_id = options[:aliyun_region_id] || options[:aliyun_oss_location] || DEFAULT_REGION
  @aliyun_oss_endpoint = options[:aliyun_oss_endpoint] || region_to_endpoint(@aliyun_region_id)
  @aliyun_accesskey_id = options[:aliyun_accesskey_id]
  @aliyun_accesskey_secret = options[:aliyun_accesskey_secret]
  @aliyun_oss_bucket = options[:aliyun_oss_bucket]
  @aliyun_oss_sdk_log_path=options[:aliyun_oss_sdk_log_path]
  if @aliyun_oss_sdk_log_path && !::File.exist?(@aliyun_oss_sdk_log_path)
    `touch #{@aliyun_oss_sdk_log_path}`
  end
  ENV["ALIYUN_OSS_SDK_LOG_PATH"] = @aliyun_oss_sdk_log_path
  # check for the parameters
  missing_credentials = []
  missing_credentials << :aliyun_oss_bucket unless @aliyun_oss_bucket
  missing_credentials << :aliyun_accesskey_id unless @aliyun_accesskey_id
  missing_credentials << :aliyun_accesskey_secret unless @aliyun_accesskey_secret
  raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}" unless missing_credentials.empty?

  @connection_options = options[:connection_options] || {}

  endpoint = @aliyun_oss_endpoint

  @aliyun_oss_endpoint = "#{DEFAULT_SCHEME}://#{endpoint}" unless endpoint.start_with?(DEFAULT_SCHEME)

  uri = URI.parse(@aliyun_oss_endpoint)
  @host = uri.host
  @path = uri.path
  @scheme = uri.scheme || DEFAULT_SCHEME
  @port = uri.port || DEFAULT_SCHEME_PORT[@scheme]

  @persistent = options[:persistent] || false
  @oss_client = AliyunOssSdk::Client.new(
                             :endpoint => @aliyun_oss_endpoint,
                             :access_key_id => @aliyun_accesskey_id,
                             :access_key_secret => @aliyun_accesskey_secret
  )

  # initiate a aliyun oss ruby sdk config and using sdk http to invoke the OSS openapi
  @oss_config = AliyunOssSdk::Config.new(
      :endpoint => @aliyun_oss_endpoint,
      :access_key_id => @aliyun_accesskey_id,
      :access_key_secret => @aliyun_accesskey_secret
  )
  @oss_http = AliyunOssSdk::HTTP.new(@oss_config)
end

Instance Attribute Details

#aliyun_accesskey_idObject (readonly)

Initialize connection to OSS

Notes

options parameter must include values for :aliyun_accesskey_id, :aliyun_secret_access_key and :aliyun_oss_bucket in order to create a connection. :aliyun_oss_location will be replaced by :aliyun_region_id, and it has a default value cn-hangzhou if :aliyun_oss_endpoint is not specified, it will be generated by method region_to_endpoint

Examples

sdb = Fog::Storage.new(:provider=>'aliyun',
 :aliyun_accesskey_id => your_:aliyun_accesskey_id,
 :aliyun_secret_access_key => your_aliyun_secret_access_key
)

Parameters

  • options<~Hash> - config arguments for connection. Defaults to {}.

Returns

  • OSS object with connection to aliyun.



72
73
74
# File 'lib/fog/aliyun/storage.rb', line 72

def aliyun_accesskey_id
  @aliyun_accesskey_id
end

#aliyun_accesskey_secretObject (readonly)

Returns the value of attribute aliyun_accesskey_secret.



73
74
75
# File 'lib/fog/aliyun/storage.rb', line 73

def aliyun_accesskey_secret
  @aliyun_accesskey_secret
end

#aliyun_oss_bucketObject (readonly)

Returns the value of attribute aliyun_oss_bucket.



76
77
78
# File 'lib/fog/aliyun/storage.rb', line 76

def aliyun_oss_bucket
  @aliyun_oss_bucket
end

#aliyun_oss_endpointObject (readonly)

Returns the value of attribute aliyun_oss_endpoint.



74
75
76
# File 'lib/fog/aliyun/storage.rb', line 74

def aliyun_oss_endpoint
  @aliyun_oss_endpoint
end

#aliyun_oss_sdk_log_pathObject (readonly)

Returns the value of attribute aliyun_oss_sdk_log_path.



77
78
79
# File 'lib/fog/aliyun/storage.rb', line 77

def aliyun_oss_sdk_log_path
  @aliyun_oss_sdk_log_path
end

#aliyun_region_idObject (readonly)

Returns the value of attribute aliyun_region_id.



75
76
77
# File 'lib/fog/aliyun/storage.rb', line 75

def aliyun_region_id
  @aliyun_region_id
end

Instance Method Details

#abort_multipart_upload(bucket_name, object, upload_id) ⇒ Object



19
20
21
22
# File 'lib/fog/aliyun/requests/storage/delete_object.rb', line 19

def abort_multipart_upload(bucket_name, object, upload_id)
  bucket = @oss_client.get_bucket(bucket_name)
  bucket.abort_upload(upload_id, object)
end

#bucket_exists?(bucket_name) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/fog/aliyun/requests/storage/get_bucket.rb', line 7

def bucket_exists?(bucket_name)
  @oss_client.bucket_exists?(bucket_name)
end

#complete_multipart_upload(bucket, object, uploadId) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/fog/aliyun/requests/storage/put_object.rb', line 117

def complete_multipart_upload(bucket, object, uploadId)
  parts = list_parts(bucket, object, uploadId, options = {})
  request_part = []
  return if parts.empty?
  for i in 0..(parts.size - 1)
    part = parts[i]
    request_part[i] = { 'PartNumber' => part['PartNumber'], 'ETag' => part['ETag'] }
  end
  body = XmlSimple.xml_out({ 'Part' => request_part }, 'RootName' => 'CompleteMultipartUpload')

  path = object + '?uploadId=' + uploadId
  resource = bucket + '/' + path
  request(
    expects: 200,
    method: 'POST',
    path: path,
    bucket: bucket,
    resource: resource,
    body: body
  )
end

#copy_object(source_bucket, source_object, target_bucket, target_object, options = {}) ⇒ Object

Copy object

Parameters

  • source_bucket<~String> - Name of source bucket

  • source_object<~String> - Name of source object

  • target_bucket<~String> - Name of bucket to create copy in

  • target_object<~String> - Name for new copy of object

  • options<~Hash> - Additional headers options={}



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fog/aliyun/requests/storage/copy_object.rb', line 15

def copy_object(source_bucket, source_object, target_bucket, target_object, options = {})
  options = options.reject { |_key, value| value.nil? }
  bucket = options[:bucket]
  bucket ||= @aliyun_oss_bucket
  source_bucket ||= bucket
  target_bucket ||= bucket
  headers = { 'x-oss-copy-source' => "/#{source_bucket}/#{source_object}" }
  resource = target_bucket + '/' + target_object
  request(expects: [200, 203],
          headers: headers,
          method: 'PUT',
          path: target_object,
          bucket: target_bucket,
          resource: resource)
end

#delete_bucket(bucket) ⇒ Object

Delete an existing bucket

Parameters

  • bucket<~String> - Name of bucket to delete



12
13
14
15
16
17
18
19
20
# File 'lib/fog/aliyun/requests/storage/delete_bucket.rb', line 12

def delete_bucket(bucket)
  resource = bucket + '/'
  request(
    expects: 204,
    method: 'DELETE',
    bucket: bucket,
    resource: resource
  )
end

#delete_container(container, options = {}) ⇒ Object

Delete an existing container

Parameters

  • container<~String> - Name of container to delete

  • options



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fog/aliyun/requests/storage/delete_container.rb', line 13

def delete_container(container, options = {})
  bucket = options[:bucket]
  bucket ||= @aliyun_oss_bucket
  object = container + '/'
  resource = bucket + '/' + object

  request(
    expects: 204,
    method: 'DELETE',
    path: object,
    bucket: bucket,
    resource: resource
  )
end

#delete_object(object, options = {}) ⇒ Object

Delete an existing object

Parameters

  • object<~String> - Name of object to delete



12
13
14
15
16
17
# File 'lib/fog/aliyun/requests/storage/delete_object.rb', line 12

def delete_object(object, options = {})
  bucket_name = options[:bucket]
  bucket_name ||= @aliyun_oss_bucket
  bucket = @oss_client.get_bucket(bucket_name)
  bucket.delete_object(object)
end

#get_bucket(bucket, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fog/aliyun/requests/storage/get_bucket.rb', line 11

def get_bucket(bucket, options = {})
  prefix = options['prefix']
  marker = options['marker']
  # Set the GetBucket max limitation to 1000
  maxKeys = options['max-keys'] || 1000
  maxKeys = maxKeys.to_i
  maxKeys = [maxKeys, 1000].min.to_s
  delimiter = options['delimiter']
  path = ''
  if prefix
    path += '/?prefix=' + prefix
    path += '&marker=' + marker if marker
    path += '&max-keys=' + maxKeys if maxKeys
    path += '&delimiter=' + delimiter if delimiter

  elsif marker
    path += '/?marker=' + marker
    path += '&max-keys=' + maxKeys if maxKeys
    path += '&delimiter=' + delimiter if delimiter

  elsif maxKeys
    path += '/?max-keys=' + maxKeys
    path += '&delimiter=' + delimiter if delimiter
  elsif delimiter
    path += '/?delimiter=' + delimiter
  end

  resource = bucket + '/'
  ret = request(
    expects: [200, 203, 404],
    method: 'GET',
    bucket: bucket,
    resource: resource,
    path: path
  )
  xml = ret.data[:body]
  XmlSimple.xml_in(xml)
end

#get_bucket_acl(bucket) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fog/aliyun/requests/storage/get_bucket.rb', line 50

def get_bucket_acl(bucket)
  attribute = '?acl'
  resource = bucket + '/' + attribute
  ret = request(
    expects: [200, 203],
    method: 'GET',
    path: attribute,
    bucket: bucket,
    resource: resource
  )
  XmlSimple.xml_in(ret.data[:body])['AccessControlList'][0]['Grant'][0]
end

#get_bucket_CORSRules(bucket) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fog/aliyun/requests/storage/get_bucket.rb', line 63

def get_bucket_CORSRules(bucket)
  attribute = '?cors'
  resource = bucket + '/' + attribute
  ret = request(
    expects: [200, 203, 404],
    method: 'GET',
    path: attribute,
    bucket: bucket,
    resource: resource
  )
  XmlSimple.xml_in(ret.data[:body])['CORSRule'][0] if ret.data[:status] != 404
end

#get_bucket_lifecycle(bucket) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fog/aliyun/requests/storage/get_bucket.rb', line 76

def get_bucket_lifecycle(bucket)
  attribute = '?lifecycle'
  resource = bucket + '/' + attribute
  ret = request(
    expects: [200, 203, 404],
    method: 'GET',
    path: attribute,
    bucket: bucket,
    resource: resource
  )
  XmlSimple.xml_in(ret.data[:body])['Rule'][0] if ret.data[:status] != 404
end

#get_bucket_logging(bucket) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fog/aliyun/requests/storage/get_bucket.rb', line 89

def get_bucket_logging(bucket)
  attribute = '?logging'
  resource = bucket + '/' + attribute
  ret = request(
    expects: [200, 203],
    method: 'GET',
    path: attribute,
    bucket: bucket,
    resource: resource
  )
  XmlSimple.xml_in(ret.data[:body])['LoggingEnabled'][0]['TargetPrefix']
end

#get_bucket_referer(bucket) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fog/aliyun/requests/storage/get_bucket.rb', line 102

def get_bucket_referer(bucket)
  attribute = '?referer'
  resource = bucket + '/' + attribute
  ret = request(
    expects: [200, 203],
    method: 'GET',
    path: attribute,
    bucket: bucket,
    resource: resource
  )
  XmlSimple.xml_in(ret.data[:body])
end

#get_bucket_website(bucket) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fog/aliyun/requests/storage/get_bucket.rb', line 115

def get_bucket_website(bucket)
  attribute = '?website'
  resource = bucket + '/' + attribute
  ret = request(
    expects: [200, 203, 404],
    method: 'GET',
    path: attribute,
    bucket: bucket,
    resource: resource
  )
  XmlSimple.xml_in(ret.data[:body]) if ret.data[:status] != 404
end

#get_container(container, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fog/aliyun/requests/storage/get_container.rb', line 7

def get_container(container, options = {})
  options = options.reject { |_key, value| value.nil? }

  bucket = options[:bucket]
  bucket ||= @aliyun_oss_bucket

  marker = options[:marker]
  maxKeys = options[:maxKeys]||1000
  maxKeys = maxKeys.to_i
  maxKeys = [maxKeys,1000].min.to_s
  delimiter = '/'

  path = ''

  prefix = if container == '' || container == '.' || container.nil?
             nil
           else
             container + '/'
           end

  if prefix
    path += '?prefix=' + prefix
    path += '&marker=' + marker if marker
    path += '&max-keys=' + maxKeys if maxKeys
    path += '&delimiter=' + delimiter if delimiter
  elsif marker
    path += '?marker=' + marker
    path += '&max-keys=' + maxKeys if maxKeys
    path += '&delimiter=' + delimiter if delimiter
  elsif maxKeys
    path += '?max-keys=' + maxKeys
    path += '&delimiter=' + delimiter if delimiter
  elsif delimiter
    path += '?delimiter=' + delimiter
  end

  resource = bucket + '/'
  ret = request(
    expects: [200, 203, 400],
    method: 'GET',
    path: path,
    resource: resource,
    bucket: bucket
  )
  xml = ret.data[:body]
  XmlSimple.xml_in(xml)['CommonPrefixes']
end

#get_containers(options = {}) ⇒ Object

List existing storage containers

Parameters

  • options<~Hash>:

    • ‘maxKeys’<~Integer> - Upper limit to number of results returned

    • ‘marker’<~String> - Only return objects with name greater than this value

Returns



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fog/aliyun/requests/storage/get_containers.rb', line 16

def get_containers(options = {})
  options = options.reject { |_key, value| value.nil? }
  bucket = options[:bucket]
  bucket ||= @aliyun_oss_bucket
  prefix = options[:prefix]
  marker = options[:marker]
  maxKeys = options[:maxKeys]||1000
  maxKeys = maxKeys.to_i
  maxKeys = [maxKeys,1000].min.to_s
  delimiter = '/'

  path = ''
  if prefix
    path += '?prefix=' + prefix
    path += '&marker=' + marker if marker
    path += '&max-keys=' + maxKeys if maxKeys
    path += '&delimiter=' + delimiter if delimiter

  elsif marker
    path += '?marker=' + marker
    path += '&max-keys=' + maxKeys if maxKeys
    path += '&delimiter=' + delimiter if delimiter

  elsif maxKeys
    path += '?max-keys=' + maxKeys
    path += '&delimiter=' + delimiter if delimiter

  elsif delimiter
    path += '?delimiter=' + delimiter
  end

  resource = bucket + '/'
  ret = request(
    expects: [200, 203, 400],
    method: 'GET',
    path: path,
    resource: resource,
    bucket: bucket
  )
  xml = ret.data[:body]
  XmlSimple.xml_in(xml)['CommonPrefixes']
end

#get_object(object, options = {}, &block) ⇒ Object

Get details for object

Parameters

  • object<~String> - Name of object to look for



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/aliyun/requests/storage/get_object.rb', line 12

def get_object(object, options = {}, &block)
  options = options.reject { |_key, value| value.nil? }
  bucket_name = options[:bucket]
  bucket_name ||= @aliyun_oss_bucket
  options.delete(:bucket)
  # Using OSS ruby SDK to fix performance issue
  params = { :headers => {} }
  params[:query] = options.delete('query') || {}

  params[:headers].merge!(options)
  if options['If-Modified-Since']
    params[:headers]['If-Modified-Since'] = Fog::Time.at(options['If-Modified-Since'].to_i).to_date_header
  end
  if options['If-Unmodified-Since']
    params[:headers]['If-Unmodified-Since'] = Fog::Time.at(options['If-Unmodified-Since'].to_i).to_date_header
  end

  if block_given?
    params[:response_block] = Proc.new
  end

  @oss_http.get({:bucket => bucket_name, :object => object}, params, &block)

end

#get_object_http_url_public(object, expires, options = {}) ⇒ Object

Get an expiring object http url

Parameters

  • container<~String> - Name of container containing object

  • object<~String> - Name of object to get expiring url for

  • expires<~Integer> - An expiry time for this url

Returns

  • response<~Excon::Response>:

    • body<~String> - url for object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fog/aliyun/requests/storage/get_object_http_url.rb', line 17

def get_object_http_url_public(object, expires, options = {})
  options = options.reject { |_key, value| value.nil? }
  bucket_name = options[:bucket]
  bucket_name ||= @aliyun_oss_bucket
  bucket = @oss_client.get_bucket(bucket_name)
  acl = bucket.acl()

  if acl == 'private'
    expires_time = (Time.now.to_i + (expires.nil? ? 0 : expires.to_i)).to_s
    resource = bucket_name + '/' + object
    signature = sign('GET', expires_time, nil, resource)
    'http://' + bucket_name + '.' + @host + '/' + object +
      '?OSSAccessKeyId=' + @aliyun_accesskey_id + '&Expires=' + expires_time +
      '&Signature=' + URI.encode(signature, '/[^!*\'()\;?:@#&%=+$,{}[]<>`" ')
  elsif acl == 'public-read' || acl == 'public-read-write'
    'http://' + bucket_name + '.' + @host + '/' + object
  else
    'acl is wrong with value:' + acl
  end
end

#get_object_https_url_public(object, expires, options = {}) ⇒ Object

Get an expiring object https url from Cloud Files

Parameters

  • container<~String> - Name of container containing object

  • object<~String> - Name of object to get expiring url for

  • expires<~Integer> - An expiry time for this url

Returns

  • response<~Excon::Response>:

    • body<~String> - url for object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fog/aliyun/requests/storage/get_object_https_url.rb', line 17

def get_object_https_url_public(object, expires, options = {})
  options = options.reject { |_key, value| value.nil? }
  bucket_name = options[:bucket]
  bucket_name ||= @aliyun_oss_bucket
  bucket = @oss_client.get_bucket(bucket_name)
  acl = bucket.acl()

  if acl == 'private'
    expires_time = (Time.now.to_i + (expires.nil? ? 0 : expires.to_i)).to_s
    resource = bucket_name + '/' + object
    signature = sign('GET', expires_time, nil, resource)
    'https://' + bucket_name + '.' + @host + '/' + object +
      '?OSSAccessKeyId=' + @aliyun_accesskey_id + '&Expires=' + expires_time +
      '&Signature=' + URI.encode(signature, '/[^!*\'()\;?:@#&%=+$,{}[]<>`" ')
  elsif acl == 'public-read' || acl == 'public-read-write'
    'https://' + bucket_name + '.' + @host + '/' + object
  else
    'acl is wrong with value:' + acl
  end
end

#head_object(object, options = {}) ⇒ Object

Get headers for object

Parameters

  • object<~String> - Name of object to look for



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/aliyun/requests/storage/head_object.rb', line 12

def head_object(object, options = {})
  bucket_name = options[:bucket]
  bucket_name ||= @aliyun_oss_bucket
  resource = bucket_name + '/' + object
  ret = request(
    expects: [200, 404],
    method: 'HEAD',
    path: object,
    bucket: bucket_name,
    resource: resource
  )
  ret
end

#initiate_multipart_upload(bucket, object) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fog/aliyun/requests/storage/put_object.rb', line 91

def initiate_multipart_upload(bucket, object)
  path = object + '?uploads'
  resource = bucket + '/' + path
  ret = request(
    expects: 200,
    method: 'POST',
    path: path,
    bucket: bucket,
    resource: resource
  )
  XmlSimple.xml_in(ret.data[:body])['UploadId'][0]
end

#list_buckets(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/aliyun/requests/storage/list_buckets.rb', line 7

def list_buckets(options = {})
  prefix = options[:prefix]
  marker = options[:marker]
  maxKeys = options[:maxKeys] || 1000
  maxKeys = maxKeys.to_i
  maxKeys =[maxKeys,1000].min.to_s

  path = ''
  if prefix
    path += '?prefix=' + prefix
    path += '&marker=' + marker if marker
    path += '&max-keys=' + maxKeys if maxKeys

  elsif marker
    path += '?marker=' + marker
    path += '&max-keys=' + maxKeys if maxKeys

  elsif maxKeys
    path += '?max-keys=' + maxKeys
  end

  ret = request(
    expects: [200, 203],
    method: 'GET',
    path: path
  )
  xml = ret.data[:body]
  XmlSimple.xml_in(xml)['Buckets'][0]
end

#list_multipart_uploads(bucket, _options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fog/aliyun/requests/storage/list_objects.rb', line 49

def list_multipart_uploads(bucket, _options = {})
  path = '?uploads'
  resource = bucket + '/' + path

  ret = request(
    expects: 200,
    method: 'GET',
    path: path,
    bucket: bucket,
    resource: resource
  )
  XmlSimple.xml_in(ret.data[:body])['Upload']
end

#list_objects(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/aliyun/requests/storage/list_objects.rb', line 7

def list_objects(options = {})
  bucket = options['bucket']
  bucket ||= @aliyun_oss_bucket
  prefix = options['prefix']
  marker = options['marker']
  # Set the ListObjects max limitation to 1000
  maxKeys = options[:maxKeys]||1000
  maxKeys = maxKeys.to_i
  maxKeys = [maxKeys,1000].min.to_s
  delimiter = options['delimiter']

  path = ''
  if prefix
    path += '/?prefix=' + prefix
    path += '&marker=' + marker if marker
    path += '&max-keys=' + maxKeys if maxKeys
    path += '&delimiter=' + delimiter if delimiter

  elsif marker
    path += '/?marker=' + marker
    path += '&max-keys=' + maxKeys if maxKeys
    path += '&delimiter=' + delimiter if delimiter

  elsif maxKeys
    path += '/?max-keys=' + maxKeys
    path += '&delimiter=' + delimiter if delimiter
  elsif delimiter
    path += '/?delimiter=' + delimiter
  end

  resource = bucket + '/'
  ret = request(
    expects: [200, 203, 400],
    method: 'GET',
    path: path,
    resource: resource,
    bucket: bucket
  )
  xml = ret.data[:body]
  XmlSimple.xml_in(xml)
end

#list_parts(bucket, object, uploadid, _options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fog/aliyun/requests/storage/list_objects.rb', line 63

def list_parts(bucket, object, uploadid, _options = {})
  path = object + '?uploadId=' + uploadid
  resource = bucket + '/' + path

  ret = request(
    expects: 200,
    method: 'GET',
    path: path,
    bucket: bucket,
    resource: resource
  )
  XmlSimple.xml_in(ret.data[:body])['Part']
end

#put_bucket(bucketName) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/fog/aliyun/requests/storage/put_bucket.rb', line 7

def put_bucket(bucketName)
  resource = bucketName + '/'
  request(
    expects: [200, 203],
    method: 'PUT',
    resource: resource,
    bucket: bucketName
  )
end

#put_container(name, options = {}) ⇒ Object

Create a new container

Parameters

  • name<~String> - Name for container



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/aliyun/requests/storage/put_container.rb', line 12

def put_container(name, options = {})
  bucket = options[:bucket]
  bucket ||= @aliyun_oss_bucket

  path = name + '/'
  resource = bucket + '/' + name + '/'
  request(
    expects: [200, 203],
    method: 'PUT',
    path: path,
    bucket: bucket,
    resource: resource
  )
end

#put_folder(bucket, folder) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fog/aliyun/requests/storage/put_object.rb', line 39

def put_folder(bucket, folder)
  path = folder + '/'
  resource = bucket + '/' + folder + '/'
  request(
    expects: [200, 203],
    method: 'PUT',
    path: path,
    bucket: bucket,
    resource: resource
  )
end

#put_multipart_object(bucket, object, file) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fog/aliyun/requests/storage/put_object.rb', line 51

def put_multipart_object(bucket, object, file)
  # find the right uploadid
  uploads = list_multipart_uploads(bucket)
  upload = (uploads&.find { |tmpupload| tmpupload['Key'][0] == object })

  uploadedSize = 0
  start_partNumber = 1
  if !upload.nil?
    uploadId = upload['UploadId'][0]
    parts = list_parts(bucket, object, uploadId)
    if !parts.nil? && !parts.empty?
      if parts[-1]['Size'][0].to_i != 5_242_880
        # the part is the last one, if its size is over 5m, then finish this upload
        complete_multipart_upload(bucket, object, uploadId)
        return
      end
      uploadedSize = (parts[0]['Size'][0].to_i * (parts.size - 1)) + parts[-1]['Size'][0].to_i
      start_partNumber = parts[-1]['PartNumber'][0].to_i + 1
    end
  else
    # create upload ID
    uploadId = initiate_multipart_upload(bucket, object)
  end

  if file.size <= uploadedSize
    complete_multipart_upload(bucket, object, uploadId)
    return
  end

  end_partNumber = (file.size + 5_242_880 - 1) / 5_242_880
  file.seek(uploadedSize)

  for i in start_partNumber..end_partNumber
    body = file.read(5_242_880)
    upload_part(bucket, object, i.to_s, uploadId, body)
  end

  complete_multipart_upload(bucket, object, uploadId)
end

#put_object(object, file = nil, options = {}) ⇒ Object

Put details for object

Parameters

  • object<~String> - Name of object to look for



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fog/aliyun/requests/storage/put_object.rb', line 12

def put_object(object, file = nil, options = {})
  bucket_name = options[:bucket]
  bucket_name ||= @aliyun_oss_bucket
  bucket = @oss_client.get_bucket(bucket_name)
  return bucket.put_object(object) if file.nil?
  # With a single PUT operation you can upload objects up to 5 GB in size.
  if file.size > 5_368_709_120
    bucket.resumable_upload(object, file.path)
  end
  bucket.put_object(object, :file => file.path)
end

#put_object_with_body(object, body, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/aliyun/requests/storage/put_object.rb', line 24

def put_object_with_body(object, body, options = {})
  bucket_name = options[:bucket]
  bucket_name ||= @aliyun_oss_bucket

  resource = bucket_name + '/' + object
  request(
    expects: [200, 203],
    method: 'PUT',
    path: object,
    bucket: bucket_name,
    resource: resource,
    body: body
  )
end

#region_to_endpoint(region = nil) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/fog/aliyun/storage.rb', line 130

def region_to_endpoint(region = nil)
  case region.to_s
  when ''
    "oss-#{DEFAULT_REGION}.aliyuncs.com"
  else
    "oss-#{region}.aliyuncs.com"
  end
end

#reloadObject



126
127
128
# File 'lib/fog/aliyun/storage.rb', line 126

def reload
  @connection.reset
end

#request(params) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/fog/aliyun/storage.rb', line 139

def request(params)
  method = params[:method]
  time = Time.new.utc
  date = time.strftime('%a, %d %b %Y %H:%M:%S GMT')

  bucket = params[:bucket]
  tmpHost = if bucket
              bucket + '.' + @host
            else
              @host
            end

  @connection = Fog::Core::Connection.new("#{@scheme}://#{tmpHost}", @persistent, @connection_options)
  contentType = params[:contentType]

  begin
    headers = ''
    if params[:headers]
      params[:headers].each do |k, v|
      headers += "#{k}:#{v}\n" if k != 'Range'
      end
    end
    signature = sign(method, date, contentType, params[:resource], headers)
    response = @connection.request(params.merge(headers: {
      'Content-Type' => contentType,
      'Authorization' => 'OSS ' + @aliyun_accesskey_id + ':' + signature,
      'Date' => date
    }.merge!(params[:headers] || {}),
                                                path: "#{@path}/#{params[:path]}",
                                                query: params[:query]))
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
          when Excon::Errors::NotFound
            Fog::Aliyun::Storage::NotFound.slurp(error)
          else
            error
          end
  end

  response
end

#sign(method, date, contentType, resource = nil, headers = nil) ⇒ Object

copmute signature



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/fog/aliyun/storage.rb', line 182

def sign(method, date, contentType, resource = nil, headers = nil)
  contentmd5 = ''

  canonicalizedResource = if resource
                            '/' + resource
                          else
                            '/'
                          end

  canonicalizedOSSHeaders = headers || ''

  contentTypeStr = contentType || ''

  stringToSign = method + "\n" + contentmd5 + "\n" + contentTypeStr + "\n" + date + "\n" + canonicalizedOSSHeaders + canonicalizedResource

  digVer = OpenSSL::Digest.new('sha1')
  digest = OpenSSL::HMAC.digest(digVer, @aliyun_accesskey_secret, stringToSign)
  signature = Base64.encode64(digest)
  signature[-1] = ''

  signature
end

#upload_part(bucket, object, partNumber, uploadId, body) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fog/aliyun/requests/storage/put_object.rb', line 104

def upload_part(bucket, object, partNumber, uploadId, body)
  path = object + '?partNumber=' + partNumber + '&uploadId=' + uploadId
  resource = bucket + '/' + path
  request(
    expects: [200, 203],
    method: 'PUT',
    path: path,
    bucket: bucket,
    resource: resource,
    body: body
  )
end