b2

package module
v0.0.0-...-2af45b2 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2018 License: MIT Imports: 14 Imported by: 0

README

Go client and library for B2 Cloud Storage

Build Status codecov Go Report Card

README | 中文文档

What is b2?

What can I do with b2?

  • Transmit file across machines.
  • OSS for your web server.
  • Share file with your partner and friend.
  • Intergrate b2 library in your program.

How to use b2?

  1. Download the latest programs from Release page according to your os and arch.

  2. Register a b2 account by

b2 register
  1. Set environment B2_ACCOUNT_ID and B2_APPLICATION_KEY which you got from b2. You can put these in your ~/.bashrc
export B2_ACCOUNT_ID="XXXX"
export B2_APPLICATION_KEY="XXXX"

Dependencies

b2 uses:

Documentation

Overview

Package b2 is a go library for backblaze B2 Cloud Storage.

Index

Constants

View Source
const AUTH_URL = "https://api.backblazeb2.com/b2api/v1/b2_authorize_account"
View Source
const DELETE_BUCKETS = "deleteBuckets"
View Source
const DELETE_FILES = "deleteFiles"
View Source
const DELETE_KEYS = "deleteKeys"
View Source
const LIST_BUCKETS = "listBuckets"
View Source
const LIST_FILES = "listFiles"
View Source
const LIST_KEYS = "listKeys"

Capability

View Source
const PRIVATE = "allPrivate"
View Source
const PUBLIC = "allPublic"
View Source
const READ_FILES = "readFiles"
View Source
const SHARE_FILES = "shareFiles"
View Source
const WRITE_BUCKETS = "writeBuckets"
View Source
const WRITE_FILES = "writeFiles"
View Source
const WRITE_KEYS = "writeKeys"

Variables

This section is empty.

Functions

func GetKeyFromEnv

func GetKeyFromEnv() (string, string)

Types

type ApplicationKey

type ApplicationKey struct {
	ApplicationKeyId    string   `json:"applicationKeyId"`
	ApplicationKey      string   `json:"applicationKey"`
	KeyName             string   `json:"keyName"`
	Capabilities        []string `json:"capabilities"`
	ExpirationTimestamp int64    `json:"expirationTimestamp,omitempty"`
	BucketId            string   `json:"bucketId,omitempty"`
	NamePrefix          string   `json:"namePrefix,omitempty"`
}

type ApplicationKeys

type ApplicationKeys struct {
	Keys                 []*ApplicationKey `json:"keys"`
	NextApplicationKeyId string            `json:"nextApplicationKeyId,omitempty"`
}

type AuthResponse

type AuthResponse struct {
	AccountId               string `json:"accountId"`
	AuthorizationToken      string `json:"authorizationToken"`
	ApiUrl                  string `json:"apiUrl"`
	DownloadUrl             string `json:"downloadUrl"`
	RecommendedPartSize     int64  `json:"recommendedPartSize"`
	AbsoluteMinimumPartSize int64  `json:"absoluteMinimumPartSize"`
}

type B2

type B2 struct {
	AccountId      string
	ApplicationKey string
	// contains filtered or unexported fields
}

B2 is used to initialize your b2 account and applicationkey

func (*B2) Auth

func (b *B2) Auth() error

Auth your account

func (*B2) CancelLargeFile

func (b *B2) CancelLargeFile(fileId string) error

CancelLargeFile cancel parted uploaded. See "b2_cancel_large_file" for an introduction: https://www.backblaze.com/b2/docs/b2_cancel_large_file.html

Parameter fileId is required. CancelLargeFile return an error.

func (*B2) CreateBucket

func (b *B2) CreateBucket(bucketName, bucketType string, bucketInfo map[string]string,
	corsRules []CorsRule, lifecycleRules []LifecycleRule) (*Bucket, error)

CreateBucket create a bucket. See "b2_create_bucket" for an introduction: https://www.backblaze.com/b2/docs/b2_create_bucket.html

Parameter bucketName and bucketType are required. You can pass empty map or slice for simplicity. CreateBucket returned a Bucket pointer and an error.

func (*B2) CreateKey

func (b *B2) CreateKey(capabilities []string, keyName string, validDurationInSeconds int64, bucketId string, namePrefix string) (*ApplicationKey, error)

