b2_copy_file
    • Dark
      Light

    b2_copy_file

    • Dark
      Light

    Article Summary

    Post
    /b2api/v3/b2_copy_file

    Copies an entire file

    When calling b2_copy_file, by default the entire source file will be copied to the destination. If you wish, you may supply a range param to only copy a portion of the source file over. Note that the resulting file must be less than 5GB. If you wish to create a file larger than 5GB, you must do it in parts with b2_copy_part. See Large Files for more info.

    If copying between two buckets, note that they must belong to the same account.


    Warning
    Do not include Protected Health Information (PHI) or Personally Identifiable Information (PII) in bucket names; object, file, or folder names; or other metadata. This metadata is not encrypted in a way that meets Health Insurance Portability and Accountability Act (HIPAA) protection requirements for PHI/PII data, and it is not generally encrypted in client-side encryption architectures.

    File Metadata

    When copying files, you have the option to either copy the source file's metadata over to the new file, OR generate new metadata (but not both). Use the metadataDirective to indicate which strategy you wish to use:


    • COPY will set the new file's metadata to be equal to the source file's metadata.
    • REPLACE will ignore the source file's metadata, and set what you provide.

    Note that a 400 will be returned in the following two scenarios:


    • The metadataDirective is COPY and contentType is set to a non-null value.
    • The metadataDirective is REPLACE and contentType is not set, or is null.

    PLEASE NOTE:

    This API endpoint can be called using a GET request by converting the parameters in the request body to query parameters.

    Header parameters
    Authorization
    stringRequired

    An account authorization token, obtained from b2_authorize_account.
    The token must have the writeFiles capability and the readFiles capability if the bucket is private.

    Body parameters
    Expand All
    object
    sourceFileId
    string Required

    The ID of the source file being copied.

    Example4_za71f544e781e6891531b001a_f200ec353a2184825_d20160409_m004829_c000_v0001016_t0028
    destinationBucketId
    string

    The ID of the bucket where the copied file will be stored. If this is not set, the copied file will be added to the same bucket as the source file. Note that the bucket containing the source file and the destination bucket must belong to the same account.

    ExampleBUCKET_ID
    fileName
    string Required

    The name of the new file being created.

    Example4a48fe8875c6214145260818
    range
    string

    The range of bytes to copy. If not provided, the whole source file will be copied.

    Examplebytes=0-4999999
    metadataDirective
    string

    The strategy for how to populate metadata for the new file. If COPY is the indicated strategy, then supplying the contentType or fileInfo param is an error.

    Example4_za71f544e781e6891531b001a_f200ec353a2184825_d20160409_m004829_c000_v0001016_t0028
    contentType
    string

    Must only be supplied if the metadataDirective is REPLACE. 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. The Content-Type mappings can be perused here.

    Exampleapplication/json
    fileInfo
    object

    Must only be supplied if the metadataDirective is REPLACE. This field stores the metadata that will be stored with the file. It follows the same rules that are applied to b2_upload_file.

    Example{ "src_last_modified_millis": "1535629029000" }
    fileRetention
    object

    If present, specifies the Object Lock retention settings for the new file. Setting the value requires the writeFileRetentions capability and that the destination bucket is Object Lock-enabled. See Object Lock for details.

    Example{ "mode": "compliance", "retainUntilTimestamp": "1628942493000" }
    mode
    string
    Valid values[ "compliance", "governance" ]
    retainUntilTimestamp
    integer
    legalHold
    string

    If present, specifies the Object Lock legal hold status for the new file. Setting the value requires the writeFileLegalHolds capability and that the destination bucket is Object Lock-enabled. Object Lock for details.

    Exampleon
    sourceServerSideEncryption
    object

    If present, specifies the parameters for Backblaze B2 to use for accessing the source file data using Server-Side Encryption. This parameter is required if and only if the source file has been encrypted using Server-Side Encryption with Customer-Managed Keys (SSE-C), and the provided encryption key must match the one with which the source file was encrypted. See Server-Side Encryption for details.

    Example{ "mode": "SSE-C", "algorithm": "AES256", "customerKey": "<base64-encoded AES-256 encryption key>", "customerKeyMd5": "<base64-encoded MD5 digest of the key>" }
    mode
    string
    algorithm
    string
    customerKey
    string
    customerKeyMd5
    string
    destinationServerSideEncryption
    object

    If present, specifies the parameters for Backblaze B2 to use for encrypting the copied data before storing the destination file using Server-Side Encryption. See Server-Side Encryption for details.

    Example{ "mode": "SSE-C", "algorithm": "AES256", "customerKey": "<base64-encoded AES-256 encryption key>", "customerKeyMd5": "<base64-encoded MD5 digest of the key>" }
    mode
    string
    algorithm
    string
    customerKey
    string
    customerKeyMd5
    string
    Responses
    200

    The request succeeded.

    Array of object
    object
    accountId
    string

    The account that owns the file.

    ExampleACCOUNT_ID
    action
    string

    One of "start", "upload", "hide", "folder", or other values added in the future. "upload" means a file that was uploaded to B2 Cloud Storage. "start" means that a large file has been started, but not finished or canceled. "hide" means a file version marking the file as hidden, so that it will not show up in b2_list_file_names. "folder" is used to indicate a virtual folder when listing files.

    Examplecopy
    bucketId
    string

    The unique ID of the bucket.

    ExampleBUCKET_ID
    contentLength
    string

    The number of bytes stored in the file. Only useful when the action is "upload". Always 0 when the action is "start", "hide", or "folder".

    Example99212871
    contentSha1
    string

    The SHA1 of the bytes stored in the file as a 40-digit hex string. Large files do not have SHA1 checksums, and the value is "none". The value is null when the action is "hide" or "folder".

    Examplec604542967a93466d4ed087956753161cfab3f31
    contentMd5
    string

    The MD5 of the bytes stored in the file as a 32-digit hex string. Not all files have an MD5 checksum, so this field is optional, and set to null for files that do not have one. Large files do not have MD5 checksums, and the value is null. The value is also null when the action is "hide" or "folder".

    Exampledc724af18fbdd4e59189f5fe768a5f8311527050
    contentType
    string

    When the action is "upload" or "start", the MIME type of the file, as specified when the file was uploaded. For "hide" action, always "application/x-bz-hide-marker". For "folder" action, always null.

    Example4a48fe8875c6214145260818
    fileId
    string

    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. The value is null when for action "folder".

    Examplebigfile.dat
    fileInfo
    object

    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.

    Example{ "src_last_modified_millis": "1554914807970" }
    fileName
    string

    The name of this file, which can be used with b2_download_file_by_name.

    Exampletest-b2-copy-file-python
    fileRetention
    object

    The Object Lock retention settings for this file, if any. This field is filtered based on application key capabilities; the readFileRetentions capability is required to access the value. See Object Lock for more details on response structure. This field is omitted when the action is "hide" or "folder".

    Example{ "isClientAuthorizedToRead": true, "value": { "mode": null, "retainUntilTimestamp": null } }
    isClientAuthorizedToRead
    boolean
    value
    object
    mode
    string
    retainUntilTimestamp
    integer
    legalHold
    object

    The Object Lock legal hold status for this file, if any. This field is filtered based on application key capabilities; the readFileLegalHolds capability is required to access the value. See Object Lock for more details on response structure. This field is omitted when the action is "hide" or "folder".

    Example{ "isClientAuthorizedToRead": true, "value": null }
    isClientAuthorizedToRead
    boolean
    value
    string
    replicationStatus
    string

    The Replication Status for this file, if any. This will show either PENDING, COMPLETED, FAILED, or REPLICA For details see Cloud Replication. This field is omitted when the file is not part of a replication rule.

    Examplebigfile.dat
    serverSideEncryption
    object

    When the file is encrypted with Server-Side Encryption, the mode ("SSE-B2" or "SSE-C") and algorithm used to encrypt the data. If the file is not encrypted with Server-Side Encryption, then both mode and algorithm will be null. This field is omitted when the action is "hide" or "folder".

    Example{ "algorithm": null, "mode": null }
    algorithm
    string
    mode
    string
    uploadTimestamp
    integer

    This is a UTC time when this file was uploaded. It is a base 10 number of milliseconds since midnight, January 1, 1970 UTC. This fits in a 64 bit integer such as the type "long" in the programming language Java. It is intended to be compatible with Java's time long. For example, it can be passed directly into the java call Date.setTime(long time). Always 0 when the action is "folder".

    Example1559585435000
    400
    statuscodedescription
    400bad_bucket_idThe requested bucket ID does not match an existing bucket.
    400bad_requestThe request had the wrong fields or illegal values. The message returned with the error will describe the problem.
    400source_too_largeThe source file being copied is too large.
    object
    status
    integer

    The numeric HTTP status code. Always matches the status in the HTTP response.

    Example400
    code
    string

    A single-identifier code that identifies the error.

    Exampleinvalid_bucket_name
    message
    string

    A human-readable message, in English, saying what went wrong.

    Examplebucket name is too long
    401
    statuscodedescription
    401bad_auth_tokenThe auth token used is not valid. Call b2_authorize_account again to either get a new one, or an error message describing the problem.
    401expired_auth_tokenThe auth token used has expired. Call b2_authorize_account again to get a new one.
    401unauthorizedThe auth token used is valid, but does not authorize this call with these parameters. The capabilities of an auth token are determined by the application key used with b2_authorize_account.
    403
    statuscodedescription
    403access_deniedThe provided customer-managed encryption key is wrong.
    403transaction_cap_exceededTransaction cap exceeded. To increase your cap, sign in to your B2 Cloud Storage account online. Then select the Caps & Alerts link in the B2 Cloud Storage section of the sidebar.
    404
    statuscodedescription
    404not_foundFile is not in B2 Cloud Storage.
    405
    statuscodedescription
    405method_not_allowedmethod not allowed: <HTTP METHOD>
    408
    statuscodedescription
    408request_timeoutThe service timed out reading the uploaded file
    416
    statuscodedescription
    416range_not_satisfiableThe Range header in the request is valid but cannot be satisfied for the file.

    Was this article helpful?

    What's Next