Documentation ¶
Overview ¶
Package backblaze B2 API for Golang
Index ¶
- type B2
- func (c *B2) AuthorizeAccount() error
- func (b *B2) Bucket(bucketName string) (*Bucket, error)
- func (b *B2) CreateBucket(bucketName string, bucketType BucketType) (*Bucket, error)
- func (c *B2) DownloadFileByID(fileID string) (*File, io.ReadCloser, error)
- func (c *B2) DownloadURL() (string, error)
- func (b *B2) ListBuckets() ([]*Bucket, error)
- type B2Error
- func (e B2Error) Error() string
- func (e *B2Error) IsFatal() bool
- func (mj *B2Error) MarshalJSON() ([]byte, error)
- func (mj *B2Error) MarshalJSONBuf(buf fflib.EncodingBuffer) error
- func (uj *B2Error) UnmarshalJSON(input []byte) error
- func (uj *B2Error) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error
- type Bucket
- func (b *Bucket) Delete() error
- func (b *Bucket) DeleteFileVersion(fileName, fileID string) (*FileStatus, error)
- func (b *Bucket) DownloadFileByName(fileName string) (*File, io.ReadCloser, error)
- func (b *Bucket) FileURL(fileName string) (string, error)
- func (b *Bucket) GetFileInfo(fileID string) (*File, error)
- func (b *Bucket) GetUploadURL() (*url.URL, error)
- func (b *Bucket) HideFile(fileName string) (*FileStatus, error)
- func (b *Bucket) ListFileNames(startFileName string, maxFileCount int) (*ListFilesResponse, error)
- func (b *Bucket) ListFileVersions(startFileName, startFileID string, maxFileCount int) (*ListFileVersionsResponse, error)
- func (b *Bucket) Update(bucketType BucketType) error
- func (b *Bucket) UploadFile(name string, meta map[string]string, file io.Reader) (*File, error)
- func (b *Bucket) UploadHashedFile(name string, meta map[string]string, file io.Reader, sha1Hash string, ...) (*File, error)
- type BucketInfo
- type BucketType
- type Credentials
- type File
- type FileAction
- type FileStatus
- type ListFileVersionsResponse
- func (mj *ListFileVersionsResponse) MarshalJSON() ([]byte, error)
- func (mj *ListFileVersionsResponse) MarshalJSONBuf(buf fflib.EncodingBuffer) error
- func (uj *ListFileVersionsResponse) UnmarshalJSON(input []byte) error
- func (uj *ListFileVersionsResponse) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error
- type ListFilesResponse
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 ¶
AuthorizeAccount is used to log in to the B2 API.
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 ¶
DownloadFileByID downloads a file from B2 using its unique ID
func (*B2) DownloadURL ¶
DownloadURL returns the URL prefix needed to construct download links. Bucket.FileURL will costruct a full URL for given file names.
func (*B2) ListBuckets ¶
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) IsFatal ¶
IsFatal returns true if this error represents an error which can't be recovered from by retrying
func (*B2Error) MarshalJSON ¶
func (*B2Error) MarshalJSONBuf ¶
func (mj *B2Error) MarshalJSONBuf(buf fflib.EncodingBuffer) error
func (*B2Error) UnmarshalJSON ¶
func (*B2Error) UnmarshalJSONFFLexer ¶
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 ¶
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 ¶
DownloadFileByName Downloads one file by providing the name of the bucket and the name of the file.
func (*Bucket) FileURL ¶
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 ¶
GetFileInfo retrieves information about one file stored in B2.
func (*Bucket) GetUploadURL ¶
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 ¶
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 ¶
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 (*File) MarshalJSONBuf ¶
func (mj *File) MarshalJSONBuf(buf fflib.EncodingBuffer) error
func (*File) UnmarshalJSON ¶
func (*File) UnmarshalJSONFFLexer ¶
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