Using the AWS Go SDK with B2

The AWS Go SDK allows developers to easily integrate object storage into their Go environment. The AWS Go SDK can be configured to take advantage of the Backblaze B2 S3 Compatible API.

 

Here is an example of configuring the AWS Go SDK with the S3 Compatible API.

 

package main

import (
"fmt"
"strings"

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

func main() {
bucket := aws.String("myBucketName")
key := aws.String("testfile.txt")

s3Config := &aws.Config{
Credentials: credentials.NewStaticCredentials("<B2-keyId>", "<b2-appKey>", ""),
Endpoint: aws.String("https://s3.us-west-002.backblazeb2.com"),
Region: aws.String("us-west-002"),
S3ForcePathStyle: aws.Bool(true),
}
newSession := session.New(s3Config)

s3Client := s3.New(newSession)

cparams := &s3.CreateBucketInput{
Bucket: bucket, // Required
}
_, err := s3Client.CreateBucket(cparams)
if err != nil {
// Print if any error.
fmt.Println(err.Error())
return
}
fmt.Printf("Successfully created bucket %s\n", *bucket)

// Upload a new object "testfile.txt" with the string "S3 Compatible API"
_, err = s3Client.PutObject(&s3.PutObjectInput{
Body: strings.NewReader("S3 Compatible API"),
Bucket: bucket,
Key: key,
})
if err != nil {
fmt.Printf("Failed to upload object %s/%s, %s\n", *bucket, *key, err.Error())
return
}
fmt.Printf("Successfully uploaded key %s\n",*key)

//Get Object
_, err = s3Client.GetObject(&s3.GetObjectInput{
Bucket: bucket,
Key: key,
})
if err != nil {
fmt.Println("Failed to download file", err)
return
}
fmt.Printf("Successfully Downloaded key %s\n",*key)

 

The S3 Compatible API for Backblaze B2 Cloud Storage allows 1000’s of integrations to work with B2 natively. If you’re new to the S3 Compatible API, please see our Getting Started Guide. If you have any trouble using the AWS Go SDK with B2, please let us know by emailing us at b2feedback@backblaze.com

 

 

Have more questions? Submit a request

0 Comments

Article is closed for comments.
Powered by Zendesk