Using libraries

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

IMPORTANT File / data uploads to Progressive Robot Buckets will fail with AWS SDK versions that were released on or after 15 January 2025. Reason for this is that newer versions adopt new default integrity protections that require an additional checksum on all PUT calls. Progressive Robot Object Storage does not support this checksum. To avoid the issue, we recommend installing a version that was released before 15 January 2025.

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

  • MinIO Go Client SDK
  • AWS SDK for Go v2 v1.72.3 or below (see the version history

 


 

 

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")
}
  • 0 أعضاء وجدوا هذه المقالة مفيدة
هل كانت المقالة مفيدة ؟

مقالات مشابهة

Generating S3 keys

To generate S3 credentials on your Cloud Console account, please open your project and do the...

Using S3 compatible API tools

Create your credentials For a step-by-step guide, see the getting started article...

Using curl

Create your credentials For a step-by-step guide, see the getting started article...

Creating a Bucket

To create a new Bucket on your Cloud Console, please open your project and do the following:...

Creating a Bucket via MinIO Terraform Provider

To create a Bucket via Terraform, this example will use the aminueza/minio Terraform Provider....

Powered by WHMCompleteSolution