最近更新时间:2024-05-10 14:21:22
以下代码用于删除单个文件:
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>"
// 填写对象的key
key := "<object_key>"
// 删除单个文件
resp, err := client.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
Key: aws.String(key), // 对象的key,必填
})
if err != nil {
panic(err)
}
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
以下代码用于批量删除文件:
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.DeleteObjects(&s3.DeleteObjectsInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
IsReTurnResults: aws.Boolean(true), // 是否返回删除结果,非必填
Delete: &s3.Delete{
Objects: []*s3.ObjectIdentifier{
{
Key: aws.String("object_key1"), // 目标对象1的key
},
{
Key: aws.String("object_key2"), // 目标对象2的key
},
},
},
})
if err != nil {
panic(err)
}
fmt.Println("删除成功的文件:\n", awsutil.StringValue(resp.Deleted))
fmt.Println("删除失败的文件:\n", awsutil.StringValue(resp.Errors))
}
以下代码用于删除指定前缀的文件:
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>"
// 填写删除文件的前缀
prefix := "test1"
// 删除指定前缀的文件
resp, err := client.DeleteBucketPrefix(&s3.DeleteBucketPrefixInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
Prefix: aws.String(prefix), // 删除文件的前缀,必填
MaxKeys: aws.Long(100), // 单次列举的文件数,非必填
IsReTurnResults: aws.Boolean(true), // 是否返回删除结果,非必填
})
if err != nil {
panic(err)
}
fmt.Println("删除成功的文件:\n", awsutil.StringValue(resp.Deleted))
fmt.Println("删除失败的文件:\n", awsutil.StringValue(resp.Errors))
}
以下代码用于删除指定前缀(包含三次重试)的文件:
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>"
// 填写删除文件的前缀
prefix := "test/"
// 删除指定前缀的文件
resp, err := client.TryDeleteBucketPrefix(&s3.DeleteBucketPrefixInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
Prefix: aws.String(prefix), // 删除文件的前缀,必填
MaxKeys: aws.Long(100), // 单次列举的文件数,非必填
IsReTurnResults: aws.Boolean(true), // 是否返回删除结果,非必填
})
if err != nil {
panic(err)
}
fmt.Println("删除成功的文件:\n", awsutil.StringValue(resp.Deleted))
fmt.Println("删除失败的文件:\n", awsutil.StringValue(resp.Errors))
}
纯净模式