Par défaut, si vous téléchargez un objet portant le même nom qu'un objet existant, ce dernier est écrasé. Cette rubrique explique comment configurer l'en-tête de requête x-oss-forbid-overwrite afin d'empêcher l'écrasement des objets existants lors de la copie d'objets ou de l'exécution d'un téléchargement simple ou multipartie.
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.
Dans cette rubrique, les identifiants d'accès 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 illustre la création d'une instance
OSSClientavec un endpoint 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).
Exemple de code
L'exemple de code suivant montre comment empêcher l'écrasement d'objets existants par des objets portant le même nom lors d'un téléchargement simple :
package main
import (
"log"
"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 {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// 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 {
log.Fatalf("Failed to create OSS client: %v", err)
}
// Specify the name of your bucket.
bucketName := "yourBucketName"
bucket, err := client.Bucket(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
}
// Specify whether to overwrite an existing object with the same name.
// By default, if you do not specify oss.ForbidOverWrite, an object that has the same name is overwritten.
// If you set oss.ForbidOverWrite to false, an object that has the same name is overwritten.
// If you set oss.ForbidOverWrite to true, an object that has the same name is not overwritten. If such an object exists, an error is reported.
forbidWrite := oss.ForbidOverWrite(true)
// Upload the string.
// Specify the full path of the object. Do not include the bucket name in the full path.
objectName := "yourObjectName"
objectValue := "yourObjectValue"
err = bucket.PutObject(objectName, strings.NewReader(objectValue), forbidWrite)
if err != nil {
log.Fatalf("Failed to upload object '%s': %v", objectName, err)
}
log.Printf("Successfully uploaded object '%s' with value '%s'", objectName, objectValue)
}
Scénarios courants
Empêcher l'écrasement lors d'une tâche de copie d'objet
Empêcher l'écrasement lors d'un téléchargement multipartie
Références
Pour plus d'informations sur l'opération API permettant d'effectuer un téléchargement simple, consultez PutObject.
Pour plus d'informations sur l'opération API permettant de copier un objet, consultez CopyObject.
Pour plus d'informations sur les opérations API permettant d'effectuer un téléchargement multipartie, consultez InitiateMultipartUpload et CompleteMultipartUpload.