All Products
Search
Document Center

Object Storage Service:Asynchronous processing (Go SDK V2)

Last Updated:Aug 02, 2025

Asynchronous processing (x-oss-async-process) allows a program to execute other tasks without waiting for a task to complete. This topic explains how to use OSS SDK for Go V2 for asynchronous processing in scenarios such as document conversion, video transcoding, and video merging.

Limitations

  • The sample code in this topic uses the China (Hangzhou) region (ID: cn-hangzhou) and a public endpoint by default. If you want to access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see OSS regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For how to configure access credentials, see Configure access credentials.

Method

func (c *Client) AsyncProcessObject(ctx context.Context, request *AsyncProcessObjectRequest, optFns ...func(*Options)) (*AsyncProcessObjectResult, error)

Request parameters

Parameter

Type

Description

ctx

context.Context

The context of the request, which can be used to specify the total duration of the request

request

*AsyncProcessObjectRequest

Specify the request parameters for the specific interface. For more information, see AsyncProcessObjectRequest

optFns

...func(*Options)

(Optional) Interface-level configuration parameters. For more information, see Options

Return value list

Return value name

Type

Description

result

*AsyncProcessObjectResult

The return value of the interface, valid when err is nil. For more information, see AsyncProcessObjectResult

err

error

The status of the request. If the request fails, the value of err is not nil

Sample code

The following code shows how to use the Go SDK V2 for document format conversion to achieve the desired output type.

package main

import (
	"context"
	"encoding/base64"
	"flag"
	"fmt"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

var (
	region     string
	bucketName string 
	objectName string
)

// The init function is executed before the main function to initialize the program
func init() {
	// Use a command line parameter to specify the region
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	// Use a command line parameter to specify the bucket name
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
	// Use a command line parameter to specify the name of the document to be converted
	flag.StringVar(&objectName, "object", "", "The name of the object.")
}

func main() {
	flag.Parse() // Parse command line parameters

	// Check whether the region is specified. If the region is not specified, output the default parameters and exit the program
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}
	// Check whether the bucket name is specified. If the bucket name is not specified, output the default parameters and exit the program
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}
	// Check whether the object name is specified. If the object name is not specified, output the default parameters and exit the program
	if len(objectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, object name required")
	}

	// Create a configuration object and use environment variables as the credential provider and the specified region
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg) // Create a new OSS client using the configuration

	// Specify the name of the output file
	targetKey := "dest.png"

	// Create a style variable of the string type to store document conversion parameters
	animationStyle := "doc/convert,target_png,source_docx" // Define the processing rule to convert the source Docx document to a PNG image

	// Create a processing instruction, in which the storage path, the Base64-encoded bucket name, and the captured frame names are included
	bucketNameEncoded := base64.URLEncoding.EncodeToString([]byte(bucketName))
	targetKeyEncoded := base64.URLEncoding.EncodeToString([]byte(targetKey))
	process := fmt.Sprintf("%s|sys/saveas,b_%v,o_%v/notify", animationStyle, bucketNameEncoded, targetKeyEncoded)

	// Create an AsyncProcessObject request to initiate asynchronous processing of a specific object
	request := &oss.AsyncProcessObjectRequest{
		Bucket:       oss.Ptr(bucketName), // Specify the name of the bucket to operate
		Key:          oss.Ptr(objectName), // Specify the name of the document to process
		AsyncProcess: oss.Ptr(process),    
	}

	// Execute the request to asynchronously process the object and receive the return result
	result, err := client.AsyncProcessObject(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to async process object %v", err)
	}

	log.Printf("async process object result:%#v\n", result)
}

Common scenarios

Video transcoding

Use the video transcoding feature to alter the video encoding format, reduce resolution and bitrate to decrease video file size, and convert the video container format.

package main

