fog-aliyun 0.3.10 → 0.3.16

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fog
4
- module Storage
5
- class Aliyun
4
+ module Aliyun
5
+ class Storage
6
6
  class Real
7
7
  # Get headers for object
8
8
  #
@@ -12,16 +12,13 @@ module Fog
12
12
  def head_object(object, options = {})
13
13
  bucket = options[:bucket]
14
14
  bucket ||= @aliyun_oss_bucket
15
- location = get_bucket_location(bucket)
16
- endpoint = 'http://' + location + '.aliyuncs.com'
17
15
  resource = bucket + '/' + object
18
16
  ret = request(
19
17
  expects: [200, 404],
20
18
  method: 'HEAD',
21
19
  path: object,
22
20
  bucket: bucket,
23
- resource: resource,
24
- endpoint: endpoint
21
+ resource: resource
25
22
  )
26
23
  ret
27
24
  end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fog
4
- module Storage
5
- class Aliyun
4
+ module Aliyun
5
+ class Storage
6
6
  class Real
7
7
  def list_buckets(options = {})
8
8
  prefix = options[:prefix]
@@ -1,18 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fog
4
- module Storage
5
- class Aliyun
4
+ module Aliyun
5
+ class Storage
6
6
  class Real
7
7
  def list_objects(options = {})
8
- bucket = options[:bucket]
8
+ bucket = options['bucket']
9
9
  bucket ||= @aliyun_oss_bucket
10
- prefix = options[:prefix]
11
- marker = options[:marker]
10
+ prefix = options['prefix']
11
+ marker = options['marker']
12
12
  # Set the ListObjects max limitation to 1000
13
- maxKeys = options[:maxKeys] || 1000
13
+ maxKeys = options['max-keys'] || 1000
14
14
  maxKeys = [maxKeys, 1000].min
15
- delimiter = options[:delimiter]
15
+ delimiter = options['delimiter']
16
16
 
17
17
  path = ''
18
18
  if prefix
@@ -45,11 +45,7 @@ module Fog
45
45
  XmlSimple.xml_in(xml)
46
46
  end
47
47
 
48
- def list_multipart_uploads(bucket, endpoint, _options = {})
49
- if endpoint.nil?
50
- location = get_bucket_location(bucket)
51
- endpoint = 'http://' + location + '.aliyuncs.com'
52
- end
48
+ def list_multipart_uploads(bucket, _options = {})
53
49
  path = '?uploads'
54
50
  resource = bucket + '/' + path
55
51
 
@@ -58,17 +54,12 @@ module Fog
58
54
  method: 'GET',
59
55
  path: path,
60
56
  bucket: bucket,
61
- resource: resource,
62
- endpoint: endpoint
57
+ resource: resource
63
58
  )
64
59
  XmlSimple.xml_in(ret.data[:body])['Upload']
65
60
  end
66
61
 
67
- def list_parts(bucket, object, endpoint, uploadid, _options = {})
68
- if endpoint.nil?
69
- location = get_bucket_location(bucket)
70
- endpoint = 'http://' + location + '.aliyuncs.com'
71
- end
62
+ def list_parts(bucket, object, uploadid, _options = {})
72
63
  path = object + '?uploadId=' + uploadid
73
64
  resource = bucket + '/' + path
74
65
 
@@ -77,8 +68,7 @@ module Fog
77
68
  method: 'GET',
78
69
  path: path,
79
70
  bucket: bucket,
80
- resource: resource,
81
- endpoint: endpoint
71
+ resource: resource
82
72
  )
83
73
  XmlSimple.xml_in(ret.data[:body])['Part']
84
74
  end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fog
4
- module Storage
5
- class Aliyun
4
+ module Aliyun
5
+ class Storage
6
6
  class Real
7
7
  def put_bucket(bucketName)
8
8
  resource = bucketName + '/'
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fog
4
- module Storage
5
- class Aliyun
4
+ module Aliyun
5
+ class Storage
6
6
  class Real
7
7
  # Create a new container
8
8
  #
@@ -12,8 +12,6 @@ module Fog
12
12
  def put_container(name, options = {})
13
13
  bucket = options[:bucket]
14
14
  bucket ||= @aliyun_oss_bucket
15
- location = get_bucket_location(bucket)
16
- endpoint = 'http://' + location + '.aliyuncs.com'
17
15
 
18
16
  path = name + '/'
19
17
  resource = bucket + '/' + name + '/'
@@ -22,8 +20,7 @@ module Fog
22
20
  method: 'PUT',
23
21
  path: path,
24
22
  bucket: bucket,
25
- resource: resource,
26
- endpoint: endpoint
23
+ resource: resource
27
24
  )
28
25
  end
29
26
  end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fog
4
- module Storage
5
- class Aliyun
4
+ module Aliyun
5
+ class Storage
6
6
  class Real
7
7
  # Put details for object
8
8
  #
@@ -10,52 +10,33 @@ module Fog
10
10
  # * object<~String> - Name of object to look for
11
11
  #
12
12
  def put_object(object, file = nil, options = {})
13
- bucket = options[:bucket]
14
- bucket ||= @aliyun_oss_bucket
15
- location = get_bucket_location(bucket)
16
- endpoint = 'http://' + location + '.aliyuncs.com'
17
- return put_folder(bucket, object, endpoint) if file.nil?
18
-
19
- # put multiparts if object's size is over 100m
20
- return put_multipart_object(bucket, object, file) if file.size > 104_857_600
21
-
22
- body = file.read
23
-
24
- resource = bucket + '/' + object
25
- request(
26
- expects: [200, 203],
27
- method: 'PUT',
28
- path: object,
29
- bucket: bucket,
30
- resource: resource,
31
- body: body,
32
- endpoint: endpoint
33
- )
13
+ bucket_name = options[:bucket]
14
+ bucket_name ||= @aliyun_oss_bucket
15
+ bucket = @oss_client.get_bucket(bucket_name)
16
+ return bucket.put_object(object) if file.nil?
17
+ # With a single PUT operation you can upload objects up to 5 GB in size.
18
+ if file.size > 5_368_709_120
19
+ bucket.resumable_upload(object, file.path)
20
+ end
21
+ bucket.put_object(object, :file => file.path)
34
22
  end
35
23
 
36
24
  def put_object_with_body(object, body, options = {})
37
- bucket = options[:bucket]
38
- bucket ||= @aliyun_oss_bucket
39
- location = get_bucket_location(bucket)
40
- endpoint = 'http://' + location + '.aliyuncs.com'
25
+ bucket_name = options[:bucket]
26
+ bucket_name ||= @aliyun_oss_bucket
41
27
 
42
- resource = bucket + '/' + object
28
+ resource = bucket_name + '/' + object
43
29
  request(
44
30
  expects: [200, 203],
45
31
  method: 'PUT',
46
32
  path: object,
47
- bucket: bucket,
33
+ bucket: bucket_name,
48
34
  resource: resource,
49
- body: body,
50
- endpoint: endpoint
35
+ body: body
51
36
  )
52
37
  end
53
38
 
54
- def put_folder(bucket, folder, endpoint)
55
- if endpoint.nil?
56
- location = get_bucket_location(bucket)
57
- endpoint = 'http://' + location + '.aliyuncs.com'
58
- end
39
+ def put_folder(bucket, folder)
59
40
  path = folder + '/'
60
41
  resource = bucket + '/' + folder + '/'
61
42
  request(
@@ -63,28 +44,24 @@ module Fog
63
44
  method: 'PUT',
64
45
  path: path,
65
46
  bucket: bucket,
66
- resource: resource,
67
- endpoint: endpoint
47
+ resource: resource
68
48
  )
69
49
  end
70
50
 
71
51
  def put_multipart_object(bucket, object, file)
72
- location = get_bucket_location(bucket)
73
- endpoint = 'http://' + location + '.aliyuncs.com'
74
-
75
52
  # find the right uploadid
76
- uploads = list_multipart_uploads(bucket, endpoint)
53
+ uploads = list_multipart_uploads(bucket)
77
54
  upload = (uploads&.find { |tmpupload| tmpupload['Key'][0] == object })
78
55
 
79
56
  uploadedSize = 0
80
57
  start_partNumber = 1
81
58
  if !upload.nil?
82
59
  uploadId = upload['UploadId'][0]
83
- parts = list_parts(bucket, object, endpoint, uploadId)
60
+ parts = list_parts(bucket, object, uploadId)
84
61
  if !parts.nil? && !parts.empty?
85
62
  if parts[-1]['Size'][0].to_i != 5_242_880
86
63
  # the part is the last one, if its size is over 5m, then finish this upload
87
- complete_multipart_upload(bucket, object, endpoint, uploadId)
64
+ complete_multipart_upload(bucket, object, uploadId)
88
65
  return
89
66
  end
90
67
  uploadedSize = (parts[0]['Size'][0].to_i * (parts.size - 1)) + parts[-1]['Size'][0].to_i
@@ -92,11 +69,11 @@ module Fog
92
69
  end
93
70
  else
94
71
  # create upload ID
95
- uploadId = initiate_multipart_upload(bucket, object, endpoint)
72
+ uploadId = initiate_multipart_upload(bucket, object)
96
73
  end
97
74
 
98
75
  if file.size <= uploadedSize
99
- complete_multipart_upload(bucket, object, endpoint, uploadId)
76
+ complete_multipart_upload(bucket, object, uploadId)
100
77
  return
101
78
  end
102
79
 
@@ -105,17 +82,13 @@ module Fog
105
82
 
106
83
  for i in start_partNumber..end_partNumber
107
84
  body = file.read(5_242_880)
108
- upload_part(bucket, object, endpoint, i.to_s, uploadId, body)
85
+ upload_part(bucket, object, i.to_s, uploadId, body)
109
86
  end
110
87
 
111
- complete_multipart_upload(bucket, object, endpoint, uploadId)
88
+ complete_multipart_upload(bucket, object, uploadId)
112
89
  end
113
90
 
114
- def initiate_multipart_upload(bucket, object, endpoint)
115
- if endpoint.nil?
116
- location = get_bucket_location(bucket)
117
- endpoint = 'http://' + location + '.aliyuncs.com'
118
- end
91
+ def initiate_multipart_upload(bucket, object)
119
92
  path = object + '?uploads'
120
93
  resource = bucket + '/' + path
121
94
  ret = request(
@@ -123,17 +96,12 @@ module Fog
123
96
  method: 'POST',
124
97
  path: path,
125
98
  bucket: bucket,
126
- resource: resource,
127
- endpoint: endpoint
99
+ resource: resource
128
100
  )
129
101
  XmlSimple.xml_in(ret.data[:body])['UploadId'][0]
130
102
  end
131
103
 
132
- def upload_part(bucket, object, endpoint, partNumber, uploadId, body)
133
- if endpoint.nil?
134
- location = get_bucket_location(bucket)
135
- endpoint = 'http://' + location + '.aliyuncs.com'
136
- end
104
+ def upload_part(bucket, object, partNumber, uploadId, body)
137
105
  path = object + '?partNumber=' + partNumber + '&uploadId=' + uploadId
138
106
  resource = bucket + '/' + path
139
107
  request(
@@ -142,17 +110,12 @@ module Fog
142
110
  path: path,
143
111
  bucket: bucket,
144
112
  resource: resource,
145
- body: body,
146
- endpoint: endpoint
113
+ body: body
147
114
  )
148
115
  end
149
116
 
150
- def complete_multipart_upload(bucket, object, endpoint, uploadId)
151
- if endpoint.nil?
152
- location = get_bucket_location(bucket)
153
- endpoint = 'http://' + location + '.aliyuncs.com'
154
- end
155
- parts = list_parts(bucket, object, endpoint, uploadId, options = {})
117
+ def complete_multipart_upload(bucket, object, uploadId)
118
+ parts = list_parts(bucket, object, uploadId, options = {})
156
119
  request_part = []
157
120
  return if parts.empty?
158
121
  for i in 0..(parts.size - 1)
@@ -169,7 +132,6 @@ module Fog
169
132
  path: path,
170
133
  bucket: bucket,
171
134
  resource: resource,
172
- endpoint: endpoint,
173
135
  body: body
174
136
  )
175
137
  end
@@ -1,13 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'xmlsimple'
4
+ require 'aliyun/oss'
5
+
6
+ # Using Aliyun OSS Ruby SDK
7
+ AliyunOssSdk= Aliyun::OSS
4
8
 
5
9
  module Fog
6
- module Storage
7
- class Aliyun < Fog::Service
10
+ module Aliyun
11
+ class Storage < Fog::Service
8
12
  DEFAULT_REGION = 'cn-hangzhou'
9
13
 
10
- DEFAULT_SCHEME = 'http'
14
+ DEFAULT_SCHEME = 'https'
11
15
  DEFAULT_SCHEME_PORT = {
12
16
  'http' => 80,
13
17
  'https' => 443
@@ -97,6 +101,11 @@ module Fog
97
101
  @port = uri.port || DEFAULT_SCHEME_PORT[@scheme]
98
102
 
99
103
  @persistent = options[:persistent] || false
104
+ @oss_client = AliyunOssSdk::Client.new(
105
+ :endpoint => @aliyun_oss_endpoint,
106
+ :access_key_id => @aliyun_accesskey_id,
107
+ :access_key_secret => @aliyun_accesskey_secret
108
+ )
100
109
  end
101
110
 
102
111
  def reload
@@ -117,34 +126,22 @@ module Fog
117
126
  time = Time.new.utc
118
127
  date = time.strftime('%a, %d %b %Y %H:%M:%S GMT')
119
128
 
120
- endpoint = params[:endpoint]
121
- if endpoint
122
- uri = URI.parse(endpoint)
123
- host = uri.host
124
- path = uri.path
125
- port = uri.port
126
- scheme = uri.scheme
127
- else
128
- host = @host
129
- path = @path
130
- port = @port
131
- scheme = @scheme
132
- end
133
-
134
129
  bucket = params[:bucket]
135
130
  tmpHost = if bucket
136
- bucket + '.' + host
131
+ bucket + '.' + @host
137
132
  else
138
- host
133
+ @host
139
134
  end
140
135
 
141
- @connection = Fog::Core::Connection.new("#{scheme}://#{tmpHost}", @persistent, @connection_options)
136
+ @connection = Fog::Core::Connection.new("#{@scheme}://#{tmpHost}", @persistent, @connection_options)
142
137
  contentType = params[:contentType]
143
138
 
144
139
  begin
145
140
  headers = ''
146
- params[:headers]&.each do |k, v|
141
+ if params[:headers]
142
+ params[:headers].each do |k, v|
147
143
  headers += "#{k}:#{v}\n" if k != 'Range'
144
+ end
148
145
  end
149
146
  signature = sign(method, date, contentType, params[:resource], headers)
150
147
  response = @connection.request(params.merge(headers: {
@@ -152,12 +149,12 @@ module Fog
152
149
  'Authorization' => 'OSS ' + @aliyun_accesskey_id + ':' + signature,
153
150
  'Date' => date
154
151
  }.merge!(params[:headers] || {}),
155
- path: "#{path}/#{params[:path]}",
152
+ path: "#{@path}/#{params[:path]}",
156
153
  query: params[:query]))
157
154
  rescue Excon::Errors::HTTPStatusError => error
158
155
  raise case error
159
156
  when Excon::Errors::NotFound
160
- Fog::Storage::Aliyun::NotFound.slurp(error)
157
+ Fog::Aliyun::Storage::NotFound.slurp(error)
161
158
  else
162
159
  error
163
160
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Fog
4
4
  module Aliyun
5
- VERSION = '0.3.10'
5
+ VERSION = '0.3.16'
6
6
  end
7
7
  end
@@ -5,7 +5,7 @@ class Aliyun < Fog::Bin
5
5
  def class_for(key)
6
6
  case key
7
7
  when :storage
8
- Fog::Storage::Aliyun
8
+ Fog::Aliyun::Storage
9
9
  when :compute
10
10
  Fog::Compute::Aliyun
11
11
  else