Tous les produits
Search
Centre de documentation

Object Storage Service:Renommer un fichier (Go SDK V1)

Dernière mise à jour :Sep 22, 2026

OSS ne prend pas en charge le renommage direct des objets. Pour renommer un objet, copiez-le puis supprimez l'original : appelez CopyObject pour créer un nouvel objet avec le nom souhaité, puis appelez DeleteObject pour supprimer l'objet source.

Notes

  • Cette rubrique utilise l'endpoint 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 endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez Régions et endpoints.

  • Les identifiants d'accès sont récupérés depuis les variables d'environnement. Pour plus d'informations sur la configuration des identifiants d'accès, consultez Configurer les identifiants d'accès.

  • Cette rubrique montre comment créer une instance OSSClient avec un endpoint OSS. Pour d'autres configurations, comme l'utilisation d'un domaine personnalisé ou l'authentification via le Security Token Service (STS), consultez Configurer un client (Go SDK V1).

Exemple de code

Le code suivant renomme l'objet srcobject.txt en destobject.txt dans le bucket examplebucket.

package main

import (
	"fmt"
	"log"

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

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the Endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual Endpoint.
	// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Set the signature version.
	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 bucket name.
	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// The full path of the source object. The full path cannot contain the bucket name.
	srcObject := "srcobject.txt"
	// The full path of the destination object. The full path cannot contain the bucket name.
	destObject := "destobject.txt"

	// Copy srcobject.txt to destobject.txt in the same bucket.
	_, err = bucket.CopyObject(srcObject, destObject)
	if err != nil {
		log.Fatalf("Failed to copy object '%s' to '%s': %v", srcObject, destObject, err)
	}

	// Delete srcobject.txt.
	err = bucket.DeleteObject(srcObject)
	if err != nil {
		log.Fatalf("Failed to delete source object '%s': %v", srcObject, err)
	}

	fmt.Printf("%s has been renamed to %s\n", srcObject, destObject)
}

Références

Pour plus d'informations sur les opérations API utilisées pour renommer un objet, consultez CopyObject et DeleteObject.