import (
	"context"
	"encoding/base64"
	"flag"
	"fmt"
	"log"
	"strings"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

var (
	region     string // Specify a variable to store the region information obtained from the command line
	bucketName string // Specify a variable to store the bucket name obtained from the command line
	objectName string // Specify a variable to store the object name obtained from the command line
)

func init() {
	// Use a command line parameter to specify the region
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	// Use a command line parameter to specify the bucket name
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
	// Use a command line parameter to specify the name of the video to be transcoded
	flag.StringVar(&objectName, "object", "", "The name of the object.")
}

func main() {
	flag.Parse() // Parse command line parameters

	// Check whether the region is specified. If the region is not specified, output the default parameters and exit the program
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}
	// Check whether the bucket name is specified. If the bucket name is not specified, output the default parameters and exit the program
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}
	// Check whether the object name is specified. If the object name is not specified, output the default parameters and exit the program
	if len(objectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, object name required")
	}

	// Create a configuration object and use environment variables as the credential provider and the specified region
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg) // Create a new OSS client using the configuration

	// Specify the name of the output video
	targetObject := "dest.avi"

	// Define the processing style. This is a sample video conversion configuration, including format, video codec, resolution, bitrate, frame rate, audio codec, audio bitrate, and other parameters
	style := "video/convert,f_avi,vcodec_h265,s_1920x1080,vb_2000000,fps_30,acodec_aac,ab_100000,sn_1"

	// Construct an asynchronous processing instruction, including the processing style and the save location of the processed file (the bucket name and object name are Base64-encoded)
	process := fmt.Sprintf("%s|sys/saveas,b_%v,o_%v",
		style,
		strings.TrimRight(base64.URLEncoding.EncodeToString([]byte(bucketName)), "="),   // Base64-encode the bucket name and remove the trailing '='
		strings.TrimRight(base64.URLEncoding.EncodeToString([]byte(targetObject)), "=")) // Base64-encode the object name and remove the trailing '='

	// Create an AsyncProcessObject request to initiate asynchronous processing of a specific object
	request := &oss.AsyncProcessObjectRequest{
		Bucket:       oss.Ptr(bucketName), // Specify the name of the bucket to operate
		Key:          oss.Ptr(objectName), // Specify the name of the video to process
		AsyncProcess: oss.Ptr(process),
	}

	// Execute the request to asynchronously process the object and receive the return result
	result, err := client.AsyncProcessObject(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to async process object %v", err)
	}

	log.Printf("async process object result:%#v\n", result)
}

Video-to-animated-image conversion

Convert videos to animated images in GIF or WebP format using video-to-animated-image conversion.

package main

import (
	"context"
	"encoding/base64"
	"flag"
	"fmt"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

var (
	region     string // Specify a variable to store the region information obtained from the command line
	bucketName string // Specify a variable to store the bucket name obtained from the command line
	objectName string // Specify a variable to store the object name obtained from the command line
)

// The init function is executed before the main function to initialize the program
func init() {
	// Use a command line parameter to specify the region
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	// Use a command line parameter to specify the bucket name
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
	// Use a command line parameter to specify the name of the video to be processed
	flag.StringVar(&objectName, "object", "", "The name of the object.")
}

func main() {
	flag.Parse() // Parse command line parameters

	// Check whether the region is specified. If the region is not specified, output the default parameters and exit the program
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}
	// Check whether the bucket name is specified. If the bucket name is not specified, output the default parameters and exit the program
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}
	// Check whether the object name is specified. If the object name is not specified, output the default parameters and exit the program
	if len(objectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, object name required")
	}

	// Create a configuration object and use environment variables as the credential provider and the specified region
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg) // Create a new OSS client using the configuration

	// Specify the name of the output GIF animated image file
	targetKey := "destexample.gif"

	// Define the parameters for converting video to GIF animated images, including GIF width, height, and frame interval
	animationStyle := "video/animation,f_gif,w_100,h_100,inter_1000"

	// Create a processing instruction, in which the storage path, the Base64-encoded bucket name, and the captured frame names are included
	bucketNameEncoded := base64.URLEncoding.EncodeToString([]byte(bucketName))
	targetKeyEncoded := base64.URLEncoding.EncodeToString([]byte(targetKey))
	process := fmt.Sprintf("%s|sys/saveas,b_%v,o_%v/notify,topic_QXVkaW9Db252ZXJ0", animationStyle, bucketNameEncoded, targetKeyEncoded)

	// Create an AsyncProcessObject request to initiate asynchronous processing of a specific object
	request := &oss.AsyncProcessObjectRequest{
		Bucket:       oss.Ptr(bucketName), // Specify the name of the bucket to operate
		Key:          oss.Ptr(objectName), // Specify the name of the video to process
		AsyncProcess: oss.Ptr(process),    
	}

	// Execute the request to asynchronously process the object and receive the return result
	result, err := client.AsyncProcessObject(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to async process object %v", err)
	}

	log.Printf("async process object result:%#v\n", result)
}

Video sprite capture

Capture video frames and stitch them into a sprite using the video sprite capture feature based on specific rules.

package main

