backblaze

package module
v0.0.0-...-899e111 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2016 License: MIT Imports: 15 Imported by: 1

README

go-backblaze

GoDoc Build Status

A golang client for Backblaze's B2 storage

Usage

Some simple examples to get you started. Errors are ommitted for brevity

Import the API package

import "gitub.com/peterdemartini/go-backblaze"

Create an API client

b2, _ := backblaze.NewB2(backblaze.Credentials{
  AccountID:      accountID,
  ApplicationKey: applicationKey,
})

Create a bucket

bucket, _ := b2.CreateBucket("test_bucket", backblaze.AllPrivate)

Uploading a file

reader, _ := os.Open(path)
name := filepath.Base(path)
metadata := make(map[string]string)

file, _ := bucket.UploadFile(name, metadata, reader)

All API methods except B2.AuthorizeAccount and Bucket.UploadHashedFile will retry once if authorization fails, which allows the operation to proceed if the current authorization token has expired.

To disable this behaviour, set B2.NoRetry to true

b2 command line client

A test applicaiton has been implemented using this package, and can be found in the /b2 directory. It should provide you with more examples of how to use the API in your own applications.

To install the b2 command, use:

go install gitub.com/peterdemartini/go-backblaze/b2

$ b2 --help
Usage:
  b2 [OPTIONS] <command>

Application Options:
      --account= The account ID to use [$B2_ACCOUNT_ID]
      --appKey=  The application key to use [$B2_APP_KEY]
  -b, --bucket=  The bucket to access [$B2_BUCKET]
  -d, --debug    Debug API requests
  -v, --verbose  Display verbose output

Help Options:
  -h, --help     Show this help message

Available commands:
  createbucket  Create a new bucket
  delete        Delete a file
  deletebucket  Delete a bucket
  get           Download a file
  list          List files in a bucket
  listbuckets   List buckets in an account
  put           Store a file

Documentation

Overview

Package backblaze B2 API for Golang

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type B2

type B2 struct {
	Credentials

	// If true, don't retry requests if authorization has expired
	NoRetry bool

	// If true, display debugging information about API calls
	Debug bool
	// contains filtered or unexported fields
}

B2 implements a B2 API client. Do not modify state concurrently.

Example
NewB2(Credentials{
	AccountID:      "", // Obtained from your B2 account page.
	ApplicationKey: "", // Obtained from your B2 account page.
})
Output:

func NewB2

func NewB2(creds Credentials) (*B2, error)

NewB2 creates a new Client for accessing the B2 API. The AuthorizeAccount method will be called immediately.

func (*B2) AuthorizeAccount

func (c *B2) AuthorizeAccount() error

AuthorizeAccount is used to log in to the B2 API.

func (*B2) Bucket

func (b *B2) Bucket(bucketName string) (*Bucket, error)

Bucket looks up a bucket for the currently authorized client

func (*B2) CreateBucket

func (b *B2) CreateBucket(bucketName string, bucketType BucketType) (*Bucket, error)

CreateBucket creates a new B2 Bucket in the authorized account.

Buckets can be named. The name must be globally unique. No account can use a bucket with the same name. Buckets are assigned a unique bucketId which is used when uploading, downloading, or deleting files.

func (*B2) DownloadFileByID

func (c *B2) DownloadFileByID(fileID string) (*File, io.ReadCloser, error)

DownloadFileByID downloads a file from B2 using its unique ID

func (*B2) DownloadURL

func (c *B2) DownloadURL() (string, error)

DownloadURL returns the URL prefix needed to construct download links. Bucket.FileURL will costruct a full URL for given file names.

func (*B2) ListBuckets

func (b *B2) ListBuckets() ([]*Bucket, error)

ListBuckets lists buckets associated with an account, in alphabetical order by bucket ID.

type B2Error

type B2Error struct {
	Code    string `json:"code"`
	Message string `json:"message"`
	Status  int    `json:"status"`
}

B2Error encapsulates an error message returned by the B2 API.

Failures to connect to the B2 servers, and networking problems in general can cause errors

func (B2Error) Error

func (e B2Error) Error() string

func (*B2Error) IsFatal

func (e *B2Error) IsFatal() bool

IsFatal returns true if this error represents an error which can't be recovered from by retrying

func (*B2Error) MarshalJSON

func (mj *B2Error) MarshalJSON() ([]byte, error)

func (*B2Error) MarshalJSONBuf

