对象存储服务 OBS-生成带授权信息的URL(Python SDK):代码样例

时间:2024-12-24 19:57:19

代码样例

此用例用于生成带授权信息的临时URL。

 1
 2
 3
 4
 5
 6
 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
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
from obs import ObsClient
import os
import traceback
import base64

# 推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险
# 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
ak = os.getenv("AccessKeyID")
sk = os.getenv("SecretAccessKey")
# 【可选】如果使用临时AKSK和SecurityToken访问OBS,则同样推荐通过环境变量获取
# security_token = os.getenv("SecurityToken")
# server填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写
server = "https://obs.cn-north-4.myhuaweicloud.com"
# 创建obsClient实例
# 如果使用临时AKSK和SecurityToken访问OBS,需要在创建实例时通过security_token参数指定securityToken值
obsClient = ObsClient(access_key_id=ak, secret_access_key=sk, server=server)
try:
    # 生成创建桶的带授权信息的URL
    res1 = obsClient.createSignedUrl(method='PUT', bucketName='bucketname', expires=3600)
    print('signedUrl:', res1.signedUrl)
    print('actualSignedRequestHeaders:', res1.actualSignedRequestHeaders)

    # 生成上传对象的带授权信息的URL
    res2 = obsClient.createSignedUrl(method='PUT', bucketName='bucketname', objectKey='objectkey', expires=3600,
                                     headers={'Content-Type': 'text/plain'})
    print('signedUrl:', res2.signedUrl)
    print('actualSignedRequestHeaders:', res2.actualSignedRequestHeaders)

    # 生成设置对象ACL的带授权信息的URL
    res3 = obsClient.createSignedUrl(method='PUT', bucketName='bucketname', objectKey='objectkey', specialParam='acl',
                                     expires=3600, headers={'x-obs-acl': 'private'})
    print('signedUrl:', res3.signedUrl)
    print('actualSignedRequestHeaders:', res3.actualSignedRequestHeaders)

    # 生成下载对象的带授权信息的URL
    res4 = obsClient.createSignedUrl(method='GET', bucketName='bucketname', objectKey='objectkey', expires=3600)
    print('signedUrl:', res4.signedUrl)
    print('actualSignedRequestHeaders:', res4.actualSignedRequestHeaders)

    # 生成删除对象的带授权信息的URL
    res5 = obsClient.createSignedUrl(method='DELETE', bucketName='bucketname', objectKey='objectkey', expires=3600)
    print('signedUrl:', res5.signedUrl)
    print('actualSignedRequestHeaders:', res5.actualSignedRequestHeaders)

    # 生成删除桶的带授权信息的URL
    res6 = obsClient.createSignedUrl(method='DELETE', bucketName='bucketname', expires=3600)
    print('signedUrl:', res6.signedUrl)
    print('actualSignedRequestHeaders:', res6.actualSignedRequestHeaders)

    # 生成设置初始化分段任务的带授权信息的URL
    res7 = obsClient.createSignedUrl(method='POST', bucketName='bucketname', objectKey='objectkey',
                                     specialParam='uploads', expires=3600)
    print('signedUrl:', res7.signedUrl)
    print('actualSignedRequestHeaders:', res7.actualSignedRequestHeaders)

    # 生成上传段的带授权信息的URL,
    res8 = obsClient.createSignedUrl(method='PUT', bucketName='bucketname', objectKey='objectkey', expires=3600,
                                     queryParams={'partNumber': '1', 'uploadId': '00000*****'})
    print('signedUrl:', res8.signedUrl)
    print('actualSignedRequestHeaders:', res8.actualSignedRequestHeaders)

    # 生成合并段的带授权信息的URL
    res9 = obsClient.createSignedUrl(method='POST', bucketName='bucketname', objectKey='objectkey', expires=3600,
                                     queryParams={'uploadId': '00000*****'})
    print('signedUrl:', res9.signedUrl)
    print('actualSignedRequestHeaders:', res9.actualSignedRequestHeaders)

    # 生成图片持久化处理的带授权信息的URL
    # 源对象所在的桶名
    bucketName = 'originBucketName'; 
    # 处理前的源对象名
    objectKey = 'test.png';

    # 处理后的对象名
    targetObjectName ="save.png"
    # 处理后保存的桶名, 可选参数
    targetBucketName ="saveBucketName"
    queryParams={}
    queryParams["x-image-process"]="image/resize,w_100"
    queryParams["x-image-save-object"]=base64.b64encode(targetObjectName .encode("utf-8")).decode()
     # 可选参数
    queryParams["x-image-save-bucket"]=base64.b64encode(targetBucketName .encode("utf-8")).decode()

    res10 = obsClient.createSignedUrl(method='GET', bucketName=bucketName, objectKey=objectKey, queryParams=queryParams, expires=3600)
    print('signedUrl:', res10.signedUrl)
    print('actualSignedRequestHeaders:', res10.actualSignedRequestHeaders)
except:
    print(traceback.format_exc())
support.huaweicloud.com/sdk-python-devg-obs/obs_22_1301.html