Use the AWS SDK for Go with Backblaze B2
- Print
- DarkLight
Use the AWS SDK for Go with Backblaze B2
- Print
- DarkLight
Article Summary
Share feedback
Thanks for sharing your feedback!
You can take advantage of Backblaze B2 Cloud Storage using the AWS SDK for Go alongside the Backblaze S3-Compatible API.
It is easy to use the AWS Go SDK to integrate object storage into your Go environment.
The following example shows a configuration of 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 allows 1000’s of integrations to natively work with Backblaze B2. If you are new to the S3-Compatible API, click here. If you have issues using this SDK with Backblaze B2, let us know by emailing us at [email protected].
Was this article helpful?