func (mj *B2Error) MarshalJSONBuf(buf fflib.EncodingBuffer) error

func (*B2Error) UnmarshalJSON

func (uj *B2Error) UnmarshalJSON(input []byte) error

func (*B2Error) UnmarshalJSONFFLexer

func (uj *B2Error) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

type Bucket

type Bucket struct {
	*BucketInfo
	// contains filtered or unexported fields
}

Bucket provides access to the files stored in a B2 Bucket

Example
var b2 B2
// ...

b2.CreateBucket("test_bucket", AllPrivate)
Output:

func (*Bucket) Delete

func (b *Bucket) Delete() error

Delete removes removes the bucket from the authorized account. Only buckets that contain no version of any files can be deleted.

func (*Bucket) DeleteFileVersion

func (b *Bucket) DeleteFileVersion(fileName, fileID string) (*FileStatus, error)

DeleteFileVersion deletes one version of a file from B2.

If the version you delete is the latest version, and there are older versions, then the most recent older version will become the current version, and be the one that you'll get when downloading by name. See the File Versions page for more details.

func (*Bucket) DownloadFileByName

func (b *Bucket) DownloadFileByName(fileName string) (*File, io.ReadCloser, error)

DownloadFileByName Downloads one file by providing the name of the bucket and the name of the file.

func (*Bucket) FileURL

func (b *Bucket) FileURL(fileName string) (string, error)

FileURL returns a URL which may be used to download the latest version of a file. This returned URL will only work for public buckets unless the correct authorization header is provided.

func (*Bucket) GetFileInfo

func (b *Bucket) GetFileInfo(fileID string) (*File, error)

GetFileInfo retrieves information about one file stored in B2.

func (*Bucket) GetUploadURL

func (b *Bucket) GetUploadURL() (*url.URL, error)

GetUploadURL retrieves the URL to use for uploading files.

When you upload a file to B2, you must call b2_get_upload_url first to get the URL for uploading directly to the place where the file will be stored.

func (*Bucket) HideFile

func (b *Bucket) HideFile(fileName string) (*FileStatus, error)

HideFile hides a file so that downloading by name will not find the file, but previous versions of the file are still stored. See File Versions about what it means to hide a file.

func (*Bucket) ListFileNames

func (b *Bucket) ListFileNames(startFileName string, maxFileCount int) (*ListFilesResponse, error)

ListFileNames lists the names of all files in a bucket, starting at a given name.

func (*Bucket) ListFileVersions

func (b *Bucket) ListFileVersions(startFileName, startFileID string, maxFileCount int) (*ListFileVersionsResponse, error)

ListFileVersions lists all of the versions of all of the files contained in one bucket, in alphabetical order by file name, and by reverse of date/time uploaded for versions of files with the same name.

func (*Bucket) Update

func (b *Bucket) Update(bucketType BucketType) error

Update allows the bucket type to be changed

func (*Bucket) UploadFile

func (b *Bucket) UploadFile(name string, meta map[string]string, file io.Reader) (*File, error)

UploadFile uploads a file to B2, returning its unique file ID. This method computes the hash of the file before passing it to UploadHashedFile

Example
var bucket Bucket
// ...

path := "/path/to/file"
reader, _ := os.Open(path)
name := filepath.Base(path)
metadata := make(map[string]string)

bucket.UploadFile(name, metadata, reader)
Output:

func (*Bucket) UploadHashedFile

func (b *Bucket) UploadHashedFile(name string, meta map[string]string, file io.Reader, sha1Hash string, contentLength int64) (*File, error)

UploadHashedFile Uploads a file to B2, returning its unique file ID.

This method will not retry if the upload fails, as the reader may have consumed some bytes. If the error type is B2Error and IsFatal returns false, you may retry the upload and expect it to succeed eventually.

type BucketInfo

type BucketInfo struct {
	ID         string `json:"bucketId"`
	AccountID  string `json:"accountId"`
	Name       string `json:"bucketName"`
	BucketType `json:"bucketType"`
}

BucketInfo describes a bucket

func (*BucketInfo) MarshalJSON

func (mj *BucketInfo) MarshalJSON() ([]byte, error)

func (*BucketInfo) MarshalJSONBuf

func (mj *BucketInfo) MarshalJSONBuf(buf fflib.EncodingBuffer) error

func (*BucketInfo) UnmarshalJSON

func (uj *BucketInfo) UnmarshalJSON(input []byte) error

func (*BucketInfo) UnmarshalJSONFFLexer