import (
	"context"
	"encoding/base64"
	"flag"
	"fmt"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

var (
	region     string // Specify a variable to store the region information obtained from the command line
	bucketName string // Specify a variable to store the bucket name obtained from the command line
	objectName string // Specify a variable to store the object name obtained from the command line
)

// The init function is executed before the main function to initialize the program
func init() {
	// Use a command line parameter to specify the region
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	// Use a command line parameter to specify the bucket name
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
	// Use a command line parameter to specify the name of the video to be processed
	flag.StringVar(&objectName, "object", "", "The name of the object.")
}

func main() {
	flag.Parse() // Parse command line parameters

	// Check whether the region is specified. If the region is not specified, output the default parameters and exit the program
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}
	// Check whether the bucket name is specified. If the bucket name is not specified, output the default parameters and exit the program
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}
	// Check whether the object name is specified. If the object name is not specified, output the default parameters and exit the program
	if len(objectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, object name required")
	}

	// Create a configuration object and use environment variables as the credential provider and the specified region
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg) // Create a new OSS client using the configuration

	// Specify the name of the output sprite file
	targetKey := "example.jpg"

	// Construct the parameters for video sprite capture
	animationStyle := "video/sprite,f_jpg,sw_100,sh_100,inter_10000,tw_10,th_10,pad_0,margin_0"

	// Create a processing instruction, in which the storage path, the Base64-encoded bucket name, and the captured frame names are included
	bucketNameEncoded := base64.URLEncoding.EncodeToString([]byte(bucketName))
	targetKeyEncoded := base64.URLEncoding.EncodeToString([]byte(targetKey))
	process := fmt.Sprintf("%s|sys/saveas,b_%v,o_%v/notify,topic_QXVkaW9Db252ZXJ0", animationStyle, bucketNameEncoded, targetKeyEncoded)

	// Create an AsyncProcessObject request to initiate asynchronous processing of a specific object
	request := &oss.AsyncProcessObjectRequest{
		Bucket:       oss.Ptr(bucketName), // Specify the name of the bucket to operate
		Key:          oss.Ptr(objectName), // Specify the name of the object to process
		AsyncProcess: oss.Ptr(process),    
	}

	// Execute the request to asynchronously process the object and receive the return result
	result, err := client.AsyncProcessObject(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to async process object %v", err)
	}

	log.Printf("async process object result:%#v\n", result)
}

Frame capture

Capture video frames as needed and convert them to a specific format with frame capture.

package main

import (
	"context"
	"encoding/base64"
	"flag"
	"fmt"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

var (
	region     string // Specify a variable to store the region information obtained from the command line
	bucketName string // Specify a variable to store the bucket name obtained from the command line
	objectName string // Specify a variable to store the object name obtained from the command line
)

// The init function is executed before the main function to initialize the program
func init() {
	// Use a command line parameter to specify the region
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	// Use a command line parameter to specify the bucket name
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
	// Use a command line parameter to specify the name of the video to be processed
	flag.StringVar(&objectName, "object", "", "The name of the object.")
}

func main() {
	flag.Parse() // Parse command line parameters

	// Check whether the region is specified. If the region is not specified, output the default parameters and exit the program
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}
	// Check whether the bucket name is specified. If the bucket name is not specified, output the default parameters and exit the program
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}
	// Check whether the object name is specified. If the object name is not specified, output the default parameters and exit the program
	if len(objectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, object name required")
	}

	// Create a configuration object and use environment variables as the credential provider and the specified region
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg) // Create a new OSS client using the configuration

	// Specify the name of the output file after frame capture
	targetKey := "dest.png"

	// Construct the parameters for frame capture
	animationStyle := "video/snapshots,f_jpg,w_100,h_100,scaletype_crop,inter_10000"

	// Create a processing instruction, in which the storage path, the Base64-encoded bucket name, and the captured frame names are included
	bucketNameEncoded := base64.URLEncoding.EncodeToString([]byte(bucketName))
	targetKeyEncoded := base64.URLEncoding.EncodeToString([]byte(targetKey))
	process := fmt.Sprintf("%s|sys/saveas,b_%v,o_%v/notify,topic_QXVkaW9Db252ZXJ0", animationStyle, bucketNameEncoded, targetKeyEncoded)

	// Create an AsyncProcessObject request to initiate asynchronous processing of a specific object
	request := &oss.AsyncProcessObjectRequest{
		Bucket:       oss.Ptr(bucketName), // Specify the name of the bucket to operate
		Key:          oss.Ptr(objectName), // Specify the name of the object to process
		AsyncProcess: oss.Ptr(process),    
	}

	// Execute the request to asynchronously process the object and receive the return result
	result, err := client.AsyncProcessObject(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to async process object %v", err)
	}

	log.Printf("async process object result:%#v\n", result)
}

Video merging

