MinIO SDK
MinIO Go Client SDK is the official Go language library that allows you to work with S3-compatible storage via the Amazon S3 API (S3 API).
Setting up MinIO SDK
- Configure S3 access.
- Install the library in your project.
- Set environment variables.
- Configure the client.
1. Configure S3 access
Access can be configured by the Account Owner or a user with the iam.admin role.
- Create a service user with a role with S3 access. If you use a service user with the role
s3.user,object_storage_userors3.bucket.user, the bucket must have an access policy configured, and its rules must grant access to this user. - Issue an S3 key to the user.
2. Install the library
-
Open the CLI.
-
Add the library to your project:
go get github.com/minio/minio-go/v7
3. Set environment variables
There are different ways to specify S3 keys. We recommend specifying S3 keys through environment variables rather than in the code. Learn more about other methods in the MinIO SDK documentation.
Linux/macOS
Windows
-
Open the CLI.
-
Set the variables:
export AWS_ACCESS_KEY_ID=<access_key>export AWS_SECRET_ACCESS_KEY=<secret_key>Specify:
<access_key>— value of the Access key field from the S3 key you received in step 1;<secret_key>— value of the Secret key field from the S3 key you received in step 1.
4. Configure the client
-
In your local project, open the
main.gofile. -
Add the client initialization script:
package mainimport ("context""log""github.com/minio/minio-go/v7""github.com/minio/minio-go/v7/pkg/credentials")func main() {creds := credentials.NewEnvAWS()endpoint := "<s3_domain>"minioClient, err := minio.New(endpoint, &minio.Options{Creds: creds,Secure: true,})if err != nil {log.Fatal(err)}}Specify
<s3_domain>— S3 API domain. The domain depends on the pool where S3 is located.
Working with MinIO SDK
If you encounter errors when working with MinIO SDK, check the list of possible errors.
Get a list of buckets
-
Open the
main.gofile with the client you configured in step 4, or create a new file and copy the client into it. -
Add the script to get the list of buckets to the
func main()block:info, err := minioClient.ListBuckets(context.Background())if err != nil {log.Fatalf("cannot get ListBuckets: %s", err)}log.Printf("%#v\n", info)Full script example
package mainimport ("context""log""github.com/minio/minio-go/v7""github.com/minio/minio-go/v7/pkg/credentials")func main() {creds := credentials.NewEnvAWS()endpoint := "<s3_domain>"minioClient, err := minio.New(endpoint, &minio.Options{Creds: creds,Secure: true,})if err != nil {log.Fatal(err)}info, err := minioClient.ListBuckets(context.Background())if err != nil {log.Fatalf("cannot get ListBuckets: %s", err)}log.Printf("%#v\n", info)}Here
<s3_domain>— S3 API domain. The domain depends on the pool where S3 is located.
Create a bucket
-
Open the
main.gofile with the client you configured in step 4, or create a new file and copy the client into it. -
Add the script to create a bucket to the
func main()block:bucketName := "<bucket_name>"err = minioClient.MakeBucket(context.Background(),bucketName,minio.MakeBucketOptions{Region: "<pool>"},)if err != nil {exists, errBucketExists := minioClient.BucketExists(context.Background(), bucketName)if errBucketExists == nil && exists {log.Printf("Bucket %s already exists", bucketName)} else {log.Fatalf("cannot create bucket: %s", err)}} else {log.Printf("Bucket %s created", bucketName)}Specify:
- optional:
<pool>— pool in which the bucket will be created; <bucket_name>— bucket name.
Full script example
package mainimport ("context""log""github.com/minio/minio-go/v7""github.com/minio/minio-go/v7/pkg/credentials")func main() {creds := credentials.NewEnvAWS()endpoint := "<s3_domain>"minioClient, err := minio.New(endpoint, &minio.Options{Creds: creds,Secure: true,})if err != nil {log.Fatal(err)}bucketName := "bucketName"err = minioClient.MakeBucket(context.Background(),bucketName,minio.MakeBucketOptions{Region: "ru-7"},)if err != nil {exists, errBucketExists := minioClient.BucketExists(context.Background(), bucketName)if errBucketExists == nil && exists {log.Printf("Bucket %s already exists", bucketName)} else {log.Fatalf("cannot create bucket: %s", err)}} else {log.Printf("Bucket %s created", bucketName)}}Here
<s3_domain>— S3 API domain. The domain depends on the pool where S3 is located. - optional:
Upload an object to a bucket
-
Open the
main.gofile with the client you configured in step 4, or create a new file and copy the client into it. -
Add the script to upload an object to a bucket to the
func main()block:objectName := "<object_name>"filePath := "<path>"contentType := "application/octet-stream"info, err := minioClient.FPutObject(context.Background(),"<bucket_name>",objectName,filePath,minio.PutObjectOptions{ContentType: contentType},)if err != nil {log.Fatalf("Error: %s", err)}log.Printf("File uploaded. Size: %d bytes", info.Size)Specify:
<object_name>— name the object will have in S3;<path>— path to the file on the local device;<bucket_name>— bucket name.
Full script example
package mainimport ("context""log""github.com/minio/minio-go/v7""github.com/minio/minio-go/v7/pkg/credentials")func main() {creds := credentials.NewEnvAWS()endpoint := "<s3_domain>"minioClient, err := minio.New(endpoint, &minio.Options{Creds: creds,Secure: true,})if err != nil {log.Fatal(err)}objectName := "object-name.csv"filePath := "/path/file.csv"contentType := "application/octet-stream"info, err := minioClient.FPutObject(context.Background(),"bucketName",objectName,filePath,minio.PutObjectOptions{ContentType: contentType},)if err != nil {log.Fatalf("Error: %s", err)}log.Printf("File uploaded. Size: %d bytes", info.Size)}Here
<s3_domain>— S3 API domain. The domain depends on the pool where S3 is located.
Get a list of objects in a bucket
-
Open the
main.gofile with the client you configured in step 4, or create a new file and copy the client into it. -
Add the script to get a list of objects in a bucket to the
func main()block:objectsChan := minioClient.ListObjects(context.TODO(), "<bucket_name>", minio.ListObjectsOptions{Recursive: true,})for object := range objectsChan {log.Printf("%#v\n", object)}Specify
<bucket_name>— bucket name.Full script example
package mainimport ("context""log""github.com/minio/minio-go/v7""github.com/minio/minio-go/v7/pkg/credentials")func main() {creds := credentials.NewEnvAWS()endpoint := "<s3_domain>"minioClient, err := minio.New(endpoint, &minio.Options{Creds: creds,Secure: true,})if err != nil {log.Fatal(err)}objectsChan := minioClient.ListObjects(context.TODO(), "bucketName", minio.ListObjectsOptions{Recursive: true,})for object := range objectsChan {log.Printf("%#v\n", object)}}Here
<s3_domain>— S3 API domain. The domain depends on the pool where S3 is located.
Change bucket versioning status
-
Open the
main.gofile with the client you configured in step 4, or create a new file and copy the client into it. -
Add the script to change the versioning status to the
func main()block:err = minioClient.SetBucketVersioning(context.Background(),"<bucket_name>",minio.BucketVersioningConfiguration{Status: "<status>"},)if err != nil {log.Fatalf("cannot enable versioning: %s", err)}log.Printf("Versioning enabled for bucket %s", "<bucket_name>")Specify:
-
<bucket_name>— bucket name; -
<status>— versioning status to set for the bucket. Possible values:Enabled— enable versioning;Suspended— suspend versioning.
Full script example
package mainimport ("context""log""github.com/minio/minio-go/v7""github.com/minio/minio-go/v7/pkg/credentials")func main() {creds := credentials.NewEnvAWS()endpoint := "<s3_domain>"minioClient, err := minio.New(endpoint, &minio.Options{Creds: creds,Secure: true,})if err != nil {log.Fatal(err)}bucketName := "bucketName"err = minioClient.SetBucketVersioning(context.Background(),bucketName,minio.BucketVersioningConfiguration{Status: "Enabled"},)if err != nil {log.Fatalf("cannot enable versioning: %s", err)}log.Printf("Versioning enabled for bucket %s", bucketName)}Here
<s3_domain>— S3 API domain. The domain depends on the pool where S3 is located. -
-
Optionally: add the script to check the versioning status to the
func main()block:bucketName := "<bucket_name>"config, err := minioClient.GetBucketVersioning(context.Background(), bucketName)if err != nil {log.Fatalf("cannot get versioning config: %s", err)}log.Printf("Versioning status: %s", config.Status)Specify
<bucket_name>— bucket name.
Get an object
-
Open the
main.gofile with the client you configured in step 4, or create a new file and copy the client into it. -
In the
importblock, add theiopackage:import ("context""log""io""github.com/minio/minio-go/v7""github.com/minio/minio-go/v7/pkg/credentials") -
Add the script to get an object to the
func main()block:object, err := minioClient.GetObject(context.TODO(), "<bucket_name>", "<object_name>", minio.GetObjectOptions{})if err != nil {log.Fatalf("cannot get object: %s", err)}log.Printf("%#v\n", object)byteSlice, err := io.ReadAll(object)if err != nil {log.Fatalf("Error reading from reader: %s\n", err)}log.Printf("object contains: \"%s\"", byteSlice)Specify:
<object_name>— object name;<bucket_name>— bucket name.
Full script example
package mainimport ("context""log""io""github.com/minio/minio-go/v7""github.com/minio/minio-go/v7/pkg/credentials")func main() {creds := credentials.NewEnvAWS()endpoint := "<s3_domain>"minioClient, err := minio.New(endpoint, &minio.Options{Creds: creds,Secure: true,})if err != nil {log.Fatal(err)}object, err := minioClient.GetObject(context.TODO(), "bucketName", "objectName", minio.GetObjectOptions{})if err != nil {log.Fatalf("cannot get object: %s", err)}log.Printf("%#v\n", object)byteSlice, err := io.ReadAll(object)if err != nil {log.Fatalf("Error reading from reader: %s\n", err)}log.Printf("object contains: \"%s\"", byteSlice)}Here
<s3_domain>— S3 API domain. The domain depends on the pool where S3 is located.
Create a temporary link to upload or download an object
You can create a link in a public or private bucket using a presigned URL (Presigned URL). Learn more about Presigned URLs in the Sharing objects with presigned URLs section of the AWS documentation.
-
Open the
main.gofile with the client you configured in step 4, or create a new file and copy the client into it. -
In the
importblock, add thetimepackage.import ("context""log""time""github.com/minio/minio-go/v7""github.com/minio/minio-go/v7/pkg/credentials") -
Add the script to create upload and download links to the
func main()block:getURL, err := minioClient.PresignedGetObject(context.Background(),"<bucket_name>","<object_name>",<expiry>,nil,)if err != nil {log.Fatalf("cannot generate presigned get url: %s", err)}log.Printf("Presigned GET URL: %s", getURL.String())putURL, err := minioClient.PresignedPutObject(context.Background(),"<bucket_name>","<object_name>",<expiry>,)if err != nil {log.Fatalf("cannot generate presigned put url: %s", err)}log.Printf("Presigned PUT URL: %s", putURL.String())Specify:
-
<bucket_name>— bucket name; -
<object_name>— object name; -
<expiry>— link expiration time in the<amount>*<time>format, where:<amount>— number of hours, minutes, or seconds the link will be valid;<time>— time unit, possible values:time.Hour,time.Minuteortime.Second.
Full script example
package mainimport ("context""log""time""github.com/minio/minio-go/v7""github.com/minio/minio-go/v7/pkg/credentials")func main() {creds := credentials.NewEnvAWS()endpoint := "<s3_domain>"minioClient, err := minio.New(endpoint, &minio.Options{Creds: creds,Secure: true,})if err != nil {log.Fatal(err)}getURL, err := minioClient.PresignedGetObject(context.Background(),"bucketName","objectName",30*time.Hour,nil,)if err != nil {log.Fatalf("cannot generate presigned get url: %s", err)}log.Printf("Presigned GET URL: %s", getURL.String())putURL, err := minioClient.PresignedPutObject(context.Background(),"bucketName","objectName",time.Hour,)if err != nil {log.Fatalf("cannot generate presigned put url: %s", err)}log.Printf("Presigned PUT URL: %s", putURL.String())}Here
<s3_domain>— S3 API domain. The domain depends on the pool where S3 is located. -
Get object metadata
-
Open the
main.gofile with the client you configured in step 4, or create a new file and copy the client into it. -
Add the script to get object metadata to the
func main()block:info, err := minioClient.StatObject(context.Background(),"<bucket_name>","<object_name>",minio.StatObjectOptions{},)if err != nil {log.Fatalf("cannot get object info: %s", err)}log.Printf("Key: %s", info.Key)log.Printf("Size: %d bytes", info.Size)log.Printf("ContentType: %s", info.ContentType)log.Printf("ETag: %s", info.ETag)log.Printf("LastModified: %s", info.LastModified)Specify:
<bucket_name>— bucket name;<object_name>— object name.
Full script example
package mainimport ("context""log""github.com/minio/minio-go/v7""github.com/minio/minio-go/v7/pkg/credentials")func main() {creds := credentials.NewEnvAWS()endpoint := "<s3_domain>"minioClient, err := minio.New(endpoint, &minio.Options{Creds: creds,Secure: true,})if err != nil {log.Fatal(err)}info, err := minioClient.StatObject(context.Background(),"bucketName","objectName",minio.StatObjectOptions{},)if err != nil {log.Fatalf("cannot get object info: %s", err)}log.Printf("Key: %s", info.Key)log.Printf("Size: %d bytes", info.Size)log.Printf("ContentType: %s", info.ContentType)log.Printf("ETag: %s", info.ETag)log.Printf("LastModified: %s", info.LastModified)}Here
<s3_domain>— S3 API domain. The domain depends on the pool where S3 is located.
Delete an object
-
Open the
main.gofile with the client you configured in step 4, or create a new file and copy the client into it. -
Add the script to delete an object to the
func main()block:err = minioClient.RemoveObject(context.Background(),"<bucket_name>","<object_name>",minio.RemoveObjectOptions{VersionID: "<version_id>"},)if err != nil {log.Fatalf("cannot remove object: %s", err)}log.Printf("Object %s removed", "<object_name>")Specify:
<bucket_name>— bucket name;<object_name>— object name;- optional:
VersionID: "<version_id>"— option to delete a specific version of an object if versioning is enabled in the bucket. Specify<version_id>— version identifier.
Full script example
package mainimport ("context""log""github.com/minio/minio-go/v7""github.com/minio/minio-go/v7/pkg/credentials")func main() {creds := credentials.NewEnvAWS()endpoint := "<s3_domain>"minioClient, err := minio.New(endpoint, &minio.Options{Creds: creds,Secure: true,})if err != nil {log.Fatal(err)}err = minioClient.RemoveObject(context.Background(),"bucketName","objectName",minio.RemoveObjectOptions{},)if err != nil {log.Fatalf("cannot remove object: %s", err)}log.Printf("Object %s removed", "objectName")}Here
<s3_domain>— S3 API domain. The domain depends on the pool where S3 is located.