func (uj *BucketInfo) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

type BucketType

type BucketType string

BucketType defines the security setting for a bucket

const (
	AllPublic  BucketType = "allPublic"
	AllPrivate BucketType = "allPrivate"
	Snapshot   BucketType = "snapshot"
)

Buckets can be either public, private, or snapshot

type Credentials

type Credentials struct {
	AccountID      string
	ApplicationKey string
}

Credentials are the identification required by the Backblaze B2 API

The account ID is a 12-digit hex number that you can get from your account page on backblaze.com.

The application key is a 40-digit hex number that you can get from your account page on backblaze.com.

type File

type File struct {
	ID            string            `json:"fileId"`
	Name          string            `json:"fileName"`
	AccountID     string            `json:"accountId"`
	BucketID      string            `json:"bucketId"`
	ContentLength int64             `json:"contentLength"`
	ContentSha1   string            `json:"contentSha1"`
	ContentType   string            `json:"contentType"`
	FileInfo      map[string]string `json:"fileInfo"`
}

File descibes a file stored in a B2 bucket

func (*File) MarshalJSON

func (mj *File) MarshalJSON() ([]byte, error)

func (*File) MarshalJSONBuf

func (mj *File) MarshalJSONBuf(buf fflib.EncodingBuffer) error

func (*File) UnmarshalJSON

func (uj *File) UnmarshalJSON(input []byte) error

func (*File) UnmarshalJSONFFLexer

func (uj *File) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

type FileAction

type FileAction string

FileAction indicates the current status of a file in a B2 bucket

const (
	Upload FileAction = "upload"
	Hide   FileAction = "hide"
)

Files can be either uploads (visible) or hidden.

Hiding a file makes it look like the file has been deleted, without removing any of the history. It adds a new version of the file that is a marker saying the file is no longer there.

type FileStatus

type FileStatus struct {
	FileAction      `json:"action"`
	ID              string `json:"fileId"`
	Name            string `json:"fileName"`
	Size            int    `json:"size"`
	UploadTimestamp int64  `json:"uploadTimestamp"`
}

FileStatus describes minimal metadata about a file in a B2 bucket. It is returned by the ListFileNames and ListFileVersions methods

func (*FileStatus) MarshalJSON

func (mj *FileStatus) MarshalJSON() ([]byte, error)

func (*FileStatus) MarshalJSONBuf

func (mj *FileStatus) MarshalJSONBuf(buf fflib.EncodingBuffer) error

func (*FileStatus) UnmarshalJSON

func (uj *FileStatus) UnmarshalJSON(input []byte) error

func (*FileStatus) UnmarshalJSONFFLexer

func (uj *FileStatus) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

type ListFileVersionsResponse

type ListFileVersionsResponse struct {
	Files        []FileStatus `json:"files"`
	NextFileName string       `json:"nextFileName"`
	NextFileID   string       `json:"nextFileId"`
}

ListFileVersionsResponse lists a page of file versions stored in a B2 bucket

func (*ListFileVersionsResponse) MarshalJSON

func (mj *ListFileVersionsResponse) MarshalJSON() ([]byte, error)

func (*ListFileVersionsResponse) MarshalJSONBuf

func (mj *ListFileVersionsResponse) MarshalJSONBuf(buf fflib.EncodingBuffer) error

func (*ListFileVersionsResponse) UnmarshalJSON

func (uj *ListFileVersionsResponse) UnmarshalJSON(input []byte) error

func (*ListFileVersionsResponse) UnmarshalJSONFFLexer

func (uj *ListFileVersionsResponse) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

type ListFilesResponse

type ListFilesResponse struct {
	Files        []FileStatus `json:"files"`
	NextFileName string       `json:"nextFileName"`
}

ListFilesResponse lists a page of files stored in a B2 bucket

func (*ListFilesResponse) MarshalJSON

func (mj *ListFilesResponse) MarshalJSON() ([]byte, error)

func (*ListFilesResponse) MarshalJSONBuf

func (mj *ListFilesResponse) MarshalJSONBuf(buf fflib.EncodingBuffer) error

func (*ListFilesResponse) UnmarshalJSON

func (uj *ListFilesResponse) UnmarshalJSON(input []byte) error

func (*ListFilesResponse) UnmarshalJSONFFLexer

func (uj *ListFilesResponse) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

Directories

Path Synopsis
A minimal client for accessing B2
A minimal client for accessing B2

Jump to

Keyboard shortcuts

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