Background introduction
MinIO is a high-performance object storage service, compatible with Amazon S3 APIs, and is widely used to store and manage massive data. In actual development, encapsulating a MinIO operation package that is easy to use can help us simplify operation logic and improve code readability and reusability.
This article will introduce how to encapsulate a simple MinIO operation package in Go language, supporting the following functions:
- Initialize the MinIO client
- Upload file
- Download the file
- List files
- Delete files
- Get the pre-signature URL of the file
Code implementation
Structural definition
package minio_wrapper import ( "context" "fmt" "log" "time" "/minio/minio-go/v7" "/minio/minio-go/v7/pkg/credentials" ) type MinioClient struct { client * bucket string location string }
Initialize the MinIO client
The NewMinioClient method allows you to initialize the MinIO client according to configuration parameters and ensure that the specified bucket exists. If the bucket does not exist, it will be created automatically.
func NewMinioClient(endpoint, accessKeyID, secretAccessKey, bucket, location string, useSSL bool) (*MinioClient, error) { client, err := (endpoint, &{ Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), Secure: useSSL, }) if err != nil { return nil, ("failed to initialize minio client: %w", err) } ctx := () exists, err := (ctx, bucket) if err != nil { return nil, ("failed to check bucket existence: %w", err) } if !exists { err = (ctx, bucket, {Region: location}) if err != nil { return nil, ("failed to create bucket: %w", err) } ("Successfully created bucket: %s\n", bucket) } return &MinioClient{ client: client, bucket: bucket, location: location, }, nil }
Upload file
The UploadFile method allows you to upload local files to a specified bucket.
func (mc *MinioClient) UploadFile(objectName, filePath, contentType string) error { ctx, cancel := ((), ) defer cancel() _, err := (ctx, , objectName, filePath, { ContentType: contentType, }) if err != nil { return ("failed to upload file: %w", err) } ("Successfully uploaded %s to bucket %s\n", objectName, ) return nil }
Download the file
The DownloadFile method allows files in the bucket to be downloaded to a local specified path.
func (mc *MinioClient) DownloadFile(objectName, filePath string) error { ctx, cancel := ((), ) defer cancel() err := (ctx, , objectName, filePath, {}) if err != nil { return ("failed to download file: %w", err) } ("Successfully downloaded %s to %s\n", objectName, filePath) return nil }
List files
The ListFiles method allows you to list all files in the bucket.
func (mc *MinioClient) ListFiles() ([]string, error) { ctx, cancel := ((), ) defer cancel() objectCh := (ctx, , {}) var objects []string for object := range objectCh { if != nil { return nil, ("error listing object: %w", ) } objects = append(objects, ) } return objects, nil }
Delete files
The DeleteFile method allows you to delete the specified file in the bucket.
func (mc *MinioClient) DeleteFile(objectName string) error { ctx, cancel := ((), ) defer cancel() err := (ctx, , objectName, {}) if err != nil { return ("failed to delete file: %w", err) } ("Successfully deleted %s from bucket %s\n", objectName, ) return nil }
Get the pre-signature URL of the file
The GetFileURL method can generate a pre-signed URL of the file, which facilitates temporary access to private files.
func (mc *MinioClient) GetFileURL(objectName string, expiry ) (string, error) { ctx, cancel := ((), ) defer cancel() reqParams := make(map[string]string) presignedURL, err := (ctx, , objectName, expiry, reqParams) if err != nil { return "", ("failed to generate presigned URL: %w", err) } ("Successfully generated URL for %s: %s\n", objectName, presignedURL) return (), nil }
Example of usage
Here is a complete example of usage:
package main import ( "log" "minio_wrapper" "time" ) func main() { endpoint := "127.0.0.1:9000" accessKeyID := "minioadmin" secretAccessKey := "minioadmin" bucket := "mybucket" location := "us-east-1" useSSL := false client, err := minio_wrapper.NewMinioClient(endpoint, accessKeyID, secretAccessKey, bucket, location, useSSL) if err != nil { ("Failed to initialize MinioClient: %v", err) } // Upload file err = ("", "/path/to/local/", "text/plain") if err != nil { ("Failed to upload file: %v", err) } // Get the pre-signed URL url, err := ("", *1) if err != nil { ("Failed to get file URL: %v", err) } ("Presigned URL: %s", url) }
Summarize
By encapsulating common operations of MinIO, we can greatly simplify code logic and improve development efficiency. In actual projects, functions can be further expanded according to requirements, such as supporting more operations, adding logging functions, or dynamically loading parameters through configuration files, etc.
This is the end of this article about detailed explanation of the operations related to Go encapsulation MinIO. For more related content on Go encapsulation MinIO, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!