O OSS não oferece suporte à renomeação direta de objetos. Para renomear um objeto, execute uma operação de cópia seguida de exclusão: chame CopyObject para criar um novo objeto com o nome desejado e, em seguida, chame DeleteObject para remover o objeto de origem.
Observações
Este tópico utiliza o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços Alibaba Cloud na mesma região, use um endpoint interno. Para obter mais informações sobre regiões e endpoints do OSS, consulte Regions and endpoints.
Neste tópico, as credenciais de acesso são obtidas de variáveis de ambiente. Para obter mais informações sobre como configurar credenciais de acesso, consulte Configure access credentials.
Este tópico demonstra como criar uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como usar um domínio personalizado ou autenticar com credenciais do Security Token Service (STS), consulte Configure a client (Go SDK V1).
Código de exemplo
O código a seguir mostra como renomear o objeto srcobject.txt para destobject.txt no 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)
}
Referências
Para obter mais informações sobre as operações de API usadas para renomear um objeto, consulte CopyObject e DeleteObject.