Merge multiple videos into a single video file in the specified format using the video merging feature.

package main

import (
	"context"
	"encoding/base64"
	"flag"
	"fmt"
	"log"
	"strings"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

var (
	region     string // Specify a variable to store the region information obtained from the command line
	bucketName string // Specify a variable to store the bucket name obtained from the command line
)

// The init function is executed before the main function to initialize the program
func init() {
	// Use a command line parameter to specify the region
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	// Use a command line parameter to specify the bucket name
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	flag.Parse() // Parse command line parameters

	// Check whether the region is specified. If the region is not specified, output the default parameters and exit the program
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}
	// Check whether the bucket name is specified. If the bucket name is not specified, output the default parameters and exit the program
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Create a configuration object and use environment variables as the credential provider and the specified region
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg) // Create a new OSS client using the configuration

	// Specify the name of the output video file
	targetObject := "dest.mp4"
	// Specify the names of the video files to be merged
	video1 := "concat1.mp4"
	video2 := "concat2.mp4"

	// Create a style variable of the string type to store video merging parameters
	style := fmt.Sprintf("video/concat,ss_0,f_mp4,vcodec_h264,fps_25,vb_1000000,acodec_aac,ab_96000,ar_48000,ac_2,align_1/pre,o_%s/sur,o_%s,t_0", strings.TrimRight(base64.URLEncoding.EncodeToString([]byte(video1)), "="), strings.TrimRight(base64.URLEncoding.EncodeToString([]byte(video2)), "="))
	// Create an asynchronous processing instruction
	process := fmt.Sprintf("%s|sys/saveas,b_%v,o_%v/notify,topic_QXVkaW9Db252ZXJ0", style, strings.TrimRight(base64.URLEncoding.EncodeToString([]byte(bucketName)), "="), strings.TrimRight(base64.URLEncoding.EncodeToString([]byte(targetObject)), "="))

	// Create an AsyncProcessObject request to initiate asynchronous processing of a specific object
	request := &oss.AsyncProcessObjectRequest{
		Bucket:       oss.Ptr(bucketName), // Specify the name of the bucket to operate
		AsyncProcess: oss.Ptr(process),    
	}

	// Execute the request to asynchronously process the object and receive the return result
	result, err := client.AsyncProcessObject(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to async process object %v", err)
	}

	log.Printf("async process object result:%#v\n", result)
}

Audio transcoding

Convert audio files from one format to another with audio transcoding.

package main

import (
	"context"
	"encoding/base64"
	"flag"
	"fmt"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

var (
	region     string // Specify a variable to store the region information obtained from the command line
	bucketName string // Specify a variable to store the bucket name obtained from the command line
	objectName string // Specify a variable to store the object name obtained from the command line
)

// The init function is executed before the main function to initialize the program
func init() {
	// Use a command line parameter to specify the region
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	// Use a command line parameter to specify the bucket name
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
	// Use a command line parameter to specify the name of the audio to be processed
	flag.StringVar(&objectName, "object", "", "The name of the object.")
}

func main() {
	flag.Parse() // Parse command line parameters

	// Check whether the region is specified. If the region is not specified, output the default parameters and exit the program
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}
	// Check whether the bucket name is specified. If the bucket name is not specified, output the default parameters and exit the program
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}
	// Check whether the object name is specified. If the object name is not specified, output the default parameters and exit the program
	if len(objectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, object name required")
	}

	// Create a configuration object and use environment variables as the credential provider and the specified region
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg) // Create a new OSS client using the configuration

	// Specify the name of the output audio
	targetKey := "dest.aac"

	// Create a style variable of the string type to store audio transcoding parameters
	animationStyle := "audio/convert,ss_10000,t_60000,f_aac,ab_96000"

	// Create a processing instruction, in which the storage path, the Base64-encoded bucket name, and the captured frame names are included
	bucketNameEncoded := base64.URLEncoding.EncodeToString([]byte(bucketName))
	targetKeyEncoded := base64.URLEncoding.EncodeToString([]byte(targetKey))
	process := fmt.Sprintf("%s|sys/saveas,b_%v,o_%v/notify,topic_QXVkaW9Db252ZXJ0", animationStyle, bucketNameEncoded, targetKeyEncoded)

	// Create an AsyncProcessObject request to initiate asynchronous processing of a specific object
	request := &oss.AsyncProcessObjectRequest{
		Bucket:       oss.Ptr(bucketName), // Specify the name of the bucket to operate
		Key:          oss.Ptr(objectName), // Specify the name of the audio to process
		AsyncProcess: oss.Ptr(process),    
	}

	// Execute the request to asynchronously process the object and receive the return result
	result, err := client.AsyncProcessObject(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to async process object %v", err)
	}

	log.Printf("async process object result:%#v\n", result)
}