CreateKey creates a new application key. See "b2_create_key" for an introduction: https://www.backblaze.com/b2/docs/b2_create_key.html

Parameter capabilities, keyName are required. CreateKey return an ApplicationKey pointer and an error.

func (*B2) DeleteBucket

func (b *B2) DeleteBucket(bucketId string) error

DeleteBucket delete a bucket. See "b2_delete_bucket" for an introduction: https://www.backblaze.com/b2/docs/b2_delete_bucket.html

Parameter bucketId is required, you can get bucketId from an Bucket struct. DeleteBucket returned nil if success, return error if failed.

func (*B2) DeleteFileVersion

func (b *B2) DeleteFileVersion(fileName, fileId string) error

DeleteFileVersion delete a file version from b2 Cloud Storage. If the version you delete is the lastest version, and there are older versions, then the most recent older version will become the current version. See "b2_delete_file_version" for an introduction: https://www.backblaze.com/b2/docs/b2_delete_file_version.html

Parameter fileName and fileId are required, you can get these from any File struct. DeleteFileVersion return nil if successed, return error if failed.

func (*B2) DeleteKey

func (b *B2) DeleteKey(key *ApplicationKey) error

DeleteKey deletes the application key specified. See "b2_delete_key" for an introduction: https://www.backblaze.com/b2/docs/b2_delete_key.html

Parameter key is required. DeleteKey return nil if delete successd or error if something error happened.

func (*B2) DownloadFileById

func (b *B2) DownloadFileById(fileId, filePath string, needAuth bool,
	report func(int64, int64)) error

DownloadFileById downlaod file from b2 Cloud Storage using fileId. See "b2_download_file_by_id" for an introduction: https://www.backblaze.com/b2/docs/b2_download_file_by_id.html

Parameter fileId and filePath are required, if the bucket is private, you should pass needAuth as true. Parameter filePath is the local file path you want to save. DownloadFileById return nil if successed, return error if failed.

func (*B2) DownloadFileByName

func (b *B2) DownloadFileByName(bucketName, fileName, filePath string,
	needAuth bool, report func(int64, int64)) error

DownloadFileByName downlaod file from b2 Cloud Storage using bucketName and fileName. See "b2_download_file_by_name" for an introduction: https://www.backblaze.com/b2/docs/b2_download_file_by_name.html

Parameter bucketName, fileName and filePath are required, if the bucket is private, you should pass needAuth as true. Parameter filePath is the local file path you want to save. DownloadFileByName return nil if successed, return error if failed.

func (*B2) FinishLargeFile

func (b *B2) FinishLargeFile(fileId string, partSha1Array []string) (*File, error)

FinishLargeFile merge all parts uploaded. See "b2_finish_large_file" for an introduction: https://www.backblaze.com/b2/docs/b2_finish_large_file.html

Parameter fileId and partSha1Array are required. FinishLargeFile return a file pointer and an error.

func (*B2) GetAuth

func (b *B2) GetAuth() AuthResponse

func (*B2) GetDownloadAuthorization

func (b *B2) GetDownloadAuthorization(bucketId, fileNamePrefix string,
	validDurationInSeconds int64) (*DownloadUrlToken, error)

GetDownloadAuthorization create a download url and a token. See "b2_get_download_authorization" for an introduction: https://www.backblaze.com/b2/docs/b2_get_download_authorization.html

Parameter bucketId, fileNamePrefix and validDurationInSeconds are required. GetDownloadAuthorization return a DownloadUrlToken pointer and an error.

func (*B2) GetFileInfo

func (b *B2) GetFileInfo(fileId string) (*File, error)

GetFileInfo get the file info. See "b2_get_file_info" for an introduction: https://www.backblaze.com/b2/docs/b2_get_file_info.html

Parameter fileId is required, you can get fileId from any File struct. GetFileInfo return a File pointer and an error.

func (*B2) GetPublicFileDownloadURL

func (b *B2) GetPublicFileDownloadURL(bucketName, fileName string) string

func (*B2) GetUploadPartUrl

func (b *B2) GetUploadPartUrl(fileId string) (*UploadUrlToken, error)

