Synchronous processing (x-oss-process) applies image and document transformations to objects already stored in OSS and saves the results — all in a single blocking call. Use it when you need the processed output before continuing program execution.
Prerequisites
Before you begin, make sure you have:
An OSS bucket containing the object to process
Access credentials configured as environment variables (
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRET)The OSS Go SDK V2 installed (
github.com/aliyun/alibabacloud-oss-go-sdk-v2)
Usage notes
The examples in this document use the China (Hangzhou) region (
cn-hangzhou) with a public endpoint. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. See OSS regions and endpoints for the full list.Credentials are read from environment variables. See Configure access credentials for other authentication options.
Method definition
func (c *Client) ProcessObject(ctx context.Context, request *ProcessObjectRequest, optFns ...func(*Options)) (*ProcessObjectResult, error)Request parameters
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Request context. Use this to set a total timeout for the request. |
request | *ProcessObjectRequest | Request parameters. See ProcessObjectRequest. |
optFns | ...func(*Options) | (Optional) Operation-level configuration. See Options. |
Return values
| Return value | Type | Description |
|---|---|---|
result | *ProcessObjectResult | The processing result. Valid only when err is nil. See ProcessObjectResult. |
err | error | nil on success; non-nil if the request fails. |
Resize an image and save the result
The Process field combines transformation directives with sys/saveas to save the output in one call. The target object key and bucket name in sys/saveas must be URL-safe Base64-encoded (o_ prefix for target object key, b_ prefix for target bucket name).
The following example resizes an image to a fixed 100x100 px and saves the result to the same 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
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
flag.StringVar(&objectName, "object", "", "The name of the object.")
}
func main() {
flag.Parse()
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(objectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Initialize the OSS client with credentials from environment variables.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg)
// Target bucket must be in the same region as the source bucket.
targetBucketName := bucketName
// If the target object is not in the bucket root, include the full path.
targetImageName := "exampledir/example.jpg"
// Build the process string:
// image/resize,m_fixed,w_100,h_100 — resize to a fixed 100x100 px
// sys/saveas,o_<base64>,b_<base64> — save the result
// o_: target object key (URL-safe Base64-encoded)
// b_: target bucket name (URL-safe Base64-encoded)
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)),
)
request := &oss.ProcessObjectRequest{
Bucket: oss.Ptr(bucketName),
Key: oss.Ptr(objectName),
Process: oss.Ptr(process),
}
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)
}Add a blind watermark
The following example embeds a text blind watermark into an image using the blindwatermark directive.
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
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
flag.StringVar(&objectName, "object", "", "The name of the object.")
}
func main() {
flag.Parse()
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(objectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Initialize the OSS client with credentials from environment variables.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg)
// Build the process string:
// image/blindwatermark,content_<base64> — embed a text blind watermark
// content_: watermark text (URL-safe Base64-encoded)
// sys/saveas,o_<base64>,b_<base64> — save the result
// o_: target object key (URL-safe Base64-encoded)
// b_: target bucket name (URL-safe Base64-encoded)
content := "Copyright Alibaba Cloud"
targetImageName := "targetDir/target.jpg"
style := fmt.Sprintf("image/blindwatermark,content_%s",
base64.URLEncoding.EncodeToString([]byte(content)),
)
process := fmt.Sprintf("%s|sys/saveas,o_%s,b_%s",
style,
base64.URLEncoding.EncodeToString([]byte(targetImageName)),
base64.URLEncoding.EncodeToString([]byte(bucketName)),
)
request := &oss.ProcessObjectRequest{
Bucket: oss.Ptr(bucketName),
Key: oss.Ptr(objectName),
Process: oss.Ptr(process),
}
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)
}What's next
Image processing parameters — full reference for all
x-oss-processdirectivesSynchronous processing — feature overview and limits
ProcessObject API reference — complete API details on pkg.go.dev