github.com/artpar/rclone@v1.67.3/backend/b2/api/types.go (about)

     1  // Package api provides types used by the Backblaze B2 API.
     2  package api
     3  
     4  import (
     5  	"fmt"
     6  	"strconv"
     7  	"time"
     8  
     9  	"github.com/artpar/rclone/fs/fserrors"
    10  	"github.com/artpar/rclone/lib/version"
    11  )
    12  
    13  // Error describes a B2 error response
    14  type Error struct {
    15  	Status  int    `json:"status"`  // The numeric HTTP status code. Always matches the status in the HTTP response.
    16  	Code    string `json:"code"`    // A single-identifier code that identifies the error.
    17  	Message string `json:"message"` // A human-readable message, in English, saying what went wrong.
    18  }
    19  
    20  // Error satisfies the error interface
    21  func (e *Error) Error() string {
    22  	return fmt.Sprintf("%s (%d %s)", e.Message, e.Status, e.Code)
    23  }
    24  
    25  // Fatal satisfies the Fatal interface
    26  //
    27  // It indicates which errors should be treated as fatal
    28  func (e *Error) Fatal() bool {
    29  	return e.Status == 403 // 403 errors shouldn't be retried
    30  }
    31  
    32  var _ fserrors.Fataler = (*Error)(nil)
    33  
    34  // Bucket describes a B2 bucket
    35  type Bucket struct {
    36  	ID             string          `json:"bucketId"`
    37  	AccountID      string          `json:"accountId"`
    38  	Name           string          `json:"bucketName"`
    39  	Type           string          `json:"bucketType"`
    40  	LifecycleRules []LifecycleRule `json:"lifecycleRules,omitempty"`
    41  }
    42  
    43  // LifecycleRule is a single lifecycle rule
    44  type LifecycleRule struct {
    45  	DaysFromHidingToDeleting  *int   `json:"daysFromHidingToDeleting"`
    46  	DaysFromUploadingToHiding *int   `json:"daysFromUploadingToHiding"`
    47  	FileNamePrefix            string `json:"fileNamePrefix"`
    48  }
    49  
    50  // Timestamp is a UTC time when this file was uploaded. It is a base
    51  // 10 number of milliseconds since midnight, January 1, 1970 UTC. This
    52  // fits in a 64 bit integer such as the type "long" in the programming
    53  // language Java. It is intended to be compatible with Java's time
    54  // long. For example, it can be passed directly into the java call
    55  // Date.setTime(long time).
    56  type Timestamp time.Time
    57  
    58  // MarshalJSON turns a Timestamp into JSON (in UTC)
    59  func (t *Timestamp) MarshalJSON() (out []byte, err error) {
    60  	timestamp := (*time.Time)(t).UTC().UnixNano()
    61  	return []byte(strconv.FormatInt(timestamp/1e6, 10)), nil
    62  }
    63  
    64  // UnmarshalJSON turns JSON into a Timestamp
    65  func (t *Timestamp) UnmarshalJSON(data []byte) error {
    66  	timestamp, err := strconv.ParseInt(string(data), 10, 64)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	*t = Timestamp(time.Unix(timestamp/1e3, (timestamp%1e3)*1e6).UTC())
    71  	return nil
    72  }
    73  
    74  // HasVersion returns true if it looks like the passed filename has a timestamp on it.
    75  //
    76  // Note that the passed filename's timestamp may still be invalid even if this
    77  // function returns true.
    78  func HasVersion(remote string) bool {
    79  	return version.Match(remote)
    80  }
    81  
    82  // AddVersion adds the timestamp as a version string into the filename passed in.
    83  func (t Timestamp) AddVersion(remote string) string {
    84  	return version.Add(remote, time.Time(t))
    85  }
    86  
    87  // RemoveVersion removes the timestamp from a filename as a version string.
    88  //
    89  // It returns the new file name and a timestamp, or the old filename
    90  // and a zero timestamp.
    91  func RemoveVersion(remote string) (t Timestamp, newRemote string) {
    92  	time, newRemote := version.Remove(remote)
    93  	t = Timestamp(time)
    94  	return
    95  }
    96  
    97  // IsZero returns true if the timestamp is uninitialized
    98  func (t Timestamp) IsZero() bool {
    99  	return time.Time(t).IsZero()
   100  }
   101  
   102  // Equal compares two timestamps
   103  //
   104  // If either are !IsZero then it returns false
   105  func (t Timestamp) Equal(s Timestamp) bool {
   106  	if time.Time(t).IsZero() {
   107  		return false
   108  	}
   109  	if time.Time(s).IsZero() {
   110  		return false
   111  	}
   112  	return time.Time(t).Equal(time.Time(s))
   113  }
   114  
   115  // File is info about a file
   116  type File struct {
   117  	ID              string            `json:"fileId"`          // The unique identifier for this version of this file. Used with b2_get_file_info, b2_download_file_by_id, and b2_delete_file_version.
   118  	Name            string            `json:"fileName"`        // The name of this file, which can be used with b2_download_file_by_name.
   119  	Action          string            `json:"action"`          // Either "upload" or "hide". "upload" means a file that was uploaded to B2 Cloud Storage. "hide" means a file version marking the file as hidden, so that it will not show up in b2_list_file_names. The result of b2_list_file_names will contain only "upload". The result of b2_list_file_versions may have both.
   120  	Size            int64             `json:"size"`            // The number of bytes in the file.
   121  	UploadTimestamp Timestamp         `json:"uploadTimestamp"` // This is a UTC time when this file was uploaded.
   122  	SHA1            string            `json:"contentSha1"`     // The SHA1 of the bytes stored in the file.
   123  	ContentType     string            `json:"contentType"`     // The MIME type of the file.
   124  	Info            map[string]string `json:"fileInfo"`        // The custom information that was uploaded with the file. This is a JSON object, holding the name/value pairs that were uploaded with the file.
   125  }
   126  
   127  // AuthorizeAccountResponse is as returned from the b2_authorize_account call
   128  type AuthorizeAccountResponse struct {
   129  	AbsoluteMinimumPartSize int      `json:"absoluteMinimumPartSize"` // The smallest possible size of a part of a large file.
   130  	AccountID               string   `json:"accountId"`               // The identifier for the account.
   131  	Allowed                 struct { // An object (see below) containing the capabilities of this auth token, and any restrictions on using it.
   132  		BucketID     string      `json:"bucketId"`     // When present, access is restricted to one bucket.
   133  		BucketName   string      `json:"bucketName"`   // When present, name of bucket - may be empty
   134  		Capabilities []string    `json:"capabilities"` // A list of strings, each one naming a capability the key has.
   135  		NamePrefix   interface{} `json:"namePrefix"`   // When present, access is restricted to files whose names start with the prefix
   136  	} `json:"allowed"`
   137  	APIURL              string `json:"apiUrl"`              // The base URL to use for all API calls except for uploading and downloading files.
   138  	AuthorizationToken  string `json:"authorizationToken"`  // An authorization token to use with all calls, other than b2_authorize_account, that need an Authorization header.
   139  	DownloadURL         string `json:"downloadUrl"`         // The base URL to use for downloading files.
   140  	MinimumPartSize     int    `json:"minimumPartSize"`     // DEPRECATED: This field will always have the same value as recommendedPartSize. Use recommendedPartSize instead.
   141  	RecommendedPartSize int    `json:"recommendedPartSize"` // The recommended size for each part of a large file. We recommend using this part size for optimal upload performance.
   142  }
   143  
   144  // ListBucketsRequest is parameters for b2_list_buckets call
   145  type ListBucketsRequest struct {
   146  	AccountID   string   `json:"accountId"`             // The identifier for the account.
   147  	BucketID    string   `json:"bucketId,omitempty"`    // When specified, the result will be a list containing just this bucket.
   148  	BucketName  string   `json:"bucketName,omitempty"`  // When specified, the result will be a list containing just this bucket.
   149  	BucketTypes []string `json:"bucketTypes,omitempty"` // If present, B2 will use it as a filter for bucket types returned in the list buckets response.
   150  }
   151  
   152  // ListBucketsResponse is as returned from the b2_list_buckets call
   153  type ListBucketsResponse struct {
   154  	Buckets []Bucket `json:"buckets"`
   155  }
   156  
   157  // ListFileNamesRequest is as passed to b2_list_file_names or b2_list_file_versions
   158  type ListFileNamesRequest struct {
   159  	BucketID      string `json:"bucketId"`                // required - The bucket to look for file names in.
   160  	StartFileName string `json:"startFileName,omitempty"` // optional - The first file name to return. If there is a file with this name, it will be returned in the list. If not, the first file name after this the first one after this name.
   161  	MaxFileCount  int    `json:"maxFileCount,omitempty"`  // optional - The maximum number of files to return from this call. The default value is 100, and the maximum allowed is 1000.
   162  	StartFileID   string `json:"startFileId,omitempty"`   // optional - What to pass in to startFileId for the next search to continue where this one left off.
   163  	Prefix        string `json:"prefix,omitempty"`        // optional - Files returned will be limited to those with the given prefix. Defaults to the empty string, which matches all files.
   164  	Delimiter     string `json:"delimiter,omitempty"`     // Files returned will be limited to those within the top folder, or any one subfolder. Defaults to NULL. Folder names will also be returned. The delimiter character will be used to "break" file names into folders.
   165  }
   166  
   167  // ListFileNamesResponse is as received from b2_list_file_names or b2_list_file_versions
   168  type ListFileNamesResponse struct {
   169  	Files        []File  `json:"files"`        // An array of objects, each one describing one file.
   170  	NextFileName *string `json:"nextFileName"` // What to pass in to startFileName for the next search to continue where this one left off, or null if there are no more files.
   171  	NextFileID   *string `json:"nextFileId"`   // What to pass in to startFileId for the next search to continue where this one left off, or null if there are no more files.
   172  }
   173  
   174  // GetUploadURLRequest is passed to b2_get_upload_url
   175  type GetUploadURLRequest struct {
   176  	BucketID string `json:"bucketId"` // The ID of the bucket that you want to upload to.
   177  }
   178  
   179  // GetUploadURLResponse is received from b2_get_upload_url
   180  type GetUploadURLResponse struct {
   181  	BucketID           string `json:"bucketId"`           // The unique ID of the bucket.
   182  	UploadURL          string `json:"uploadUrl"`          // The URL that can be used to upload files to this bucket, see b2_upload_file.
   183  	AuthorizationToken string `json:"authorizationToken"` // The authorizationToken that must be used when uploading files to this bucket, see b2_upload_file.
   184  }
   185  
   186  // GetDownloadAuthorizationRequest is passed to b2_get_download_authorization
   187  type GetDownloadAuthorizationRequest struct {
   188  	BucketID               string `json:"bucketId"`                       // The ID of the bucket that you want to upload to.
   189  	FileNamePrefix         string `json:"fileNamePrefix"`                 // The file name prefix of files the download authorization token will allow access to.
   190  	ValidDurationInSeconds int64  `json:"validDurationInSeconds"`         // The number of seconds before the authorization token will expire. The minimum value is 1 second. The maximum value is 604800 which is one week in seconds.
   191  	B2ContentDisposition   string `json:"b2ContentDisposition,omitempty"` // optional - If this is present, download requests using the returned authorization must include the same value for b2ContentDisposition.
   192  }
   193  
   194  // GetDownloadAuthorizationResponse is received from b2_get_download_authorization
   195  type GetDownloadAuthorizationResponse struct {
   196  	BucketID           string `json:"bucketId"`           // The unique ID of the bucket.
   197  	FileNamePrefix     string `json:"fileNamePrefix"`     // The file name prefix of files the download authorization token will allow access to.
   198  	AuthorizationToken string `json:"authorizationToken"` // The authorizationToken that must be used when downloading files, see b2_download_file_by_name.
   199  }
   200  
   201  // FileInfo is received from b2_upload_file, b2_get_file_info and b2_finish_large_file
   202  type FileInfo struct {
   203  	ID              string            `json:"fileId"`          // The unique identifier for this version of this file. Used with b2_get_file_info, b2_download_file_by_id, and b2_delete_file_version.
   204  	Name            string            `json:"fileName"`        // The name of this file, which can be used with b2_download_file_by_name.
   205  	Action          string            `json:"action"`          // Either "upload" or "hide". "upload" means a file that was uploaded to B2 Cloud Storage. "hide" means a file version marking the file as hidden, so that it will not show up in b2_list_file_names. The result of b2_list_file_names will contain only "upload". The result of b2_list_file_versions may have both.
   206  	AccountID       string            `json:"accountId"`       // Your account ID.
   207  	BucketID        string            `json:"bucketId"`        // The bucket that the file is in.
   208  	Size            int64             `json:"contentLength"`   // The number of bytes stored in the file.
   209  	UploadTimestamp Timestamp         `json:"uploadTimestamp"` // This is a UTC time when this file was uploaded.
   210  	SHA1            string            `json:"contentSha1"`     // The SHA1 of the bytes stored in the file.
   211  	ContentType     string            `json:"contentType"`     // The MIME type of the file.
   212  	Info            map[string]string `json:"fileInfo"`        // The custom information that was uploaded with the file. This is a JSON object, holding the name/value pairs that were uploaded with the file.
   213  }
   214  
   215  // CreateBucketRequest is used to create a bucket
   216  type CreateBucketRequest struct {
   217  	AccountID      string          `json:"accountId"`
   218  	Name           string          `json:"bucketName"`
   219  	Type           string          `json:"bucketType"`
   220  	LifecycleRules []LifecycleRule `json:"lifecycleRules,omitempty"`
   221  }
   222  
   223  // DeleteBucketRequest is used to create a bucket
   224  type DeleteBucketRequest struct {
   225  	ID        string `json:"bucketId"`
   226  	AccountID string `json:"accountId"`
   227  }
   228  
   229  // DeleteFileRequest is used to delete a file version
   230  type DeleteFileRequest struct {
   231  	ID   string `json:"fileId"`   // The ID of the file, as returned by b2_upload_file, b2_list_file_names, or b2_list_file_versions.
   232  	Name string `json:"fileName"` // The name of this file.
   233  }
   234  
   235  // HideFileRequest is used to delete a file
   236  type HideFileRequest struct {
   237  	BucketID string `json:"bucketId"` // The bucket containing the file to hide.
   238  	Name     string `json:"fileName"` // The name of the file to hide.
   239  }
   240  
   241  // GetFileInfoRequest is used to return a FileInfo struct with b2_get_file_info
   242  type GetFileInfoRequest struct {
   243  	ID string `json:"fileId"` // The ID of the file, as returned by b2_upload_file, b2_list_file_names, or b2_list_file_versions.
   244  }
   245  
   246  // StartLargeFileRequest (b2_start_large_file) Prepares for uploading the parts of a large file.
   247  //
   248  // If the original source of the file being uploaded has a last
   249  // modified time concept, Backblaze recommends using
   250  // src_last_modified_millis as the name, and a string holding the base
   251  // 10 number of milliseconds since midnight, January 1, 1970
   252  // UTC. This fits in a 64 bit integer such as the type "long" in the
   253  // programming language Java. It is intended to be compatible with
   254  // Java's time long. For example, it can be passed directly into the
   255  // Java call Date.setTime(long time).
   256  //
   257  // If the caller knows the SHA1 of the entire large file being
   258  // uploaded, Backblaze recommends using large_file_sha1 as the name,
   259  // and a 40 byte hex string representing the SHA1.
   260  //
   261  // Example: { "src_last_modified_millis" : "1452802803026", "large_file_sha1" : "a3195dc1e7b46a2ff5da4b3c179175b75671e80d", "color": "blue" }
   262  type StartLargeFileRequest struct {
   263  	BucketID    string            `json:"bucketId"`    //The ID of the bucket that the file will go in.
   264  	Name        string            `json:"fileName"`    // The name of the file. See Files for requirements on file names.
   265  	ContentType string            `json:"contentType"` // The MIME type of the content of the file, which will be returned in the Content-Type header when downloading the file. Use the Content-Type b2/x-auto to automatically set the stored Content-Type post upload. In the case where a file extension is absent or the lookup fails, the Content-Type is set to application/octet-stream.
   266  	Info        map[string]string `json:"fileInfo"`    // A JSON object holding the name/value pairs for the custom file info.
   267  }
   268  
   269  // StartLargeFileResponse is the response to StartLargeFileRequest
   270  type StartLargeFileResponse struct {
   271  	ID              string            `json:"fileId"`          // The unique identifier for this version of this file. Used with b2_get_file_info, b2_download_file_by_id, and b2_delete_file_version.
   272  	Name            string            `json:"fileName"`        // The name of this file, which can be used with b2_download_file_by_name.
   273  	AccountID       string            `json:"accountId"`       // The identifier for the account.
   274  	BucketID        string            `json:"bucketId"`        // The unique ID of the bucket.
   275  	ContentType     string            `json:"contentType"`     // The MIME type of the file.
   276  	Info            map[string]string `json:"fileInfo"`        // The custom information that was uploaded with the file. This is a JSON object, holding the name/value pairs that were uploaded with the file.
   277  	UploadTimestamp Timestamp         `json:"uploadTimestamp"` // This is a UTC time when this file was uploaded.
   278  }
   279  
   280  // GetUploadPartURLRequest is passed to b2_get_upload_part_url
   281  type GetUploadPartURLRequest struct {
   282  	ID string `json:"fileId"` // The unique identifier of the file being uploaded.
   283  }
   284  
   285  // GetUploadPartURLResponse is received from b2_get_upload_url
   286  type GetUploadPartURLResponse struct {
   287  	ID                 string `json:"fileId"`             // The unique identifier of the file being uploaded.
   288  	UploadURL          string `json:"uploadUrl"`          // The URL that can be used to upload files to this bucket, see b2_upload_part.
   289  	AuthorizationToken string `json:"authorizationToken"` // The authorizationToken that must be used when uploading files to this bucket, see b2_upload_part.
   290  }
   291  
   292  // UploadPartResponse is the response to b2_upload_part
   293  type UploadPartResponse struct {
   294  	ID         string `json:"fileId"`        // The unique identifier of the file being uploaded.
   295  	PartNumber int64  `json:"partNumber"`    // Which part this is (starting from 1)
   296  	Size       int64  `json:"contentLength"` // The number of bytes stored in the file.
   297  	SHA1       string `json:"contentSha1"`   // The SHA1 of the bytes stored in the file.
   298  }
   299  
   300  // FinishLargeFileRequest is passed to b2_finish_large_file
   301  //
   302  // The response is a FileInfo object (with extra AccountID and BucketID fields which we ignore).
   303  //
   304  // Large files do not have a SHA1 checksum. The value will always be "none".
   305  type FinishLargeFileRequest struct {
   306  	ID    string   `json:"fileId"`        // The unique identifier of the file being uploaded.
   307  	SHA1s []string `json:"partSha1Array"` // A JSON array of hex SHA1 checksums of the parts of the large file. This is a double-check that the right parts were uploaded in the right order, and that none were missed. Note that the part numbers start at 1, and the SHA1 of the part 1 is the first string in the array, at index 0.
   308  }
   309  
   310  // CancelLargeFileRequest is passed to b2_finish_large_file
   311  //
   312  // The response is a CancelLargeFileResponse
   313  type CancelLargeFileRequest struct {
   314  	ID string `json:"fileId"` // The unique identifier of the file being uploaded.
   315  }
   316  
   317  // CancelLargeFileResponse is the response to CancelLargeFileRequest
   318  type CancelLargeFileResponse struct {
   319  	ID        string `json:"fileId"`    // The unique identifier of the file being uploaded.
   320  	Name      string `json:"fileName"`  // The name of this file.
   321  	AccountID string `json:"accountId"` // The identifier for the account.
   322  	BucketID  string `json:"bucketId"`  // The unique ID of the bucket.
   323  }
   324  
   325  // CopyFileRequest is as passed to b2_copy_file
   326  type CopyFileRequest struct {
   327  	SourceID          string            `json:"sourceFileId"`                  // The ID of the source file being copied.
   328  	Name              string            `json:"fileName"`                      // The name of the new file being created.
   329  	Range             string            `json:"range,omitempty"`               // The range of bytes to copy. If not provided, the whole source file will be copied.
   330  	MetadataDirective string            `json:"metadataDirective,omitempty"`   // The strategy for how to populate metadata for the new file: COPY or REPLACE
   331  	ContentType       string            `json:"contentType,omitempty"`         // The MIME type of the content of the file (REPLACE only)
   332  	Info              map[string]string `json:"fileInfo,omitempty"`            // This field stores the metadata that will be stored with the file. (REPLACE only)
   333  	DestBucketID      string            `json:"destinationBucketId,omitempty"` // The destination ID of the bucket if set, if not the source bucket will be used
   334  }
   335  
   336  // CopyPartRequest is the request for b2_copy_part - the response is UploadPartResponse
   337  type CopyPartRequest struct {
   338  	SourceID    string `json:"sourceFileId"`    // The ID of the source file being copied.
   339  	LargeFileID string `json:"largeFileId"`     // The ID of the large file the part will belong to, as returned by b2_start_large_file.
   340  	PartNumber  int64  `json:"partNumber"`      // Which part this is (starting from 1)
   341  	Range       string `json:"range,omitempty"` // The range of bytes to copy. If not provided, the whole source file will be copied.
   342  }
   343  
   344  // UpdateBucketRequest describes a request to modify a B2 bucket
   345  type UpdateBucketRequest struct {
   346  	ID             string          `json:"bucketId"`
   347  	AccountID      string          `json:"accountId"`
   348  	Type           string          `json:"bucketType,omitempty"`
   349  	LifecycleRules []LifecycleRule `json:"lifecycleRules,omitempty"`
   350  }