Obtain an upload URL and credentials for a media asset

Updated at:

After reading this article, you will learn the sample code for obtaining the media upload URL and credential by calling the OpenAPI of the server SDK.

Sample code

Use the Alibaba Cloud OpenAPI Developer Portal to debug this API online.

import OpenApi, * as $OpenApi from '@alicloud/openapi-client';
import Credential, { Config } from '@alicloud/credentials';
const Client = require('@alicloud/ice20201109').default;

// An AccessKey of an Alibaba Cloud account provides full access to all API operations. For better security, use a Resource Access Management (RAM) user for API calls and routine O&M.
// This example shows how to store the AccessKey ID and AccessKey secret in environment variables. For more information about the configuration, see https://help.aliyun.com/document_detail/378664.html.

const cred = new Credential();
const iceClient = new Client(new $OpenApi.Config({
  credential: cred,
  endpoint: 'ice.cn-shanghai.aliyuncs.com'
}));

// The following code shows how to hard-code the AccessKey ID and AccessKey secret. For security reasons, do not hard-code your AccessKey ID and AccessKey secret in your project code. Hard coding your AccessKey pair may cause it to be leaked, which compromises the security of all resources in your account.
// const iceClient = new Client(new $OpenApi.Config({
//   accessKeyId: '<yourAccessKeyId>',
//   accessKeySecret: '<yourAccessKeySecret>',
//   endpoint: 'ice.cn-shanghai.aliyuncs.com'
// }));

// Obtain the upload URL and credentials for the media asset.
iceClient.createUploadMedia({
    fileInfo: "{\"Type\":\"video\",\"Name\":\"test\",\"Size\":123,\"Ext\":\"mp4\"}",
    mediaMetaData: "{\"Title\":\"UploadTest\",\"Description\":\"UploadTest\",\"BusinessType\":\"general\"}",
    uploadTargetConfig: "{\"StorageType\":\"oss\",\"StorageLocation\":\"testbucket.oss-cn-shanghai.aliyuncs.com\"}",
}).then(function (data) {
    console.log(data.body);
}, function (err) {
    console.log('Error:' + err);
});

Related API operations

CreateUploadMedia - Obtain an upload URL and credentials for a media asset