Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions cli/cmd/s3/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", ""),
Expand All @@ -22,20 +22,21 @@ 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",
Run: func(cmd *cobra.Command, args []string) {
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)
Expand All @@ -44,14 +45,15 @@ 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",
Run: func(cmd *cobra.Command, args []string) {
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)
Expand All @@ -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)
Expand All @@ -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)")
}
47 changes: 44 additions & 3 deletions cli/cmd/s3/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}
Expand All @@ -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)
},
}
Expand All @@ -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)
Expand All @@ -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)
},
}
Expand Down Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions cli/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test file!