From 156d1a3e23781080eec1a1d1a4c8e53c9174d75f Mon Sep 17 00:00:00 2001 From: leafus Date: Sun, 29 Sep 2024 04:58:53 +0200 Subject: [PATCH] Add file deletion and ignore s3-client-dev --- .gitignore | 3 ++- main.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 946016d..196993a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -s3config.toml \ No newline at end of file +s3config.toml +s3-client-dev \ No newline at end of file diff --git a/main.go b/main.go index 6058b01..c4a94e5 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ func main() { configPath := flag.String("config", "", "Path to the configuration file") directory := flag.String("directory", "", "Directory in the S3 bucket to upload the file to") listFiles := flag.Bool("list", false, "List files in the S3 bucket") + deleteFile := flag.String("delete", "", "Path to the file to delete from the S3 bucket") flag.Parse() if *configPath == "" { @@ -80,6 +81,11 @@ func main() { return } + if *deleteFile != "" { + deleteS3File(svc, bucket, *deleteFile) + return + } + if *filePath == "" { fmt.Println("No file specified for upload. Use -file to specify a file or -list to list bucket contents.") fmt.Println("Use -help for more information") @@ -141,3 +147,28 @@ func listBucketFiles(svc *s3.S3, bucket string) { os.Exit(1) } } + +func deleteS3File(svc *s3.S3, bucket, filePath string) { + filePath = strings.TrimPrefix(filePath, "/") + _, err := svc.DeleteObject(&s3.DeleteObjectInput{ + Bucket: aws.String(bucket), + Key: aws.String(filePath), + }) + + if err != nil { + fmt.Printf("Error deleting file from S3: %s\n", err) + os.Exit(1) + } + + err = svc.WaitUntilObjectNotExists(&s3.HeadObjectInput{ + Bucket: aws.String(bucket), + Key: aws.String(filePath), + }) + + if err != nil { + fmt.Printf("Error waiting for file deletion: %s\n", err) + os.Exit(1) + } + + fmt.Printf("Successfully deleted file: %s\n", filePath) +}