The following example uses Copier.Copy to rename an object.
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Define global variables.
var (
region string // Region.
srcBucketName string // Name of the source bucket.
srcObjectName string // Name of the source object.
destBucketName string // Name of the destination bucket.
destObjectName string // Name of the destination object.
)
// Specify the init function used to initialize command line parameters.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&srcBucketName, "src-bucket", "", "The name of the source bucket.")
flag.StringVar(&srcObjectName, "src-object", "", "The name of the source object.")
flag.StringVar(&destBucketName, "dest-bucket", "", "The name of the destination bucket.")
flag.StringVar(&destObjectName, "dest-object", "", "The name of the destination object.")
}
func main() {
// Parse command line parameters.
flag.Parse()
// Check whether the source bucket name is specified.
if len(srcBucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is specified.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// If the destination bucket name is not specified, the source bucket name is used.
if len(destBucketName) == 0 {
destBucketName = srcBucketName
}
// Check whether the source object name is specified.
if len(srcObjectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, src object name required")
}
// Check whether the destination object name is specified.
if len(destObjectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, destination object name required")
}
// Load the default configurations and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Create a copier.
c := client.NewCopier()
// Create a request to copy the object.
copyRequest := &oss.CopyObjectRequest{
Bucket: oss.Ptr(destBucketName), // Name of the destination bucket.
Key: oss.Ptr(destObjectName), // Name of the destination object.
SourceKey: oss.Ptr(srcObjectName), // Name of the source object.
SourceBucket: oss.Ptr(srcBucketName), // Name of the source bucket.
StorageClass: oss.StorageClassStandard, // Set the storage class to Standard.
}
// Copy the source object and process the results.
result, err := c.Copy(context.TODO(), copyRequest)
if err != nil {
log.Fatalf("failed to copy object %v", err)
}
// Create a request to delete the object.
deleteRequest := &oss.DeleteObjectRequest{
Bucket: oss.Ptr(srcBucketName), // Name of the bucket.
Key: oss.Ptr(srcObjectName), // Name of the object to delete.
}
// Delete the object.
deleteResult, err := client.DeleteObject(context.TODO(), deleteRequest)
if err != nil {
log.Fatalf("failed to delete multiple objects %v", err)
}
// Display the result of the copy operation.
log.Printf("copy object result:%#v\n", result)
// Display the result of deletion.
log.Printf("delete objects result:%#v\n", deleteResult)
}