上传回调是指客户端在请求时携带回调(Callback)参数,服务端在上传完成后,发送同步的 POST 回调请求到 CallBack 中指定的第三方应用服务器,在服务器确认接受并返回结果后,才将所有结果返回给客户端。
关于上传回调的详细介绍,请参见上传回调。
import os from io import StringIO import base64 import json import tos # 从环境变量获取 AK 和 SK 信息。 ak = os.getenv('TOS_ACCESS_KEY') sk = os.getenv('TOS_SECRET_KEY') # your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例,your endpoint 填写 tos-cn-beijing.volces.com,your region 填写 cn-beijing。 endpoint = "your endpoint" region = "your region" bucket_name = "bucket-test" # 对象名称,例如 example_dir 下的 example_object.txt 文件,则填写为 example_dir/example_object.txt object_key = "object-test" content = StringIO('Hello TOS') callback_url = "*** Provide your callback url ***" try: # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现 client = tos.TosClientV2(ak, sk, endpoint, region) callback_param = { "callbackUrl": callback_url, "callbackBody": "{\"bucket\": ${bucket}, \"object\": ${object}, \"key1\": ${x:key1}}", "callbackBodyType": "application/json", } callback = json.dumps(callback_param) callback_var_param = {"x:key1": "ceshi"} callback_var = json.dumps(callback_var_param) result = client.put_object(bucket_name, object_key, content=content, callback=base64.b64encode(callback.encode('utf-8')).decode('utf-8'), callback_var=base64.b64encode(callback_var.encode('utf-8')).decode('utf-8')) # HTTP状态码 print('http status code:{}'.format(result.status_code)) # 请求ID。请求ID是本次请求的唯一标识,建议在日志中添加此参数 print('request_id: {}'.format(result.request_id)) except tos.exceptions.TosClientError as e: # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常 print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause)) except tos.exceptions.TosServerError as e: # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息 print('fail with server error, code: {}'.format(e.code)) # request id 可定位具体问题,强烈建议日志中保存 print('error with request id: {}'.format(e.request_id)) print('error with message: {}'.format(e.message)) print('error with http code: {}'.format(e.status_code)) print('error with ec: {}'.format(e.ec)) print('error with request url: {}'.format(e.request_url)) except Exception as e: print('fail with unknown error: {}'.format(e))
import os import base64 import json import tos # 从环境变量获取 AK 和 SK 信息。 ak = os.getenv('TOS_ACCESS_KEY') sk = os.getenv('TOS_SECRET_KEY') # your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例,your endpoint 填写 tos-cn-beijing.volces.com,your region 填写 cn-beijing。 endpoint = "your endpoint" region = "your region" bucket_name = "bucket-test" # 对象名称,例如 example_dir 下的 example_object.txt 文件,则填写为 example_dir/example_object.txt object_key = "object-test" upload_id = "*** Provide your upload id ***" callback_url = "*** Provide your callback url ***" try: # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现 client = tos.TosClientV2(ak, sk, endpoint, region) callback_param = { "callbackUrl": callback_url, "callbackBody": "{\"bucket\": ${bucket}, \"object\": ${object}, \"key1\": ${x:key1}}", "callbackBodyType": "application/json", } callback = json.dumps(callback_param) callback_var_param = {"x:key1": "ceshi"} callback_var = json.dumps(callback_var_param) result = client.complete_multipart_upload(bucket_name, object_key, upload_id=upload_id, callback=base64.b64encode(callback.encode('utf-8')).decode('utf-8'), callback_var=base64.b64encode(callback_var.encode('utf-8')).decode('utf-8')) # HTTP状态码 print('http status code:{}'.format(result.status_code)) # 请求ID。请求ID是本次请求的唯一标识,建议在日志中添加此参数 print('request_id: {}'.format(result.request_id)) except tos.exceptions.TosClientError as e: # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常 print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause)) except tos.exceptions.TosServerError as e: # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息 print('fail with server error, code: {}'.format(e.code)) # request id 可定位具体问题,强烈建议日志中保存 print('error with request id: {}'.format(e.request_id)) print('error with message: {}'.format(e.message)) print('error with http code: {}'.format(e.status_code)) print('error with ec: {}'.format(e.ec)) print('error with request url: {}'.format(e.request_url)) except Exception as e: print('fail with unknown error: {}'.format(e))