最近更新时间:2024-11-11 19:47:33
以下代码用于设置存储空间复制规则:
package main
import (
"fmt"
"github.com/ks3sdklib/aws-sdk-go/aws"
"github.com/ks3sdklib/aws-sdk-go/aws/awsutil"
"github.com/ks3sdklib/aws-sdk-go/aws/credentials"
"github.com/ks3sdklib/aws-sdk-go/service/s3"
)
func main() {
// 创建访问凭证,请将<AccessKeyID>与<SecretAccessKey>替换成真正的值
cre := credentials.NewStaticCredentials("<AccessKeyID>", "<SecretAccessKey>", "")
// 创建S3Client,更多配置项请查看Go-SDK初始化文档
client := s3.New(&aws.Config{
Credentials: cre, // 访问凭证
Region: "BEIJING", // 填写您的Region
Endpoint: "ks3-cn-beijing.ksyuncs.com", // 填写您的Endpoint
})
// 填写存储空间名称
bucket := "<bucket_name>"
// 构造存储空间复制规则
replicationConfiguration := &s3.ReplicationConfiguration{
Prefix: []*string{aws.String("test/")},
DeleteMarkerStatus: aws.String("Disabled"),
TargetBucket: aws.String(bucket),
HistoricalObjectReplication: aws.String("Enabled"),
}
// 设置存储空间复制规则
resp, err := client.PutBucketReplication(&s3.PutBucketReplicationInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
ReplicationConfiguration: replicationConfiguration, // 复制规则,必填
})
if err != nil {
panic(err)
}
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
有关设置存储空间复制规则详情,请参见 PUT Bucket Replication。
以下代码用于获取存储空间复制规则:
package main
import (
"fmt"
"github.com/ks3sdklib/aws-sdk-go/aws"
"github.com/ks3sdklib/aws-sdk-go/aws/awsutil"
"github.com/ks3sdklib/aws-sdk-go/aws/credentials"
"github.com/ks3sdklib/aws-sdk-go/service/s3"
)
func main() {
// 创建访问凭证,请将<AccessKeyID>与<SecretAccessKey>替换成真正的值
cre := credentials.NewStaticCredentials("<AccessKeyID>", "<SecretAccessKey>", "")
// 创建S3Client,更多配置项请查看Go-SDK初始化文档
client := s3.New(&aws.Config{
Credentials: cre, // 访问凭证
Region: "BEIJING", // 填写您的Region
Endpoint: "ks3-cn-beijing.ksyuncs.com", // 填写您的Endpoint
})
// 填写存储空间名称
bucket := "<bucket_name>"
// 获取存储空间复制规则
resp, err := client.GetBucketReplication(&s3.GetBucketReplicationInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
})
if err != nil {
panic(err)
}
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
有关获取存储空间复制规则详情,请参见 GET Bucket Replication。
以下代码用于删除存储空间复制规则:
package main
import (
"fmt"
"github.com/ks3sdklib/aws-sdk-go/aws"
"github.com/ks3sdklib/aws-sdk-go/aws/awsutil"
"github.com/ks3sdklib/aws-sdk-go/aws/credentials"
"github.com/ks3sdklib/aws-sdk-go/service/s3"
)
func main() {
// 创建访问凭证,请将<AccessKeyID>与<SecretAccessKey>替换成真正的值
cre := credentials.NewStaticCredentials("<AccessKeyID>", "<SecretAccessKey>", "")
// 创建S3Client,更多配置项请查看Go-SDK初始化文档
client := s3.New(&aws.Config{
Credentials: cre, // 访问凭证
Region: "BEIJING", // 填写您的Region
Endpoint: "ks3-cn-beijing.ksyuncs.com", // 填写您的Endpoint
})
// 填写存储空间名称
bucket := "<bucket_name>"
// 删除存储空间复制规则
resp, err := client.DeleteBucketReplication(&s3.DeleteBucketReplicationInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
})
if err != nil {
panic(err)
}
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
有关删除存储空间复制规则详情,请参见 DELETE Bucket Replication。
纯净模式