b2_copy_part

IN BETA - COMING SOON!

This API endpoint is in development. The API and this documentation is subject to change before this feature is released.

Creates a new large file part by copying from an existing file.

When calling b2_copy_part, 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 source file must be in the same bucket as the large file being created.

Request

The upload request is a POST.

Request HTTP Headers

Authorization

required

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.

Request HTTP Message Body Parameters

sourceFileId

required

The ID of the source file being copied.

largeFileId

required

The ID of the large file the created file will belong to.

partNumber

required

Indicates which part of the large file the created file is.

range

optional

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

Response

Response HTTP Status 200

File successfully copied. The JSON response will contain the standard part information. For copied files, the action is always "copy".

fileId

The file ID for uploading this file.

partNumber

Which part this is.

contentLength

The number of bytes stored in the part.

contentSha1

The SHA1 of the bytes stored in the part.

uploadTimestamp

This is a UTC time when this part 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).

Response Errors

File not copied. If possible the server will return a JSON error structure. Errors include:

status

code

description

400

bad_request

The request had the wrong fields or illegal values. The message returned with the error will describe the problem.

401

unauthorized

The 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.

401

bad_auth_token

The auth token used is not valid. Call b2_authorize_account again to either get a new one, or an error message describing the problem.

401

expired_auth_token

The auth token used has expired. Call b2_authorize_account again to get a new one.

403

cap_exceeded

Usage cap exceeded.

404

not_found

File is not in B2 Cloud Storage.

405

method_not_allowed

Only POST is supported

408

request_timeout

The service timed out reading the uploaded file

416

range_not_satisfiable

The Range header in the request is outside the size of the file.

Sample Code

Code

ACCOUNT_AUTHORIZATION_TOKEN=''; # Provided by b2_authorize_account
API_URL=''; # Provided by b2_authorize_account
SOURCE_FILE_ID=''; # The file you wish to copy
LARGE_FILE_ID=''; # Provided by b2_start_large_file
curl \
    -H "Authorization: $ACCOUNT_AUTHORIZATION_TOKEN" \
    -d "`printf '{"sourceFileId":"%s","largeFileId":"%s","partNumber":1,"range":"bytes=0-4999999"}' $SOURCE_FILE_ID $LARGE_FILE_ID`" \
    "$API_URL/b2api/v2/b2_copy_part";

Output

{
  "contentLength": 5000000,
  "contentSha1": "e8daf2cae7b77beb23a246aec2c6d3ede06089c9",
  "fileId": "LARGE_FILE_ID",
  "partNumber": 1,
  "uploadTimestamp": 1559581682460
}

Code

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class B2CopyPart {

    public static void main(String[] args) throws IOException {
        final String apiUrl = ""; // Provided by b2_authorize_account
        final String accountAuthorizationToken = ""; // Provided by b2_authorize_account
        final String sourceFileId = ""; // The file you wish to copy
        final String largeFileId = ""; // Provided by b2_start_large_file
        final String partNumber = "1";

        final URL url = new URL(apiUrl + "/b2api/v2/b2_copy_part");
        final HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        try {
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Authorization", accountAuthorizationToken);

            connection.setDoOutput(true);
            DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
            writer.write((
                    "{"
                            + "\"sourceFileId\": \"" + sourceFileId + "\","
                            + "\"largeFileId\": \"" + largeFileId + "\","
                            + "\"partNumber\": " + partNumber
                            + "}").getBytes(StandardCharsets.UTF_8)
            );
            final InputStream in = new BufferedInputStream(connection.getInputStream());
            final String jsonResponse = inputStreamToString(in);

            System.out.println(jsonResponse);
        } finally {
            connection.disconnect();
        }
    }

    private static String inputStreamToString(InputStream in) throws IOException {
        InputStreamReader reader = new InputStreamReader(in);
        StringBuilder sb = new StringBuilder();
        int c = reader.read();
        while (c != -1) {
            sb.append((char) c);
            c = reader.read();
        }
        reader.close();
        return sb.toString();
    }

}

Output

{
  "contentLength": 99212871,
  "contentSha1": "c604542967a93466d4ed087956753161cfab3f31",
  "fileId": "LARGE_FILE_ID",
  "partNumber": 1,
  "uploadTimestamp": 1559584243788
}

Code

import json
import urllib2

api_url = '' # Provided by b2_authorize_account
account_authorization_token = '' # Provided by b2_authorize_account
source_file_id = '' # The file you wish to copy
large_file_id = '' # Provided by b2_start_large_file
part_number = 1
request = urllib2.Request(
    '%s/b2api/v2/b2_copy_part' % api_url,
    json.dumps({
      'sourceFileId' : source_file_id,
      'largeFileId': large_file_id,
      'partNumber': part_number,
    }),
    headers = {
        'Authorization': account_authorization_token
    })
response = urllib2.urlopen(request)
response_data = json.loads(response.read())
print(json.dumps(response_data, indent=2))
response.close()

Output

{
  "uploadTimestamp": 1559585618236,
  "contentLength": 99212871,
  "fileId": "LARGE_FILE_ID",
  "partNumber": 1,
  "contentSha1": "c604542967a93466d4ed087956753161cfab3f31"
}

Code

under construction

Output

under construction

Code

under construction

Output

under construction

Code

under construction

Output

under construction

Code

under construction

Output

under construction