Tous les produits
Search
Centre de documentation

Object Storage Service:Upload callbacks (OSS SDK for Go 1.0)

Dernière mise à jour :Sep 22, 2026

Object Storage Service (OSS) peut envoyer des rappels à votre serveur d'application une fois les opérations de chargement simple (PutObject et PutObjectFromFile) et de chargement en plusieurs parties (UploadFile) terminées. Pour activer les rappels de chargement, ajoutez les paramètres de rappel requis à la requête de chargement envoyée à OSS.

Notes

  • Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez Régions et points de terminaison.

  • Les identifiants d'accès utilisés dans cette rubrique sont obtenus à partir des variables d'environnement. Pour savoir comment configurer les identifiants d'accès, consultez Configurer les identifiants d'accès.

  • Cette rubrique explique comment créer une instance OSSClient avec un point de terminaison OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Configurer un client (Go SDK V1).

Exemples

L'exemple suivant montre comment configurer les rappels de chargement lors du chargement d'une chaîne dans l'objet exampleobject.txt du répertoire examplefiles du compartiment examplebucket :

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.")
}

Références

  • Pour consulter l'exemple de code complet relatif aux rappels de chargement, accédez à GitHub.

  • Pour plus d'informations sur l'opération API relative aux rappels de chargement, consultez Callback.