From 80df21b46b168109eb7186bae051b02edc627c4e Mon Sep 17 00:00:00 2001 From: Suryansh Prajapati <58465650+drk1rd@users.noreply.github.com> Date: Tue, 25 Mar 2025 12:37:09 +0530 Subject: [PATCH] deletion fix and commented --- cli/cmd/s3/bucket.go | 16 ++++++++++----- cli/cmd/s3/object.go | 47 +++++++++++++++++++++++++++++++++++++++++--- cli/test.txt | 1 + 3 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 cli/test.txt diff --git a/cli/cmd/s3/bucket.go b/cli/cmd/s3/bucket.go index ae5745a..aa9697d 100644 --- a/cli/cmd/s3/bucket.go +++ b/cli/cmd/s3/bucket.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -// Initialize MinIO client +// connectMinIO initializes and returns a MinIO client func connectMinIO() *minio.Client { client, err := minio.New("localhost:9000", &minio.Options{ Creds: credentials.NewStaticV4("minioadmin", "minioadmin", ""), @@ -22,13 +22,13 @@ func connectMinIO() *minio.Client { return client } -// Define bucket commands +// bucketCmd defines the root command for bucket operations var bucketCmd = &cobra.Command{ Use: "bucket", Short: "Manage S3 buckets", } -// Create bucket command +// createBucketCmd defines the command to create a new S3 bucket var createBucketCmd = &cobra.Command{ Use: "create", Short: "Create a new S3 bucket", @@ -36,6 +36,7 @@ var createBucketCmd = &cobra.Command{ client := connectMinIO() bucketName, _ := cmd.Flags().GetString("name") + // Create a new bucket err := client.MakeBucket(context.Background(), bucketName, minio.MakeBucketOptions{}) if err != nil { log.Fatalf("Failed to create bucket: %v", err) @@ -44,7 +45,7 @@ var createBucketCmd = &cobra.Command{ }, } -// Delete bucket command +// deleteBucketCmd defines the command to delete an existing S3 bucket var deleteBucketCmd = &cobra.Command{ Use: "delete", Short: "Delete an S3 bucket", @@ -52,6 +53,7 @@ var deleteBucketCmd = &cobra.Command{ client := connectMinIO() bucketName, _ := cmd.Flags().GetString("name") + // Delete the specified bucket err := client.RemoveBucket(context.Background(), bucketName) if err != nil { log.Fatalf("Failed to delete bucket: %v", err) @@ -60,12 +62,14 @@ var deleteBucketCmd = &cobra.Command{ }, } -// List buckets command +// listBucketsCmd defines the command to list all S3 buckets var listBucketsCmd = &cobra.Command{ Use: "list", Short: "List all S3 buckets", Run: func(cmd *cobra.Command, args []string) { client := connectMinIO() + + // List all buckets buckets, err := client.ListBuckets(context.Background()) if err != nil { log.Fatalf("Failed to list buckets: %v", err) @@ -77,10 +81,12 @@ var listBucketsCmd = &cobra.Command{ } func init() { + // Add subcommands to the root bucket command bucketCmd.AddCommand(createBucketCmd) bucketCmd.AddCommand(deleteBucketCmd) bucketCmd.AddCommand(listBucketsCmd) + // Define flags for the create and delete commands createBucketCmd.Flags().StringP("name", "n", "", "Name of the bucket (required)") deleteBucketCmd.Flags().StringP("name", "n", "", "Name of the bucket (required)") } diff --git a/cli/cmd/s3/object.go b/cli/cmd/s3/object.go index 2b125dd..e27855e 100644 --- a/cli/cmd/s3/object.go +++ b/cli/cmd/s3/object.go @@ -21,23 +21,30 @@ var uploadCmd = &cobra.Command{ Use: "upload", Short: "Upload a file to an S3 bucket", Run: func(cmd *cobra.Command, args []string) { + // Connect to MinIO client := connectMinIO() + // Get bucket and file path from flags bucket, _ := cmd.Flags().GetString("bucket") filePath, _ := cmd.Flags().GetString("file") + // Open the file file, err := os.Open(filePath) if err != nil { log.Fatalf("Failed to open file: %v", err) } defer file.Close() + // Get file stats fileStat, _ := file.Stat() + + // Upload the file to the specified bucket _, err = client.PutObject(context.Background(), bucket, filePath, file, fileStat.Size(), minio.PutObjectOptions{}) if err != nil { log.Fatalf("Failed to upload file: %v", err) } + // Print success message fmt.Printf("✅ File '%s' uploaded to bucket '%s'!\n", filePath, bucket) }, } @@ -47,21 +54,26 @@ var downloadCmd = &cobra.Command{ Use: "download", Short: "Download a file from an S3 bucket", Run: func(cmd *cobra.Command, args []string) { + // Connect to MinIO client := connectMinIO() + // Get bucket, file, and destination from flags bucket, _ := cmd.Flags().GetString("bucket") file, _ := cmd.Flags().GetString("file") destination, _ := cmd.Flags().GetString("destination") + // If no destination is provided, use the file name if destination == "" { destination = file } + // Download the file from the specified bucket err := client.FGetObject(context.Background(), bucket, file, destination, minio.GetObjectOptions{}) if err != nil { log.Fatalf("❌ Failed to download file: %v", err) } + // Print success message fmt.Printf("✅ File '%s' downloaded from bucket '%s' to '%s'!\n", file, bucket, destination) }, } @@ -71,12 +83,17 @@ var listObjectsCmd = &cobra.Command{ Use: "list", Short: "List all objects in a bucket", Run: func(cmd *cobra.Command, args []string) { + // Connect to MinIO client := connectMinIO() + + // Get bucket from flags bucket, _ := cmd.Flags().GetString("bucket") + // List objects in the specified bucket objectCh := client.ListObjects(context.Background(), bucket, minio.ListObjectsOptions{}) fmt.Printf("📂 Objects in bucket '%s':\n", bucket) + // Iterate over the objects and print their keys for object := range objectCh { if object.Err != nil { log.Fatalf("❌ Error listing objects: %v", object.Err) @@ -86,21 +103,45 @@ var listObjectsCmd = &cobra.Command{ }, } -// Delete an object +// Delete an object command var deleteObjectCmd = &cobra.Command{ Use: "delete", Short: "Delete an object from an S3 bucket", Run: func(cmd *cobra.Command, args []string) { + // Connect to MinIO client := connectMinIO() + // Get bucket and file from flags bucket, _ := cmd.Flags().GetString("bucket") file, _ := cmd.Flags().GetString("file") + fmt.Printf("🔍 Checking if object '%s' exists in bucket '%s'...\n", file, bucket) + + // Verify the file exists first + found := false + objectCh := client.ListObjects(context.Background(), bucket, minio.ListObjectsOptions{}) + for object := range objectCh { + if object.Err != nil { + log.Fatalf("❌ Error listing objects: %v", object.Err) + } + if object.Key == file { + found = true + break + } + } + + if !found { + log.Fatalf("❌ Error: Object '%s' not found in bucket '%s'. Double-check the filename!\n", file, bucket) + } + + // Proceed to delete now that we've confirmed the object exists + fmt.Printf("🚀 Deleting object '%s' from bucket '%s'...\n", file, bucket) err := client.RemoveObject(context.Background(), bucket, file, minio.RemoveObjectOptions{}) if err != nil { log.Fatalf("❌ Failed to delete object: %v", err) } + // Print success message fmt.Printf("✅ Object '%s' deleted from bucket '%s'!\n", file, bucket) }, } @@ -128,11 +169,11 @@ func init() { downloadCmd.MarkFlagRequired("bucket") downloadCmd.MarkFlagRequired("file") - // List objects + // List objects flags listObjectsCmd.Flags().StringP("bucket", "b", "", "Bucket to list objects from (required)") listObjectsCmd.MarkFlagRequired("bucket") - // Delete object + // Delete object flags deleteObjectCmd.Flags().StringP("bucket", "b", "", "Target bucket (required)") deleteObjectCmd.Flags().StringP("file", "f", "", "Object to delete (required)") deleteObjectCmd.MarkFlagRequired("bucket") diff --git a/cli/test.txt b/cli/test.txt new file mode 100644 index 0000000..c66d471 --- /dev/null +++ b/cli/test.txt @@ -0,0 +1 @@ +This is a test file!