最近更新时间:2023-11-03 16:34:51
以下代码用于设置生命周期规则:
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>"
// 构造存储空间的生命周期规则
lifecycleConfiguration := &s3.LifecycleConfiguration{
Rules: []*s3.LifecycleRule{
{
ID: aws.String("rule1"), // Rule的唯一标识,一个Bucket内ID不能重复。ID可以是任意字符串,包括中文,但不能超过255个字符(UTF-8编码)
Status: aws.String("Enabled"), // 指定Rule是启用还是禁用。 Enabled:该Rule定期被执行; Disabled:该Rule不生效,但该Rule随时能被Enable而不是被删
Expiration: &s3.LifecycleExpiration{
Days: aws.Long(30), // 指定生命周期规则在Object最后修改多少天后生效
},
},
},
}
// 设置存储空间的生命周期
resp, err := client.PutBucketLifecycle(&s3.PutBucketLifecycleInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
LifecycleConfiguration: lifecycleConfiguration, // 生命周期规则,必填
})
if err != nil {
panic(err)
}
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
有关设置生命周期规则详情,请参见Put Bucket Lifecycle。
以下代码用于获取生命周期规则:
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.GetBucketLifecycle(&s3.GetBucketLifecycleInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
})
if err != nil {
panic(err)
}
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
有关获取生命周期规则详情,请参见GET Bucket Lifecycle。
以下代码用于删除生命周期规则:
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.DeleteBucketLifecycle(&s3.DeleteBucketLifecycleInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
})
if err != nil {
panic(err)
}
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
有关删除生命周期规则详情,请参见DELETE Bucket Lifecycle。
纯净模式