Using libraries

Last change on 2024-12-03 • Created on 2024-11-20 • ID: ST-CAE3E

In general, all the AWS SDKs should work well with our Object Storage. Below, you can find a few examples for Python and Go.

Please note that these recommendations are intended to help, but we cannot guarantee that everything will work as expected, and we do NOT provide support for them.

This article covers the following examples:

Python

Go




For Python

AWS SDK for Python Boto3

official documentation

import logging
import random
import string
from boto3 import client

# Function to create a random hash
def random_hash(length=8):
    return ''.join(random.choices(string.ascii_lowercase + string.digits, k=length))

# Set variable with random hash
VAR_BUCKET_NAME = f"my-bucket-{random_hash()}"

# Create a new S3 client
s3 = client(
    "s3",
    region_name="fsn1",
    endpoint_url="https://fsn1.your-objectstorage.com",
    aws_access_key_id="YOUR_ACCESS_KEY",
    aws_secret_access_key="YOUR_SECRET_KEY",
)

# Create a new Bucket
s3.create_bucket(Bucket=VAR_BUCKET_NAME)

logging.info(f"created Bucket: {VAR_BUCKET_NAME}")

# Upload a small object
s3.put_object(
    Bucket=VAR_BUCKET_NAME,
    Key="my-object",
    Body="""
# My object

This markdown document is the content of my object.
""",
)

logging.info("created object")

For Go

MinIO Go Client SDK

official documentation

package main

import (
        "context"
        "crypto/rand"
        "encoding/hex"
        "fmt"
        "log"
        "strings"

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

// Generate a random hash
func generateRandomHash(length int) string {
        bytes := make([]byte, length)
        _, err := rand.Read(bytes)
        if err != nil {
                log.Fatalf("Failed to generate random hash: %v", err)
        }
        return hex.EncodeToString(bytes)[:length]
}

func main() {
        ctx := context.Background()

        // Set variable with random hash
        VAR_BUCKET_NAME := fmt.Sprintf("my-bucket-%s", generateRandomHash(8))

        // Create a new S3 client
        client, err := minio.New("fsn1.your-objectstorage.com", &minio.Options{
                Secure: true,
                Creds: credentials.NewStaticV4(
                        "YOUR-ACCESS-KEY",
                        "YOUR-SECRET-KEY",
                        "",
                ),
        })
        if err != nil {
                log.Fatal(err)
        }

        // Create a new Bucket
        err = client.MakeBucket(ctx, VAR_BUCKET_NAME, minio.MakeBucketOptions{Region: "fsn1"})
        if err != nil {
                log.Fatal(err)
        }

        log.Printf("Created Bucket: %s", VAR_BUCKET_NAME)

        // Upload a small object
        body := strings.NewReader(strings.TrimSpace(`
# My object

This markdown document is the content of my object.
`))

        _, err = client.PutObject(ctx,
                VAR_BUCKET_NAME,
                "my-object",
                body,
                body.Size(),
                minio.PutObjectOptions{},
        )
        if err != nil {
                log.Fatal(err)
        }

        log.Println("Created object")
}

AWS SDK for Go v2

official documentation

package main

import (
	"context"
	"crypto/rand"
	"encoding/hex"
	"fmt"
	"log"
	"strings"

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/credentials"
	"github.com/aws/aws-sdk-go-v2/service/s3"
)

// Generate a random hash
func generateRandomHash(length int) string {
        bytes := make([]byte, length)
        _, err := rand.Read(bytes)
        if err != nil {
                log.Fatalf("Failed to generate random hash: %v", err)
        }
        return hex.EncodeToString(bytes)[:length]
}

func main() {
	ctx := context.Background()

	// Set variable with random hash
	VAR_BUCKET_NAME := fmt.Sprintf("my-bucket-%s", generateRandomHash(8))

	// Create a new s3 client
	client := s3.New(s3.Options{
		AppID: "my-application/0.0.1",

		Region:       "fsn1",
		BaseEndpoint: aws.String("https://fsn1.your-objectstorage.com"),

		Credentials: credentials.StaticCredentialsProvider{Value: aws.Credentials{
			AccessKeyID:     "YOUR-ACCESS-KEY",
			SecretAccessKey: "YOUR-SECRET-KEY",
		}},
	})

	// Create a new Bucket
	_, err := client.CreateBucket(ctx, &s3.CreateBucketInput{
		Bucket: aws.String(VAR_BUCKET_NAME),
	})
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("Created Bucket: %s", VAR_BUCKET_NAME)

	// Upload a small object
	body := strings.NewReader(strings.TrimSpace(`
# My object

This markdown document is the content of my object.
`))

	_, err = client.PutObject(ctx, &s3.PutObjectInput{
		Bucket:      aws.String(VAR_BUCKET_NAME),
		Key:         aws.String("my-object-e9be8f5c"),
		ContentType: aws.String("text/markdown"),

		Body: body,
	})
	if err != nil {
		log.Fatal(err)
	}

	log.Println("Created object")
}
Table of Contents