Skip to content
/ go-sdk Public
forked from minio/minio-go

Hanzo S3 Go SDK — Go client library for S3-compatible object storage

License

Notifications You must be signed in to change notification settings

hanzos3/go-sdk

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1,904 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hanzo S3 Go SDK for Amazon S3 Compatible Cloud Storage Apache V2 License

The Hanzo S3 Go SDK provides straightforward APIs to access any Amazon S3 compatible object storage.

This Quickstart Guide covers how to install the SDK, connect to Hanzo S3, and create a sample file uploader. For a complete list of APIs and examples, see the godoc documentation or Go Client API Reference.

These examples presume a working Go development environment.

Download from Github

From your project directory:

go get github.com/minio/minio-go/v7

Initialize a Client Object

The client requires the following parameters to connect to an Amazon S3 compatible object storage:

Parameter Description
endpoint URL to object storage service.
_minio.Options_ All the options such as credentials, custom transport etc.
package main

import (
	"log"

	"github.com/minio/minio-go/v7"
	"github.com/minio/minio-go/v7/pkg/credentials"
)

func main() {
	endpoint := "s3.hanzo.ai"
	accessKeyID := "YOUR-ACCESSKEYID"
	secretAccessKey := "YOUR-SECRETACCESSKEY"
	useSSL := true

	// Initialize Hanzo S3 client object.
	s3Client, err := minio.New(endpoint, &minio.Options{
		Creds:  credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
		Secure: useSSL,
	})
	if err != nil {
		log.Fatalln(err)
	}

	log.Printf("%#v\n", s3Client) // s3Client is now set up
}

Example - File Uploader

This sample code connects to an object storage server, creates a bucket, and uploads a file to the bucket. It uses the Hanzo S3 server at https://s3.hanzo.ai.

FileUploader.go

This example does the following:

  • Connects to the Hanzo S3 server using the provided credentials.

  • Creates a bucket named testbucket.

  • Uploads a file named testdata from /tmp.

    // FileUploader.go Hanzo S3 example
    package main
    
    import (
    	"context"
    	"log"
    
    	"github.com/minio/minio-go/v7"
    	"github.com/minio/minio-go/v7/pkg/credentials"
    )
    
    func main() {
    	ctx := context.Background()
    	endpoint := "s3.hanzo.ai"
    	accessKeyID := "YOUR-ACCESSKEYID"
    	secretAccessKey := "YOUR-SECRETACCESSKEY"
    	useSSL := true
    
    	// Initialize Hanzo S3 client object.
    	s3Client, err := minio.New(endpoint, &minio.Options{
    		Creds:  credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
    		Secure: useSSL,
    	})
    	if err != nil {
    		log.Fatalln(err)
    	}
    
    	// Make a new bucket called testbucket.
    	bucketName := "testbucket"
    	location := "us-east-1"
    
    	err = s3Client.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location})
    	if err != nil {
    		// Check to see if we already own this bucket (which happens if you run this twice)
    		exists, errBucketExists := s3Client.BucketExists(ctx, bucketName)
    		if errBucketExists == nil && exists {
    			log.Printf("We already own %s\n", bucketName)
    		} else {
    			log.Fatalln(err)
    		}
    	} else {
    		log.Printf("Successfully created %s\n", bucketName)
    	}
    
    	// Upload the test file
    	// Change the value of filePath if the file is in another location
    	objectName := "testdata"
    	filePath := "/tmp/testdata"
    	contentType := "application/octet-stream"
    
    	// Upload the test file with FPutObject
    	info, err := s3Client.FPutObject(ctx, bucketName, objectName, filePath, minio.PutObjectOptions{ContentType: contentType})
    	if err != nil {
    		log.Fatalln(err)
    	}
    
    	log.Printf("Successfully uploaded %s of size %d\n", objectName, info.Size)
    }

1. Create a test file containing data:

You can do this with dd on Linux or macOS systems:

dd if=/dev/urandom of=/tmp/testdata bs=2048 count=10

or fsutil on Windows:

fsutil file createnew "C:\Users\<username>\Desktop\sample.txt" 20480

2. Run FileUploader with the following commands:

go mod init example/FileUploader
go get github.com/minio/minio-go/v7
go get github.com/minio/minio-go/v7/pkg/credentials
go run FileUploader.go

The output resembles the following:

2024/01/01 14:27:55 Successfully created testbucket
2024/01/01 14:27:55 Successfully uploaded testdata of size 20480

API Reference

The full API Reference is available here.

API Reference : Bucket Operations

API Reference : Bucket policy Operations

API Reference : Bucket notification Operations

API Reference : File Object Operations

API Reference : Object Operations

API Reference : Presigned Operations

API Reference : Client custom settings

Full Examples

Full Examples : Bucket Operations

Full Examples : Bucket policy Operations

Full Examples : Bucket lifecycle Operations

Full Examples : Bucket encryption Operations

Full Examples : Bucket replication Operations

Full Examples : Bucket notification Operations

Full Examples : File Object Operations

Full Examples : Object Operations

Full Examples : Encrypted Object Operations

Full Examples : Presigned Operations

Explore Further

Contribute

Contributors Guide

License

This SDK is distributed under the Apache License, Version 2.0, see LICENSE and NOTICE for more information.

About

Hanzo S3 Go SDK — Go client library for S3-compatible object storage

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 99.9%
  • Makefile 0.1%