|
@@ -0,0 +1,91 @@
|
|
|
+package test
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "github.com/aliyun/aliyun-oss-go-sdk/oss"
|
|
|
+ "strings"
|
|
|
+ "testing"
|
|
|
+)
|
|
|
+
|
|
|
+// 删除出了 callback 文件之外的所有文件
|
|
|
+func TestDeleteExceptCallback(t *testing.T) {
|
|
|
+ // Define OSS configuration
|
|
|
+ endpoint := "http://pji-bucket1.oss.icvdc.com"
|
|
|
+ accessKeyID := "n8glvFGS25MrLY7j"
|
|
|
+ accessKeySecret := "xZ2Fozoarpfw0z28FUhtg8cu0yDc5d"
|
|
|
+ bucketName := "pji-bucket1"
|
|
|
+
|
|
|
+ // Create an OSS client
|
|
|
+ client, err := oss.New(endpoint, accessKeyID, accessKeySecret, oss.UseCname(true))
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("Failed to create OSS client: %v\n", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // Get the bucket
|
|
|
+ bucket, err := client.Bucket(bucketName)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("Failed to get bucket: %v\n", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // Define an array of directories (prefixes) to scan
|
|
|
+ //directories := []string{
|
|
|
+ // "pjibot_delivery/pjibot-P1YVPS1M22CM00020/data_parse/",
|
|
|
+ // "pjibot_delivery/pjibot-P1YYPS1M227M00107/data_parse/",
|
|
|
+ //}
|
|
|
+ directories := []string{
|
|
|
+ "pjibot_patrol/pjibot-P1YVXJ1M231M00022/data_parse",
|
|
|
+ "pjibot_patrol/pjibot-P1YVXJ1M231M00023/data_parse",
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, dir := range directories {
|
|
|
+ // List objects in the directory
|
|
|
+ marker := ""
|
|
|
+ for {
|
|
|
+ lsRes, err := bucket.ListObjects(oss.Prefix(dir), oss.Marker(marker))
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("Failed to list objects in directory %s: %v\n", dir, err)
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, object := range lsRes.Objects {
|
|
|
+ objectKey := object.Key
|
|
|
+
|
|
|
+ // Process files not containing "callback"
|
|
|
+ if !strings.Contains(objectKey, "callback") {
|
|
|
+ err := bucket.DeleteObject(objectKey)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("Failed to delete object %s: %v\n", objectKey, err)
|
|
|
+ } else {
|
|
|
+ fmt.Printf("Deleted object: %s\n", objectKey)
|
|
|
+ }
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // Rename files containing "callback_done" to "callback"
|
|
|
+ if strings.Contains(objectKey, "callback_done") {
|
|
|
+ newKey := strings.Replace(objectKey, "callback_done", "callback", 1)
|
|
|
+ err, _ = bucket.CopyObject(objectKey, newKey)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("Failed to copy object %s to %s: %v\n", objectKey, newKey, err)
|
|
|
+ err = bucket.DeleteObject(objectKey)
|
|
|
+ } else {
|
|
|
+ err = bucket.DeleteObject(objectKey)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("Failed to delete original object %s after renaming: %v\n", objectKey, err)
|
|
|
+ } else {
|
|
|
+ fmt.Printf("Renamed object: %s to %s\n", objectKey, newKey)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Exit the loop if all objects are processed
|
|
|
+ if !lsRes.IsTruncated {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ marker = lsRes.NextMarker
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|