All Products
Search
Document Center

Object Storage Service:Examples for configuring upload callbacks by using OSS SDK for Go

Last Updated:Nov 01, 2024

Object Storage Service (OSS) can send callbacks to the application server when simple upload (PutObject and PutObjectFromFile) and multipart upload (UploadFile) tasks are completed. To configure upload callbacks, you need to only add the required callback parameters to the upload request that is sent to OSS.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions, endpoints and open ports.

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

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.

Examples

The following sample code provides an example on how to configure upload callbacks when you upload a string to the exampleobject.txt object in the examplefiles directory of the examplebucket bucket:

package main

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"os"
	"strings"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the version of the signature algorithm.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

        // Specify the name of the bucket. Example: examplebucket. 
	bucket, err := client.Bucket("examplebucket")
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Specify custom callback parameters. 
	callbackUrl := "http://your.callback.server.address" // Specify the URL of the callback server. 
	callbackMap := map[string]string{
		"callbackUrl": callbackUrl, // Specify the URL of the callback server. 
		"callbackBody": "bucket=${bucket}&object=${object}", // Specify the callback request body. 
		"callbackBodyType": "application/x-www-form-urlencoded", // Specify the type of the callback request body. 
	}

	// Convert the configurations of the callback parameters to a JSON string and encode the string in Base64. 
	callbackStr, err := json.Marshal(callbackMap)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	callbackBase64 := base64.StdEncoding.EncodeToString(callbackStr)

	// Specify the upload callback parameters. 
	options := []oss.Option{
		oss.Callback(callbackBase64),
	}

	// Construct a string that consists of 1024 x 1024 "a" character. 
	content := strings.Repeat("a", 1024*1024)

	// Upload the string. Do not include the bucket name in the object path. 
	objectPath := "examplefiles/exampleobject.txt"
	err = bucket.PutObject(objectPath, strings.NewReader(content), options...)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// A message which indicates the object is uploaded is returned. 
	fmt.Println("Object uploaded with callback successfully.")
}

References

  • For the complete sample code that is used to configure upload callbacks, visit GitHub.

  • For more information about the API operation that you can call to configure upload callbacks, see Callback.