OSS no admite cambiar el nombre de los objetos directamente. Para hacerlo, copie el objeto y luego elimine el original: llame a CopyObject para crear un nuevo objeto con el nombre deseado y después llame a DeleteObject para eliminar el objeto de origen.
Notas
En este tema se utiliza el endpoint público de la región China (Hangzhou). Si necesita acceder a OSS desde otros servicios de Alibaba Cloud en la misma región que OSS, utilice un endpoint interno. Para obtener más información sobre las regiones y los endpoints de OSS, consulte Regions and endpoints.
En este tema, las credenciales de acceso se obtienen de variables de entorno. Para obtener más información sobre cómo configurar las credenciales de acceso, consulte Configure access credentials.
Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para conocer configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Configure a client (Go SDK V1).
Código de ejemplo
A continuación se muestra un ejemplo de cómo cambiar el nombre del objeto srcobject.txt a destobject.txt en el 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)
}
Referencias
Para obtener más información sobre las operaciones de API que se utilizan para cambiar el nombre de un objeto, consulte CopyObject y DeleteObject.