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"
"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 called before main to set up the command-line flags.
func init() {
// Sets a command-line flag for the region.
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
// Sets a command-line flag for the bucket name.
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
// Sets a command-line flag for the name of the document to convert.
flag.StringVar(&objectName, "object", "", "The name of the object.")
}
func main() {
flag.Parse() // Parses the command-line flags.
// Verifies that the region is provided. If not, prints default flags and exits.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Verifies that the bucket name is provided. If not, prints default flags and exits.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Verifies that the object name is provided. If not, prints default flags and exits.
if len(objectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Creates a configuration object that uses environment variables for credentials and the specified region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg) // Creates a new OSS client using the configuration.
// Specifies the name of the output file.
targetKey := "dest.png"
// Builds the style string for document processing and conversion.
animationStyle := "doc/convert,target_png,source_docx" // Defines the rule to convert the source .docx document to a .png image.
// Builds the processing instruction, including the save path and the Base64-encoded bucket name and target object name.
bucketNameEncoded := strings.TrimRight(base64.URLEncoding.EncodeToString([]byte(bucketName)), "=")
targetKeyEncoded := strings.TrimRight(base64.URLEncoding.EncodeToString([]byte(targetKey)), "=")
process := fmt.Sprintf("%s|sys/saveas,b_%v,o_%v/notify", animationStyle, bucketNameEncoded, targetKeyEncoded)
// Builds an AsyncProcessObjectRequest to initiate an asynchronous processing task.
request := &oss.AsyncProcessObjectRequest{
Bucket: oss.Ptr(bucketName), // Specifies the bucket to operate on.
Key: oss.Ptr(objectName), // Specifies the object to process.
AsyncProcess: oss.Ptr(process),
}
// Executes the request to process the object asynchronously and receive the result.
result, err := client.AsyncProcessObject(context.TODO(), request)
if err != nil {
log.Fatalf("failed to asynchronously 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.