blazer

module
v0.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2023 License: Apache-2.0

README

Blazer

GoDoc

Blazer is a Golang client library for Backblaze B2 Cloud Object Storage.

import "github.com/Backblaze/blazer/b2"

Blazer targets the Backblaze B2 Native API. Unless you specifically need to access Backblaze B2 via its Native API, you should use the MinIO Go Client SDK with Backblaze B2's S3 Compatible SDK.

Many thanks to Toby Burress (kurin) for creating and maintaining Blazer for its first six years.

Examples

Getting started
import "os"

id := os.Getenv("B2_APPLICATION_KEY_ID")
key := os.Getenv("B2_APPLICATION_KEY")

ctx := context.Background()

// b2_authorize_account
b2, err := b2.NewClient(ctx, id, key)
if err != nil {
	log.Fatalln(e)
}

buckets, err := b2.ListBuckets(ctx)
if err != nil {
	log.Fatalln(e)
}
Copy a file into B2
func copyFile(ctx context.Context, bucket *b2.Bucket, src, dst string) error {
	f, err := os.Open(src)
	if err != nil {
		return err
	}
	defer f.Close()

	obj := bucket.Object(dst)
	w := obj.NewWriter(ctx)
	if _, err := io.Copy(w, f); err != nil {
		w.Close()
		return err
	}
	return w.Close()
}

If the file is less than 100MB, Blazer will simply buffer the file and use the b2_upload_file API to send the file to Backblaze. If the file is greater than 100MB, Blazer will use B2's large file support to upload the file in 100MB chunks.

Copy a file into B2, with multiple concurrent uploads

Uploading a large file with multiple HTTP connections is simple:

func copyFile(ctx context.Context, bucket *b2.Bucket, writers int, src, dst string) error {
	f, err := os.Open(src)
	if err != nil {
		return err
	}
	defer f.Close()

	w := bucket.Object(dst).NewWriter(ctx)
	w.ConcurrentUploads = writers
	if _, err := io.Copy(w, f); err != nil {
		w.Close()
		return err
	}
	return w.Close()
}

This will automatically split the file into writers chunks of 100MB uploads. Note that 100MB is the smallest chunk size that B2 supports.

Download a file from B2

Downloading is as simple as uploading:

func downloadFile(ctx context.Context, bucket *b2.Bucket, downloads int, src, dst string) error {
	r := bucket.Object(src).NewReader(ctx)
	defer r.Close()

	f, err := os.Create(dst)
	if err != nil {
		return err
	}
	r.ConcurrentDownloads = downloads
	if _, err := io.Copy(f, r); err != nil {
		f.Close()
		return err
	}
	return f.Close()
}
List all objects in a bucket
func printObjects(ctx context.Context, bucket *b2.Bucket) error {
	iterator := bucket.List(ctx)
	for iterator.Next() {
		fmt.Println(iterator.Object())
	}
	return iterator.Err()
}
Grant temporary auth to a file

Say you have a number of files in a private bucket, and you want to allow other people to download some files. This is possible to do by issuing a temporary authorization token for the prefix of the files you want to share.

token, err := bucket.AuthToken(ctx, "photos", time.Hour)

If successful, token is then an authorization token valid for one hour, which can be set in HTTP GET requests.

The hostname to use when downloading files via HTTP is account-specific and can be found via the BaseURL method:

base := bucket.BaseURL()
Licenses

The b2 package currently does not consume any third party packages and entirely depends on imports of the Go stdlib or from sources provided within the blazer repository itself. A report of used licenses can be found at ./b2/licenses.csv which was generated with https://github.com/google/go-licenses . Please double check yourself if this is a concern as this may change over time and the licenses report could become stale

Directories

Path Synopsis
Package b2 provides a high-level interface to Backblaze's B2 cloud storage service.
Package b2 provides a high-level interface to Backblaze's B2 cloud storage service.
Package base provides a very low-level interface on top of the B2 v1 API.
Package base provides a very low-level interface on top of the B2 v1 API.
bin
b2keys
b2keys is a small utility for managing Backblaze B2 keys.
b2keys is a small utility for managing Backblaze B2 keys.
Package bonfire implements the B2 service.
Package bonfire implements the B2 service.
examples
simple
This is a simple program that will copy named files into or out of B2.
This is a simple program that will copy named files into or out of B2.
internal
b2assets
Package b2assets contains data required by other libraries in blazer.
Package b2assets contains data required by other libraries in blazer.
b2types
Package b2types implements internal types common to the B2 API.
Package b2types implements internal types common to the B2 API.
blog
Package blog implements a private logger, in the manner of glog, without polluting the flag namespace or leaving files all over /tmp.
Package blog implements a private logger, in the manner of glog, without polluting the flag namespace or leaving files all over /tmp.
pyre
Package pyre provides a gRPC-based implementation of B2, as well as a RESTful gateway on top of it.
Package pyre provides a gRPC-based implementation of B2, as well as a RESTful gateway on top of it.
pyre/proto
Package pyre_proto is a reverse proxy.
Package pyre_proto is a reverse proxy.
x
consistent
Package consistent implements an experimental interface for using B2 as a coordination primitive.
Package consistent implements an experimental interface for using B2 as a coordination primitive.
transport
Package transport provides http.RoundTrippers that may be useful to clients of Blazer.
Package transport provides http.RoundTrippers that may be useful to clients of Blazer.
window
Package window provides a type for efficiently recording events as they occur over a given span of time.
Package window provides a type for efficiently recording events as they occur over a given span of time.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL