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.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/CHANGELOG.md +55 -0
- data/README.md +61 -3
- data/fog-aliyun.gemspec +4 -0
- data/lib/fog/aliyun.rb +6 -9
- data/lib/fog/aliyun/models/storage/directories.rb +4 -4
- data/lib/fog/aliyun/models/storage/directory.rb +4 -4
- data/lib/fog/aliyun/models/storage/file.rb +5 -4
- data/lib/fog/aliyun/models/storage/files.rb +50 -86
- data/lib/fog/aliyun/requests/storage/copy_object.rb +3 -6
- data/lib/fog/aliyun/requests/storage/delete_bucket.rb +3 -6
- data/lib/fog/aliyun/requests/storage/delete_container.rb +3 -6
- data/lib/fog/aliyun/requests/storage/delete_object.rb +9 -31
- data/lib/fog/aliyun/requests/storage/get_bucket.rb +13 -52
- data/lib/fog/aliyun/requests/storage/get_container.rb +2 -3
- data/lib/fog/aliyun/requests/storage/get_containers.rb +2 -3
- data/lib/fog/aliyun/requests/storage/get_object.rb +12 -25
- data/lib/fog/aliyun/requests/storage/get_object_http_url.rb +9 -9
- data/lib/fog/aliyun/requests/storage/get_object_https_url.rb +9 -9
- data/lib/fog/aliyun/requests/storage/head_object.rb +3 -6
- data/lib/fog/aliyun/requests/storage/list_buckets.rb +2 -2
- data/lib/fog/aliyun/requests/storage/list_objects.rb +11 -21
- data/lib/fog/aliyun/requests/storage/put_bucket.rb +2 -2
- data/lib/fog/aliyun/requests/storage/put_container.rb +3 -6
- data/lib/fog/aliyun/requests/storage/put_object.rb +31 -69
- data/lib/fog/aliyun/storage.rb +20 -23
- data/lib/fog/aliyun/version.rb +1 -1
- data/lib/fog/bin/aliyun.rb +1 -1
- metadata +59 -2
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Fog
|
4
|
-
module
|
5
|
-
class
|
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,18 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Fog
|
4
|
-
module
|
5
|
-
class
|
4
|
+
module Aliyun
|
5
|
+
class Storage
|
6
6
|
class Real
|
7
7
|
def list_objects(options = {})
|
8
|
-
bucket = options[
|
8
|
+
bucket = options['bucket']
|
9
9
|
bucket ||= @aliyun_oss_bucket
|
10
|
-
prefix = options[
|
11
|
-
marker = options[
|
10
|
+
prefix = options['prefix']
|
11
|
+
marker = options['marker']
|
12
12
|
# Set the ListObjects max limitation to 1000
|
13
|
-
maxKeys = options[
|
13
|
+
maxKeys = options['max-keys'] || 1000
|
14
14
|
maxKeys = [maxKeys, 1000].min
|
15
|
-
delimiter = options[
|
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,
|
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,
|
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
|
5
|
-
class
|
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
|
5
|
-
class
|
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
38
|
-
|
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 =
|
28
|
+
resource = bucket_name + '/' + object
|
43
29
|
request(
|
44
30
|
expects: [200, 203],
|
45
31
|
method: 'PUT',
|
46
32
|
path: object,
|
47
|
-
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
|
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
|
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,
|
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,
|
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
|
72
|
+
uploadId = initiate_multipart_upload(bucket, object)
|
96
73
|
end
|
97
74
|
|
98
75
|
if file.size <= uploadedSize
|
99
|
-
complete_multipart_upload(bucket, object,
|
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,
|
85
|
+
upload_part(bucket, object, i.to_s, uploadId, body)
|
109
86
|
end
|
110
87
|
|
111
|
-
complete_multipart_upload(bucket, object,
|
88
|
+
complete_multipart_upload(bucket, object, uploadId)
|
112
89
|
end
|
113
90
|
|
114
|
-
def initiate_multipart_upload(bucket, object
|
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,
|
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,
|
151
|
-
|
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
|
data/lib/fog/aliyun/storage.rb
CHANGED
@@ -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
|
7
|
-
class
|
10
|
+
module Aliyun
|
11
|
+
class Storage < Fog::Service
|
8
12
|
DEFAULT_REGION = 'cn-hangzhou'
|
9
13
|
|
10
|
-
DEFAULT_SCHEME = '
|
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]
|
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::
|
157
|
+
Fog::Aliyun::Storage::NotFound.slurp(error)
|
161
158
|
else
|
162
159
|
error
|
163
160
|
end
|
data/lib/fog/aliyun/version.rb
CHANGED