In synchronous processing (x-oss-process), a program must wait for a task to complete before it can execute other tasks. This topic describes how to use Go SDK V2 for synchronous processing in scenarios such as image processing and document processing.
Usage notes
The sample code in this topic uses the China (Hangzhou) region (region ID:
cn-hangzhou) as an example. By default, a public endpoint is used. If you want to access OSS from other Alibaba Cloud products in the same region, use an internal endpoint. For more information about the mappings between OSS-supported regions and endpoints, see OSS regions and endpoints.This topic provides an example of obtaining access credentials from environment variables. For more information about how to configure access credentials, see Configure access credentials.
Method definition
func (c *Client) ProcessObject(ctx context.Context, request *ProcessObjectRequest, optFns ...func(*Options)) (*ProcessObjectResult, error)Request parameters
Parameter | Type | Description |
ctx | context.Context | The context of the request. You can use this parameter to specify the total timeout period for the request. |
request | *ProcessObjectRequest | The request parameters for the specific API operation. For more information, see ProcessObjectRequest. |
optFns | ...func(*Options) | (Optional) The operation-level configuration parameters. For more information, see Options. |
Return values
Return value | Type | Description |
result | *ProcessObjectResult | The return value of the API operation. This parameter is valid only when err is nil. For more information, see ProcessObjectResult. |
err | error | The status of the request. If the request fails, err is not nil. |
Sample code
The following sample code shows how to use Go SDK V2 to scale an image and save the processed image to a specified bucket.
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() {
// Set a command-line parameter to specify the region.
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
// Set a command-line parameter to specify the bucket name.
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
// Set 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 information is provided. If not, print 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 provided. If not, print 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 provided. If not, print 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 bucket used to store the processed image. The bucket must be in the same region as the bucket that stores the source image.
targetBucketName := bucketName
// Specify the name of the processed image. If the image is not in the root directory of the bucket, you must include the full path of the file, for example, exampledir/example.jpg.
targetImageName := "exampledir/example.jpg"
// Resize the image to a fixed width and height of 100 px and save it to the specified bucket.
style := "image/resize,m_fixed,w_100,h_100"
process := fmt.Sprintf("%s|sys/saveas,o_%v,b_%v", style, base64.URLEncoding.EncodeToString([]byte(targetImageName)), base64.URLEncoding.EncodeToString([]byte(targetBucketName)))
// Build a ProcessObjectRequest to initiate synchronous processing for a specific object.
request := &oss.ProcessObjectRequest{
Bucket: oss.Ptr(bucketName), // Specify the name of the bucket to operate on.
Key: oss.Ptr(objectName), // Specify the name of the image to process.
Process: oss.Ptr(process), // Specify the processing instruction.
}
// Execute the request to synchronously process the object and receive the return value.
result, err := client.ProcessObject(context.TODO(), request)
if err != nil {
log.Fatalf("failed to process object %v", err)
}
log.Printf("process object result:%#v\n", result)
}
Scenarios
References
For more information about image processing parameters, see Image processing parameters.
For more information about the synchronous processing feature, see Synchronous processing.
For more information about the synchronous processing feature, see the ProcessObject API operation.