由于 AK/SK 信息存放在客户端有泄漏风险,在客户端上传的过程中,通常需要服务端配合生成上传签名或凭证,下发给客户端。您可参考以下方法生成临时 Token。
服务端根据账号的 AK/SK 信息和相关的 policy 生成临时的上传密钥下发给客户端,客户端根据临时密钥和上传参数自行计算签名并发送 OpenAPI 请求。
调用接口前,请先完成 Python SDK 的安装及初始化操作。
# coding:utf-8 from __future__ import print_function from volcengine.imagex.v2.imagex_service import ImagexService if __name__ == '__main__': imagex_service = ImagexService() # call below method if you dont set ak and sk in $HOME/.volc/config imagex_service.set_ak('ak') imagex_service.set_sk('sk') # service id list allowed to do upload, set to empty if no restriction service_ids = ['imagex service id'] tag = dict() # tag 字段如下,可选择所需字段传入 # upload_policy_dict = { # "FileSizeUpLimit": "xxx", # "FileSizeBottomLimit": "xxx", # "ContentTypeBlackList":[ # "xxx" # ], # "ContentTypeWhiteList":[ # "xxx" # ] # } # policy_str = json.dumps(upload_policy_dict) # # tag = { # "UploadOverwrite": "true", # "UploadPolicy": policy_str, # } resp = imagex_service.get_upload_auth(service_ids, tag=tag) print(resp)