通过阅读本文,您可以了解通过服务端SDK调用OpenAPI获取媒资上传地址和凭证的示例代码。
示例代码
您可以通过阿里云OpenAPI开发者门户在线调试。
import json
import os
import sys
from typing import List
from alibabacloud_ice20201109.client import Client as ICE20201109Client
# 引入阿里云IMS SDK
from alibabacloud_ice20201109 import models as ice20201109_models
# Credentials和云产品SDK都需引入Client,此处为创建别名
from alibabacloud_credentials.client import Client as CredClient
# 引入阿里云SDK核心包
from alibabacloud_tea_openapi.models import Config
#######需要的依赖#############
#pip install alibabacloud_credentials
#pip install alibabacloud_ice20201109==1.3.11
class Sample:
# 初始化客户端
@staticmethod
def create_client(region: str) -> ICE20201109Client:
# 使用默认凭证初始化Credentials Client
cred = CredClient()
config = Config(credential = cred)
# 配置云产品服务接入地址(endpoint)
config.endpoint = 'ice.' + region + '.aliyuncs.com'
# 使用Credentials Client初始化ECS Client
return ICE20201109Client(config)
# @staticmethod
# def create_client(
# access_key_id: str,
# access_key_secret: str,
# region: str,
# ) -> ICE20201109Client:
# 如需硬编码AccessKey ID和AccessKey Secret,代码如下,但强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全
# config = open_api_models.Config(
# # 必填,您的 AccessKey ID,
# access_key_id = access_key_id,
# # 必填,您的 AccessKey Secret,
# access_key_secret = access_key_secret
# )
# # 访问的域名
# config.endpoint = 'ice.' + region + '.aliyuncs.com'
# return ICE20201109Client(config)
# 读取命令行参数
@staticmethod
def main() -> None:
region = 'cn-shanghai'
# 初始化客户端
client = Sample.create_client(region)
request = ice20201109_models.CreateUploadMediaRequest(
file_info='{"Type":"video","Name":"test","Size":123,"Ext":"mp4"}',
media_meta_data='{"Title":"UploadTest","Description":"UploadTest","BusinessType":"general"}',
upload_target_config='{"StorageType":"oss","StorageLocation":"outin-xxxxxx.oss-' + region + '.aliyuncs.com"}'
)
response = client.create_upload_media(request)
print('[LOG]', json.dumps(response.to_map()))
if __name__ == '__main__':
Sample.main()
相关接口
文档内容是否对您有帮助?