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(®ion, "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
References
For more information about the asynchronous processing feature, see Asynchronous processing.
For more information about the API interface of the asynchronous processing feature, see AsyncProcessObject.