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

You can debug this API operation online in the Alibaba Cloud OpenAPI Developer Portal.

package main

import (
    "fmt"
    openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
    ice "github.com/alibabacloud-go/ice-20201109/v2/client"
    "github.com/aliyun/credentials-go/credentials"
    "github.com/alibabacloud-go/tea/tea"
)

func main() {

    // An AccessKey of an Alibaba Cloud account provides full access to all API operations. For 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 method, see https://help.aliyun.com/document_detail/378661.html.
    credential, _err := credentials.NewCredential(nil)
    if _err != nil {
      panic(_err)
    }
    iceClient, _err := ice.NewClient(&openapi.Config{
      Credential: credential,
      Endpoint: tea.String("ice.cn-shanghai.aliyuncs.com"),
    })

    // The following code shows how to hard-code an AccessKey ID and an AccessKey secret. Do not hard-code the AccessKey ID and AccessKey secret in your project code. This practice can lead to an AccessKey leak, which compromises the security of all resources in your account.
    // config := new(openapi.Config)
    // config.SetAccessKeyId("<accessKeyId>").
    //     SetAccessKeySecret("<accessKeySecret>").
    //     SetRegionId("<regionId>").
    //     SetEndpoint("<endpoint>")
    // iceClient, _err := ice.NewClient(config)

    if _err != nil {
      panic(_err)
    }

    // Obtain the upload URL and credentials for the media asset.
    fileInfo := "{\"Type\":\"video\",\"Name\":\"test\",\"Size\":123,\"Ext\":\"mp4\"}"
    mediaMetaData := "{\"Title\":\"UploadTest\",\"Description\":\"UploadTest\",\"BusinessType\":\"general\"}"
    uploadTargetConfig := "{\"StorageType\":\"oss\",\"StorageLocation\":\"out-****.oss-cn-shanghai.aliyuncs.com\"}"
    request := &ice.CreateUploadMediaRequest{
        FileInfo:           &fileInfo,
        MediaMetaData:      &mediaMetaData,
        UploadTargetConfig: &uploadTargetConfig,
    }
    response, err := iceClient.CreateUploadMedia(request)
    fmt.Println(response, err)
}

Related API operations

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