|
@@ -4,6 +4,8 @@ import (
|
|
|
"cicv-data-closedloop/common/util"
|
|
|
"cicv-data-closedloop/tools/plugin-compile/package/config"
|
|
|
"fmt"
|
|
|
+ "github.com/aliyun/aliyun-oss-go-sdk/oss"
|
|
|
+ "log"
|
|
|
"testing"
|
|
|
)
|
|
|
|
|
@@ -22,3 +24,60 @@ func TestCompareSize(t *testing.T) {
|
|
|
fmt.Println("本地文件大小为", fileSize)
|
|
|
// 4 比较大小
|
|
|
}
|
|
|
+
|
|
|
+func TestDeleteDir(t *testing.T) {
|
|
|
+
|
|
|
+ // 配置阿里云 Access Key ID、Access Key Secret 和 Endpoint
|
|
|
+ accessKeyID := "yourAccessKeyID"
|
|
|
+ accessKeySecret := "yourAccessKeySecret"
|
|
|
+ endpoint := "http://oss-cn-hangzhou.aliyuncs.com" // 根据你的 region 修改
|
|
|
+ bucketName := "yourBucketName"
|
|
|
+ prefix := "yourPrefix/"
|
|
|
+
|
|
|
+ // 创建客户端
|
|
|
+ client, err := oss.New(endpoint, accessKeyID, accessKeySecret)
|
|
|
+ if err != nil {
|
|
|
+ log.Fatalf("Error creating OSS client: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取 bucket
|
|
|
+ bucket, err := client.Bucket(bucketName)
|
|
|
+ if err != nil {
|
|
|
+ log.Fatalf("Error getting bucket: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 列出符合前缀的对象
|
|
|
+ opts := oss.ListObjectsOptions{
|
|
|
+ Prefix: prefix,
|
|
|
+ Delimiter: "",
|
|
|
+ MaxKeys: 1000, // 设置每次返回的最大对象数,可以根据需要调整
|
|
|
+ }
|
|
|
+
|
|
|
+ for {
|
|
|
+ lor, err := bucket.ListObjects(opts)
|
|
|
+ if err != nil {
|
|
|
+ log.Fatalf("Error listing objects: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 遍历并删除对象
|
|
|
+ for _, obj := range lor.Objects {
|
|
|
+ err = bucket.DeleteObject(obj.Key)
|
|
|
+ if err != nil {
|
|
|
+ log.Printf("Error deleting object %s: %v", obj.Key, err)
|
|
|
+ // 你可以选择如何处理删除失败的情况,例如记录日志或重新尝试
|
|
|
+ } else {
|
|
|
+ fmt.Printf("Deleted object: %s\n", obj.Key)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果没有更多对象,退出循环
|
|
|
+ if !lor.IsTruncated {
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新 ListObjectsOptions 以获取下一页对象
|
|
|
+ opts.Marker = lor.NextMarker
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Println("All objects with the specified prefix have been deleted.")
|
|
|
+}
|