post异步转码
// 对文件example.wav进行音频转码。 POST /exmaple.wav?x-oss-async-process HTTP/1.1 Host: video-demo.oss-cn-hangzhou.aliyuncs.com Date: Fri, 28 Oct 2022 06:40:10 GMT Authorization: OSS qn6q**************:77Dv**************** x-oss-async-process=audio/convert,f_opus,ab_96000,ar_48000,ac_2|sys/saveas,b_b3V0YnVja2V0, o_b3V0b2JqLnthdXRvZXh0fQo/notify,topic_QXVkaW9Db252ZXJ0python
# -*- coding: utf-8 -*-
import base64
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
def main():
# 从环境变量中获取访问凭证。运行本代码示例之前,请先配置环境变量。
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# 填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
# 指定阿里云通用Region ID,例如cn-hangzhou。
region = 'cn-hangzhou'
# 指定Bucket名称,例如examplebucket。
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', region=region)
# 指定原音频文件名称。
source_key = 'src.mp3'
# 指定转码后的音频文件。
target_key = 'dest.aac'
# 构建音频处理样式字符串以及音频转码处理参数。
animation_style = 'audio/convert,ss_10000,t_60000,f_aac,ab_96000'
# 构建处理指令,包括保存路径和Base64编码的Bucket名称和目标文件名称。
bucket_name_encoded = base64.urlsafe_b64encode('examplebucket'.encode()).decode().rstrip('=')
target_key_encoded = base64.urlsafe_b64encode(target_key.encode()).decode().rstrip('=')
process = f"{animation_style}|sys/saveas,b_{bucket_name_encoded},o_{target_key_encoded}/notify,topic_QXVkaW9Db252ZXJ0"
try:
# 执行异步处理任务。
result = bucket.async_process_object(source_key, process)
print(f"EventId: {result.event_id}")
print(f"RequestId: {result.request_id}")
print(f"TaskId: {result.task_id}")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main() 网友回复