GetUploadPartUrl return uploadUrlToken for a registered large file. See "b2_get_upload_part_url" for an introduction: https://www.backblaze.com/b2/docs/b2_get_upload_part_url.html

Parameter fileId is required. GetUploadPartUrl return a File pointer and an error.

func (*B2) GetUploadUrl

func (b *B2) GetUploadUrl(bucketId string) (*UploadUrlToken, error)

GetUploadUrl hide the file. See "b2_get_upload_url" for an introduction: https://www.backblaze.com/b2/docs/b2_get_upload_url.html

Parameter bucketId is required, you can get bucketId from any Bucket struct. GetUploadUrl return a UploadUrlToken pointer and an error.

func (*B2) HideFile

func (b *B2) HideFile(bucketId, fileName string) error

HideFile hide the file. See "b2_hide_file" for an introduction: https://www.backblaze.com/b2/docs/b2_hide_file.html

Parameter bucketId and fileName is required. HideFile return nil if successed, return error if failed.

func (*B2) ListBuckets

func (b *B2) ListBuckets(bucketId, bucketName, bucketTypes string) ([]*Bucket, error)

ListBuckets update a bucket. See "b2_list_buckets" for introduction: https://www.backblaze.com/b2/docs/b2_list_buckets.html

All parameter are optional, you can pass empty value for simplicity. List returned a bucket array and an error.

func (*B2) ListFileNames

func (b *B2) ListFileNames(bucketId, startFileName, prefix, delimiter string,
	maxFileCount int64) ([]*File, error)

ListFileNames list files names. See "b2_list_file_names" for an introduction: https://www.backblaze.com/b2/docs/b2_list_file_names.html

Parameter bucketId is required. You can pass other empty value for other parameter for simplicity. ListFileNames return a File array and an error.

func (*B2) ListFileVersions

func (b *B2) ListFileVersions(bucketId, startFileName, startFileId, prefix, delimiter string,
	maxFileCount int64) ([]*File, error)

ListFileVersions list files versions. See "b2_list_file_versions" for an introduction: https://www.backblaze.com/b2/docs/b2_list_file_versions.html

Parameter bucketId is required. You can pass other empty value for other parameter for simplicity. ListFileVersions return a File array and an error.

func (*B2) ListKeys

func (b *B2) ListKeys(maxKeyCount int64, startApplicationKeyId string) (*ApplicationKeys, error)

ListKeys list application keys associated with an account. See "b2_list_keys" for an introduction: https://www.backblaze.com/b2/docs/b2_list_keys.html All parameters are optional. ListKeys return an list of application pointer and an error.

func (*B2) ListParts

func (b *B2) ListParts(fileId string, startPartNumber int64, maxPartNumber int64) ([]*Part, error)

ListParts return parts of a large file. See "b2_list_parts" for an introduction: https://www.backblaze.com/b2/docs/b2_list_parts.html

All parameters are required. ListParts return uploaded parts of a large file.

func (*B2) ListUnfinishedLargeFiles

func (b *B2) ListUnfinishedLargeFiles(bucketId, namePrefix string, startFileId string, maxfileCount int64) ([]*File, error)

ListUnfinishedLargeFiles lists unfinished large files. See "b2_list_unfinished_large_files" for an introduction: https://www.backblaze.com/b2/docs/b2_list_unfinished_large_files.html

All parameters are required. ListUnfinishedLargeFiles return an array of file and an error.

func (*B2) SetAuth

func (b *B2) SetAuth(auth AuthResponse)

func (*B2) StartLargeFile

func (b *B2) StartLargeFile(bucketId, fileName string, fileInfo map[string]string) (*File, error)

StartLargeFile register a file to upload See "b2_start_large_file" for an introduction: https://www.backblaze.com/b2/docs/b2_start_large_file.html

Parameter bucketId and fileName are required. You can pass other empty value for other parameter for simplicity. StartLargeFile return a File array and an error.

func (*B2) UpdateBucket

func (b *B2) UpdateBucket(bucket *Bucket, ifRevisionIs bool) (*Bucket, error)

UpdateBucket update a bucket. See "b2_update_bucket" for an introduction: https://www.backblaze.com/b2/docs/b2_update_bucket.html