Audio merging

Merge multiple audio objects into a single audio object in the specified format with audio merging.

package main

import (
	"context"
	"encoding/base64"
	"flag"
	"fmt"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

var (
	region     string // Specify a variable to store the region information obtained from the command line
	bucketName string // Specify a variable to store the bucket name obtained from the command line
)

// The init function is executed before the main function to initialize the program
func init() {
	// Use a command line parameter to specify the region
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	// Use a command line parameter to specify the bucket name
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	flag.Parse() // Parse command line parameters

	// Check whether the region is specified. If the region is not specified, output the default parameters and exit the program
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}
	// Check whether the bucket name is specified. If the bucket name is not specified, output the default parameters and exit the program
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Create a configuration object and use environment variables as the credential provider and the specified region
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg) // Create a new OSS client using the configuration

	// Specify the audio objects to be merged
	audio1 := "src1.mp3"
	audio2 := "src2.mp3"
	// Specify the name of the output audio
	targetAudio := "dest.aac"

	// Create a style variable of the string type to store audio merging parameters
	audio1Encoded := base64.URLEncoding.EncodeToString([]byte(audio1))
	audio2Encoded := base64.URLEncoding.EncodeToString([]byte(audio2))
	style := fmt.Sprintf("audio/concat,f_aac,ac_1,ar_44100,ab_96000,align_2/pre,o_%s/pre,o_%s,t_0", audio1Encoded, audio2Encoded)

	// Create an asynchronous processing instruction
	bucketEncoded := base64.URLEncoding.EncodeToString([]byte(bucketName))
	targetEncoded := base64.URLEncoding.EncodeToString([]byte(targetAudio))
	process := fmt.Sprintf("%s|sys/saveas,b_%s,o_%s/notify,topic_QXVkaW9Db252ZXJ0", style, bucketEncoded, targetEncoded)

	// Create an AsyncProcessObject request to initiate asynchronous processing of a specific object
	request := &oss.AsyncProcessObjectRequest{
		Bucket:       oss.Ptr(bucketName), // Specify the name of the bucket to operate
		AsyncProcess: oss.Ptr(process),    
	}

	// Execute the request to asynchronously process the object and receive the return result
	result, err := client.AsyncProcessObject(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to async process object %v", err)
	}

	log.Printf("async process object result:%#v\n", result)
}

Parse image blind watermark

The code below demonstrates how to parse a blind watermark in an image.

package main

import (
	"context"
	"encoding/base64"
	"flag"
	"fmt"
	"log"
	"strings"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

var (
	region     string
	bucketName string
	objectName string
)

// The init function is executed before the main function to initialize the program
func init() {
	// Use a command line parameter to specify the region
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	// Use a command line parameter to specify the bucket name
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
	// Use a command line parameter to specify the name of the image to be processed
	flag.StringVar(&objectName, "object", "", "The name of the object.")
}

func main() {
	flag.Parse() // Parse command line parameters

	// Check whether the region is specified. If the region is not specified, output the default parameters and exit the program
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}
	// Check whether the bucket name is specified. If the bucket name is not specified, output the default parameters and exit the program
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}
	// Check whether the object name is specified. If the object name is not specified, output the default parameters and exit the program
	if len(objectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, object name required")
	}

	// Create a configuration object and use environment variables as the credential provider and the specified region
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg) // Create a new OSS client using the configuration

	// Specify the name of the watermark image file
	sourceKey := objectName // Specify the name of the image object to process

	// Specify the topic of the MNS message
	topic := "imm-blindwatermark-test"

	// Extract the watermark content from the specified image
	style := "image/deblindwatermark,s_low,t_text"
	encodedTopic := strings.TrimRight(base64.URLEncoding.EncodeToString([]byte(topic)), "=")
	process := fmt.Sprintf("%s|sys/notify,topic_%s", style, encodedTopic)
	// Create an AsyncProcessObject request to initiate asynchronous processing of a specific object
	request := &oss.AsyncProcessObjectRequest{
		Bucket:       oss.Ptr(bucketName), // Specify the name of the bucket to operate
		Key:          oss.Ptr(sourceKey),  // Specify the name of the image to process
		AsyncProcess: oss.Ptr(process),    
	}

	// Execute the request to asynchronously process the object and receive the return result
	result, err := client.AsyncProcessObject(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to async process object %v", err)
	}

	log.Printf("async process object result:%#v\n", result)
}

References