The following example creates an OSS client, builds a PutBucketTransferAccelerationRequest with Enabled set to true, and calls PutBucketTransferAcceleration to enable transfer acceleration for the specified bucket.
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 in which the bucket is located.
bucketName string // Name of the bucket.
)
// 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(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
// Parse command line parameters.
flag.Parse()
// Check whether the name of the bucket is specified.
if len(bucketName) == 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")
}
// 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 request to enable transfer acceleration for the bucket.
request := &oss.PutBucketTransferAccelerationRequest{
Bucket: oss.Ptr(bucketName), // Name of the bucket.
TransferAccelerationConfiguration: &oss.TransferAccelerationConfiguration{
Enabled: oss.Ptr(true), // Enable transfer acceleration.
},
}
// Execute the request to enable transfer acceleration.
result, err := client.PutBucketTransferAcceleration(context.TODO(), request)
if err != nil {
log.Fatalf("failed to put bucket transfer acceleration %v", err)
}
// Display the result.
log.Printf("put bucket transfer acceleration result:%#v\n", result)
}