OSS identifies each object by its bucket name and object key. Because no single rename operation exists, renaming an object requires two steps: copy it to a new key, then delete the original.
How it works
To rename an object within the same bucket:
Call
CopyObjectto copy the source object to the destination key.Call
DeleteObjectto delete the source object only after the copy succeeds.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket with the source object
The
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables set with valid access credentials
Usage notes
The examples use the public endpoint for the China (Hangzhou) region (
https://oss-cn-hangzhou.aliyuncs.com). To access OSS from other Alibaba Cloud services in the same region, use the internal endpoint instead. For endpoint details, see Regions and endpoints.Access credentials are read from environment variables. For setup instructions, see Configure access credentials.
The OSSClient instance in these examples is created using an OSS endpoint. To use a custom domain name or Security Token Service (STS), see Configure OSSClient instances.
Rename an object
The following example renames srcobject.txt to destobject.txt in examplebucket. It copies the source object to the destination key, then deletes the source only if the copy succeeds.
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 your bucket's region.
// For example, for China (Hangzhou): https://oss-cn-hangzhou.aliyuncs.com
// Set yourRegion to the region ID of your bucket, for example: cn-hangzhou
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
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)
}
bucket, err := client.Bucket("examplebucket")
if err != nil {
log.Fatalf("Failed to get bucket: %v", err)
}
// The full path of the source and destination objects, excluding the bucket name.
srcObject := "srcobject.txt"
destObject := "destobject.txt"
// Copy the source object to the destination key.
_, err = bucket.CopyObject(srcObject, destObject)
if err != nil {
log.Fatalf("Failed to copy object: %v", err)
}
// Delete the source object only after the copy succeeds.
err = bucket.DeleteObject(srcObject)
if err != nil {
log.Fatalf("Failed to delete source object: %v", err)
}
fmt.Printf("%s has been renamed to %s\n", srcObject, destObject)
}Replace the following placeholders before running the code:
| Placeholder | Description | Example |
|---|---|---|
yourEndpoint | Endpoint for your bucket's region | https://oss-cn-hangzhou.aliyuncs.com |
yourRegion | Region ID of your bucket | cn-hangzhou |