Parameter bucket is required, you can pass ifRevisionIs as false for simplicity. UpdateBucket returned a bucket pointer and an error.

func (*B2) UploadFile

func (b *B2) UploadFile(uploadUrlToken *UploadUrlToken, filePath string, progress func(int64, int64)) (*File, error)

UploadFile upload file to b2 Close Storage. See "b2_upload_file" for an introduction: https://www.backblaze.com/b2/docs/b2_upload_file.html

Parameter uploadUrlToken and filePath are required. UploadFile return a File pointer and an error.

func (*B2) UploadPart

func (b *B2) UploadPart(uploadUrlToken *UploadUrlToken, filePath string, offset, size, partNumber int64,
	progress func(int64, int64)) (string, error)

UploadPart upload file part to b2 Close Storage. See "b2_upload_part" for an introduction: https://www.backblaze.com/b2/docs/b2_upload_part.html

All parameters are required. UploadPart return a content sha1 and an error.

type Bucket

type Bucket struct {
	BucketId       string            `json:"bucketId"`
	BucketName     string            `json:"bucketName"`
	BucketType     string            `json:"bucketType"`
	BucketInfo     map[string]string `json:"bucketInfo,omitempty"`
	CorsRules      []CorsRule        `json:"corsRules,omitempty"`
	LifecycleRules []LifecycleRule   `json:"lifecycleRules,omitempty"`
	Revision       int64             `json:"revision,omitempty"`
}

type CorsRule

type CorsRule struct {
	CorsRuleName      string   `json:"corsRuleName"`
	AllowedOrigins    []string `json:"allowedOrigins"`
	AllowedHeaders    []string `json:"allowedHeaders"`
	AllowedOperations []string `json:"allowedOperations"`
	ExposeHeaders     []string `json:"exposeHeaders"`
	MaxAgeSeconds     int64    `json:"maxAgeSeconds"`
}

type DownloadUrlToken

type DownloadUrlToken struct {
	FileNamePrefix     string `json:"fileNamePrefix"`
	AuthorizationToken string `json:"authorizationToken"`
}

type ErrorResponse

type ErrorResponse struct {
	Code    string `json:"code"`
	Message string `json:"message"`
	Status  int64  `json:"status"`
}

type File

type File struct {
	FileId          string   `json:"fileId"`
	FileName        string   `json:"fileName"`
	ContentLength   int64    `json:"contentLength"`
	ContentType     string   `json:"contentType"`
	ContentSha1     string   `json:"contentSha1"`
	FileInfo        FileInfo `json:"fileInfo"`
	Action          string   `json:"action"`
	UploadTimestamp int64    `json:"uploadTimestamp"`
}

type FileInfo

type FileInfo map[string]interface{}

type LifecycleRule

type LifecycleRule struct {
	DaysFromHidingToDeleting  int64  `json:"daysFromHidingToDeleting,omitempty"`
	DaysFromUploadingToHiding int64  `json:"daysFromUploadingToHiding,omitempty"`
	FileNamePrefix            string `json:"fileNamePrefix"`
}

type Part

type Part struct {
	FileId          string `json:"fileId"`
	PartNumber      int64  `json:"partNumber"`
	ContentType     int64  `json:"contentType"`
	ContentSha1     string `json:"contentSha1"`
	UploadTimestamp int64  `json:"uploadTimestamp"`
}

type ProgressReaderWriter

type ProgressReaderWriter struct {
	Reader io.Reader
	Writer io.Writer
	Total  int64
	Done   int64
	Report func(int64, int64)
}

func (*ProgressReaderWriter) Read

func (prw *ProgressReaderWriter) Read(p []byte) (int, error)

func (*ProgressReaderWriter) Write

func (prw *ProgressReaderWriter) Write(p []byte) (int, error)

type UploadUrlToken

type UploadUrlToken struct {
	UploadUrl          string `json:"uploadUrl"`
	AuthorizationToken string `json:"authorizationToken"`
}

Source Files

  • auth.go
  • b2.go
  • big.go
  • bucket.go
  • download.go
  • error.go
  • file.go
  • key.go
  • models.go
  • small.go
  • test_utils.go
  • utils.go

Directories

Path Synopsis
cli
cmd

Jump to

Keyboard shortcuts

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