|
| 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() |
0 commit comments