Skip to content

Commit 250fc37

Browse files
author
x00403408
committed
first upload
1 parent 2ba6c26 commit 250fc37

37 files changed

+9355
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
# huaweicloud-sdk-python-obs
1+
2+
Version: 3.0.5
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
#!/usr/bin/python
2+
# -*- coding:utf-8 -*-
3+
4+
'''
5+
This sample demonstrates how to do bucket-related operations
6+
(such as do bucket ACL/CORS/Lifecycle/Logging/Website/Location/Tagging/OPTIONS)
7+
on OBS using the OBS SDK for Python.
8+
'''
9+
10+
AK = '*** Provide your Access Key ***'
11+
SK = '*** Provide your Secret Key ***'
12+
server = 'https://your-endpoint'
13+
14+
bucketName = 'my-obs-bucket-demo'
15+
16+
from obs import *
17+
# Constructs a obs client instance with your account for accessing OBS
18+
obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server)
19+
20+
bucketClient = obsClient.bucketClient(bucketName)
21+
22+
def createBucket():
23+
# resp = obsClient.createBucket(bucketName)
24+
resp = bucketClient.createBucket()
25+
if resp.status < 300:
26+
print('Create bucket:' + bucketName + ' successfully!\n')
27+
else:
28+
print(resp.errorCode)
29+
30+
def getBucketLocation():
31+
# resp = obsClient.getBucketLocation(bucketName)
32+
resp = bucketClient.getBucketLocation()
33+
if resp.status < 300:
34+
print('Getting bucket location ' + str(resp.body) + ' \n')
35+
else:
36+
print(resp.errorCode)
37+
38+
def getBucketStorageInfo():
39+
# resp = obsClient.getBucketStorageInfo(bucketName)
40+
resp = bucketClient.getBucketStorageInfo()
41+
if resp.status < 300:
42+
print('Getting bucket storageInfo ' + str(resp.body) + ' \n')
43+
else:
44+
print(resp.errorCode)
45+
46+
def doBucketQuotaOperation():
47+
# Set bucket quota to 1GB
48+
# obsClient.setBucketQuota(bucketName, 1024 * 1024 * 1024)
49+
bucketClient.setBucketQuota(1024*1024*1024)
50+
# resp = obsClient.getBucketQuota(bucketName)
51+
resp = bucketClient.getBucketQuota()
52+
53+
print('Getting bucket quota ' + str(resp.body) + ' \n')
54+
55+
def doBucketVersioningOperation():
56+
# print('Getting bucket versioning config ' + str(obsClient.getBucketVersioning(bucketName).body) + ' \n')
57+
print('Getting bucket versioning config ' + str(bucketClient.getBucketVersioning().body) + ' \n')
58+
# Enable bucket versioning
59+
# obsClient.setBucketVersioning(bucketName, 'Enabled')
60+
bucketClient.setBucketVersioning('Enabled')
61+
print('Current bucket versioning config ' + str(obsClient.getBucketVersioning(bucketName).body) + ' \n')
62+
63+
# Suspend bucket versioning
64+
# obsClient.setBucketVersioning(bucketName, 'Suspended')
65+
bucketClient.setBucketVersioning('Suspended')
66+
print('Current bucket versioning config ' + str(obsClient.getBucketVersioning(bucketName).body) + ' \n')
67+
68+
def doBucketAclOperation():
69+
print('Setting bucket ACL to public-read \n')
70+
# obsClient.setBucketAcl(bucketName, aclControl='public-read')
71+
bucketClient.setBucketAcl(aclControl='public-read')
72+
73+
# print('Getting bucket ACL ' + str(obsClient.getBucketAcl(bucketName).body) + ' \n')
74+
print('Getting bucket ACL ' + str(bucketClient.getBucketAcl().body) + ' \n')
75+
print('Setting bucket ACL to private \n')
76+
obsClient.setBucketAcl(bucketName, None, 'private')
77+
78+
print('Getting bucket ACL ' + str(obsClient.getBucketAcl(bucketName).body) + ' \n')
79+
80+
81+
def doBucketCorsOperation():
82+
print('Setting bucket CORS\n')
83+
cors1 = CorsRule(id='rule1', allowedMethod=['PUT', 'HEAD', 'GET'],
84+
allowedOrigin=['http://www.a.com', 'http://www.b.com'], allowedHeader=['Authorization1'],
85+
maxAgeSecond=100, exposeHeader=['x-obs-test1'])
86+
cors2 = CorsRule(id='rule2', allowedMethod=['PUT', 'HEAD', 'GET'],
87+
allowedOrigin=['http://www.c.com', 'http://www.d.com'], allowedHeader=['Authorization2'],
88+
maxAgeSecond=200, exposeHeader=['x-obs-test2'])
89+
90+
corsList = [cors1, cors2]
91+
92+
# obsClient.setBucketCors(bucketName, corsList)
93+
bucketClient.setBucketCors(corsList)
94+
95+
# print('Getting bucket CORS ' + str(obsClient.getBucketCors(bucketName).body) + '\n')
96+
print('Getting bucket CORS ' + str(bucketClient.getBucketCors().body) + '\n')
97+
98+
def optionsBucket():
99+
print('Options bucket \n')
100+
option = Options(origin='http://www.a.com', accessControlRequestMethods=['GET', 'PUT'],
101+
accessControlRequestHeaders=['Authorization1'])
102+
# print('\t' + str(obsClient.optionsBucket(bucketName, option).body))
103+
print('\t' + str(bucketClient.optionsBucket(option).body))
104+
105+
def getBucketMetadata():
106+
print('Getting bucket metadata\n')
107+
108+
# resp = obsClient.getBucketMetadata(bucketName, origin='http://www.b.com', requestHeaders='Authorization1')
109+
resp = bucketClient.getBucketMetadata(origin='http://www.b.com', requestHeaders='Authorization1')
110+
print('storageClass:', resp.body.storageClass)
111+
print('accessContorlAllowOrigin:', resp.body.accessContorlAllowOrigin)
112+
print('accessContorlMaxAge:', resp.body.accessContorlMaxAge)
113+
print('accessContorlExposeHeaders:', resp.body.accessContorlExposeHeaders)
114+
print('accessContorlAllowMethods:', resp.body.accessContorlAllowMethods)
115+
print('accessContorlAllowHeaders:', resp.body.accessContorlAllowHeaders)
116+
117+
print('Deleting bucket CORS\n')
118+
# obsClient.deleteBucketCors(bucketName)
119+
resp = bucketClient.deleteBucketCors()
120+
print('status' + str(resp.status))
121+
122+
def doBucketLifycleOperation():
123+
print('Setting bucket lifecycle\n')
124+
125+
rule1 = Rule(id='delete obsoleted files', prefix='obsoleted/', status='Enabled', expiration=Expiration(days=10))
126+
rule2 = Rule(id='delete temporary files', prefix='temporary/', status='Enabled', expiration=Expiration(date=DateTime(2017, 12, 31)))
127+
rule3 = Rule(id='delete temp files', prefix='temp/', status='Enabled', noncurrentVersionExpiration=NoncurrentVersionExpiration(noncurrentDays=10))
128+
129+
Llifecycle = Lifecycle(rule=[rule1, rule2, rule3])
130+
# obsClient.setBucketLifecycle(bucketName, Llifecycle)
131+
bucketClient.setBucketLifecycle(Llifecycle)
132+
133+
print('Getting bucket lifecycle:')
134+
# resp = obsClient.getBucketLifecycle(bucketName)
135+
resp = bucketClient.getBucketLifecycle()
136+
print('\t' + str(resp.body) + '\n')
137+
138+
print('Deleting bucket lifecyle\n')
139+
# obsClient.deleteBucketLifecycle(bucketName)
140+
bucketClient.deleteBucketLifecycle()
141+
142+
def doBucketLoggingOperation():
143+
print('Setting bucket logging\n')
144+
145+
# obsClient.setBucketLogging(bucketName, Logging(targetBucket=bucketName, targetPrefix='log-', agency='your agency'))
146+
bucketClient.setBucketLogging(Logging(targetBucket=bucketName, targetPrefix='log-', agency='your agency'))
147+
148+
print('Getting bucket logging:')
149+
# print('\t' + str(obsClient.getBucketLogging(bucketName).body) + '\n')
150+
print('\t' + str(bucketClient.getBucketLogging().body) + '\n')
151+
152+
print('Deleting bucket logging\n')
153+
obsClient.setBucketLogging(bucketName, Logging())
154+
155+
print('Getting bucket logging:')
156+
print('\t' + str(obsClient.getBucketLogging(bucketName).body) + '\n')
157+
158+
def doBucketWebsiteOperation():
159+
print('Setting bucket website\n')
160+
Lwebsite = WebsiteConfiguration(indexDocument=IndexDocument(suffix='index.html'), errorDocument=ErrorDocument(key='error.html'))
161+
# obsClient.setBucketWebsite(bucketName, Lwebsite)
162+
bucketClient.setBucketWebsite(Lwebsite)
163+
164+
print('Getting bucket website:')
165+
# print('\t' + str(obsClient.getBucketWebsite(bucketName).body) + '\n')
166+
print('\t' + str(bucketClient.getBucketWebsite().body) + '\n')
167+
print('Deleting bucket website\n')
168+
# obsClient.deleteBucketWebsite(bucketName)
169+
bucketClient.deleteBucketWebsite()
170+
171+
def doBucketTaggingOperation():
172+
print('Setting bucket tagging\n')
173+
tagInfo = TagInfo()
174+
tagInfo.addTag('key1', 'value1').addTag('key2', 'value2')
175+
# resp = obsClient.setBucketTagging(bucketName, tagInfo)
176+
resp = bucketClient.setBucketTagging(tagInfo)
177+
178+
if resp.status < 300:
179+
print('Getting bucket tagging\n')
180+
# resp = obsClient.getBucketTagging(bucketName)
181+
resp = bucketClient.getBucketTagging()
182+
for item in resp.body.tagSet:
183+
print('\t' + item.key + ':' + item.value + '\n')
184+
185+
print('Deleting bucket tagging\n')
186+
# obsClient.deleteBucketTagging(bucketName)
187+
bucketClient.deleteBucketTagging()
188+
else:
189+
print('common msg:status:', resp.status, ',errorCode:', resp.errorCode, ',errorMessage:', resp.errorMessage)
190+
191+
def deleteBucket():
192+
print('Deleting bucket ' + bucketName + '\n')
193+
# resp = obsClient.deleteBucket(bucketName)
194+
resp = bucketClient.deleteBucket()
195+
print('common msg:status:', resp.status, ',errorCode:', resp.errorCode, ',errorMessage:', resp.errorMessage)
196+
197+
198+
# Put bucket operation
199+
createBucket()
200+
201+
# Get bucket location operation
202+
getBucketLocation()
203+
204+
# Get bucket storageInfo operation
205+
getBucketStorageInfo()
206+
207+
# Put/Get bucket quota operations
208+
doBucketQuotaOperation()
209+
210+
# Put/Get bucket versioning operations
211+
doBucketVersioningOperation()
212+
213+
# Put/Get bucket acl operations
214+
doBucketAclOperation()
215+
216+
# Put/Get/Delete bucket cors operations
217+
doBucketCorsOperation()
218+
219+
# Options bucket operation
220+
optionsBucket()
221+
222+
# Get bucket metadata operation
223+
getBucketMetadata()
224+
225+
# Put/Get/Delete bucket lifecycle operations
226+
doBucketLifycleOperation()
227+
228+
# Put/Get/Delete bucket logging operations
229+
doBucketLoggingOperation()
230+
231+
# Put/Get/Delete bucket website operations
232+
doBucketWebsiteOperation()
233+
234+
# Put/Get/Delete bucket tagging operations
235+
doBucketTaggingOperation()
236+
237+
# Delete bucket operation
238+
deleteBucket()
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/usr/bin/python
2+
# -*- coding:utf-8 -*-
3+
4+
'''
5+
This sample demonstrates how to multipart upload an object concurrently by copy mode
6+
to OBS using the OBS SDK for Python.
7+
'''
8+
9+
AK = '*** Provide your Access Key ***'
10+
SK = '*** Provide your Secret Key ***'
11+
server = 'https://your-endpoint'
12+
bucketName = 'my-obs-bucket-demo'
13+
sourceBucketName = bucketName
14+
sourceObjectKey = 'my-obs-object-key-demo'
15+
objectKey = sourceObjectKey + '-back'
16+
sampleFilePath = '*** Provide your local file path ***'
17+
18+
import platform, os, threading, multiprocessing
19+
IS_WINDOWS = platform.system() == 'Windows' or os.name == 'nt'
20+
21+
def createSampleFile(sampleFilePath):
22+
if not os.path.exists(sampleFilePath):
23+
_dir = os.path.dirname(sampleFilePath)
24+
if not os.path.exists(_dir):
25+
os.makedirs(_dir, mode=0o755)
26+
import uuid
27+
index = 1000000
28+
with open(sampleFilePath, 'w') as f:
29+
while index >= 0:
30+
f.write(str(uuid.uuid1()) + '\n')
31+
f.write(str(uuid.uuid4()) + '\n')
32+
index -= 1
33+
return sampleFilePath
34+
35+
36+
from obs import *
37+
38+
def doCopyPart(partETags, bucketName, objectKey, partNumber, uploadId, copySource, copySourceRange):
39+
if IS_WINDOWS:
40+
global obsClient
41+
else:
42+
obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server)
43+
resp = obsClient.copyPart(bucketName=bucketName, objectKey=objectKey, partNumber=partNumber, uploadId=uploadId, copySource=copySource, copySourceRange=copySourceRange)
44+
if resp.status < 300:
45+
partETags[partNumber] = resp.body.etag
46+
print('Part#', partNumber, 'done\n')
47+
48+
if __name__ == '__main__':
49+
# Constructs a obs client instance with your account for accessing OBS
50+
obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server)
51+
# Create bucket
52+
print('Create a new bucket for demo\n')
53+
obsClient.createBucket(bucketName)
54+
55+
# # Upload an object to your source bucket
56+
print('Uploading a new object to OBS from a file\n')
57+
obsClient.putFile(sourceBucketName, sourceObjectKey, sampleFilePath)
58+
59+
# Claim a upload id firstly
60+
resp = obsClient.initiateMultipartUpload(bucketName, objectKey)
61+
uploadId = resp.body.uploadId
62+
print('Claiming a new upload id ' + uploadId + '\n')
63+
64+
# 5MB
65+
partSize = 5 * 1024 * 1024
66+
resp = obsClient.getObjectMetadata(sourceBucketName, sourceObjectKey)
67+
header = dict(resp.header)
68+
objectSize = int(header.get('content-length'))
69+
70+
partCount = int(objectSize / partSize) if (objectSize % partSize == 0) else int(objectSize / partSize) + 1
71+
72+
if partCount > 10000:
73+
raise Exception('Total parts count should not exceed 10000')
74+
75+
print('Total parts count ' + str(partCount) + '\n')
76+
77+
# Upload multiparts by copy mode
78+
print('Begin to upload multiparts to OBS by copy mode \n')
79+
80+
proc = threading.Thread if IS_WINDOWS else multiprocessing.Process
81+
82+
partETags = dict() if IS_WINDOWS else multiprocessing.Manager().dict()
83+
84+
processes = []
85+
86+
for i in range(partCount):
87+
rangeStart = i * partSize
88+
rangeEnd = objectSize - 1 if (i + 1 == partCount) else rangeStart + partSize - 1
89+
90+
p = proc(target=doCopyPart, args=(partETags, bucketName, objectKey, i+1, uploadId, sourceBucketName + '/' + sourceObjectKey, str(rangeStart) + '-' + str(rangeEnd)))
91+
p.daemon = True
92+
processes.append(p)
93+
94+
for p in processes:
95+
p.start()
96+
97+
98+
for p in processes:
99+
p.join()
100+
101+
if len(partETags) != partCount:
102+
raise Exception('Upload multiparts fail due to some parts are not finished yet')
103+
104+
# View all parts uploaded recently
105+
print('Listing all parts......')
106+
resp = obsClient.listParts(bucketName, objectKey, uploadId)
107+
for part in resp.body.parts:
108+
print('\tPart#' + str(part.partNumber) + ', ETag=' + part.etag)
109+
print('\n')
110+
111+
# Complete to upload multiparts
112+
113+
partETags = sorted(partETags.items(), key=lambda d: d[0])
114+
115+
parts = []
116+
for key, value in partETags:
117+
parts.append(CompletePart(partNum=key, etag=value))
118+
119+
print('Completing to upload multiparts\n')
120+
resp = obsClient.completeMultipartUpload(bucketName, objectKey, uploadId, CompleteMultipartUploadRequest(parts))
121+
if resp.status < 300:
122+
print('Succeed to complete multiparts into an object named ' + objectKey + '\n')

0 commit comments

Comments
 (0)