b2_get_upload_part_url

Gets an URL to use for uploading parts of a large file.

When you upload part of a large file to B2, you must call b2_get_upload_part_url first to get the URL for uploading. Then, you use b2_upload_part on this URL to upload your data.

An uploadUrl and upload authorizationToken are valid for 24 hours or until the endpoint rejects an upload, see b2_upload_file. You can upload as many files to this URL as you need. To achieve faster upload speeds, request multiple uploadUrls and upload your files to these different endpoints in parallel.

Request

Request HTTP Headers

Authorization

required

An account authorization token, obtained from b2_authorize_account.

Request HTTP Message Body Parameters

fileId

required

The ID of the large file whose parts you want to upload.

Response

Response HTTP Status 200

Information for uploading file parts. The JSON response will contain:

fileId

The unique ID of file being uploaded.

uploadUrl

The URL that can be used to upload parts of this file, see b2_upload_part.

authorizationToken

The authorizationToken that must be used when uploading files with this URL. This token is valid for 24 hours or until the uploadUrl endpoint rejects an upload, see b2_upload_part

Response Errors

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

status

code

message

400

bad_request

Not a valid file id: <fileId>

400

bad_request

No active upload for: <fileId>

400

bad_request

Wrong host name for request

401

missing_auth_token

Authorization token is missing

401

expired_auth_token

Authorization token has expired

401

bad_auth_token

Invalid authorization token

401

bad_auth_token

Authorization token for wrong cluster

503

service_unavailable

No uploads available in vault <vaultId>

Sample Code

Code

FILE_ID=''; # Provided by b2_start_large_file
ACCOUNT_AUTHORIZATION_TOKEN=''; # Provided by b2_authorize_account
API_URL=''; # Provided by b2_authorize_account
curl \
    -H "Authorization: $ACCOUNT_AUTHORIZATION_TOKEN" \
    -d "`printf '{"fileId":"%s"}' $FILE_ID`" \
    "$API_URL/b2api/v1/b2_get_upload_part_url"

Output

{
  "authorizationToken": "3_20160409004829_42b8f80ba60fb4323dcaad98_ec81302316fccc2260201cbf17813247f312cf3b_000_uplg",
  "fileId": "4_ze73ede9c9c8412db49f60715_f100b4e93fbae6252_d20150824_m224353_c900_v8881000_t0001",
  "uploadUrl": "https://pod-000-1016-09.backblaze.com/b2api/v1/b2_upload_part/4_ze73ede9c9c8412db49f60715_f100b4e93fbae6252_d20150824_m224353_c900_v8881000_t0001/0037"
}

Code

import java.io.*;
import java.util.*;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.json.*;

String fileId = ""; // Provided by b2_start_large_file
String accountAuthorizationToken = ""; // Provided by b2_authorize_account
String apiUrl = ""; // Provided by b2_authorize_account

// Get Upload Part URL
// Create a model
JsonObject getUploadPartJsonObj = Json.createObjectBuilder()
        .add("fileId", fileId)
        .build();

// Write the model to stream
StringWriter sw = new StringWriter();
JsonWriter jw = Json.createWriter(sw);
jsonWriter.writeObject(getUploadPartJsonObj);
jsonWriter.close();
postData = sw.toString();
try {
    URL url = new URL(apiUrl + "/b2api/v1/b2_get_upload_part_url");
    connection = (HttpURLConnection)url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Authorization", accountAuthorizationToken);
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setRequestProperty("Charset", "UTF-8");
    connection.setRequestProperty("Content-Length", Integer.toString(postData.length()));
    connection.setDoOutput(true);
    DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
    dataOutputStream.writeBytes(postData);
    dataOutputStream.close();
    String jsonResponse = myInputStreamReader(connection.getInputStream());
    System.out.println(jsonResponse);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    connection.disconnect();
}

// Input stream reader example. 
static public String myInputStreamReader(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

{
  "authorizationToken": "3_20160409004829_42b8f80ba60fb4323dcaad98_ec81302316fccc2260201cbf17813247f312cf3b_000_uplg",
  "fileId": "4_ze73ede9c9c8412db49f60715_f100b4e93fbae6252_d20150824_m224353_c900_v8881000_t0001",
  "uploadUrl": "https://pod-000-1016-09.backblaze.com/b2api/v1/b2_upload_part/4_ze73ede9c9c8412db49f60715_f100b4e93fbae6252_d20150824_m224353_c900_v8881000_t0001/0037"
}

Code

import base64
import json
import urllib2

file_id = "" # Provided by b2_start_large_file
request = urllib2.Request(
    '%s/b2api/v1/b2_get_upload_part_url' % api_url,
    json.dumps({ 'fileId' : file_id }),
    headers = { 'Authorization': account_authorization_token }
)
response = urllib2.urlopen(request)
response_data = json.loads(response.read())
response.close()

Output

{
  "authorizationToken": "3_20160409004829_42b8f80ba60fb4323dcaad98_ec81302316fccc2260201cbf17813247f312cf3b_000_uplg",
  "fileId": "4_ze73ede9c9c8412db49f60715_f100b4e93fbae6252_d20150824_m224353_c900_v8881000_t0001",
  "uploadUrl": "https://pod-000-1016-09.backblaze.com/b2api/v1/b2_upload_part/4_ze73ede9c9c8412db49f60715_f100b4e93fbae6252_d20150824_m224353_c900_v8881000_t0001/0037"
}

Code

import Foundation

apiUrl = "" // Provided by b2_authorize_account
fileId = "" // The file ID provided by b2_start_large_file
let request = NSMutableURLRequest(URL:NSURL(string: "\(apiUrl)")!.URLByAppendingPathComponent("/b2api/v1/b2_get_upload_part_url"))
request.HTTPMethod = "POST"
request.addValue(config.accountAuthorizationToken!, forHTTPHeaderField: "Authorization")
request.HTTPBody = try "{\"fileId\":\"\(fileId)\"}".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let task = session.dataTaskWithRequest(request, completionHandler:{ (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
    if let jsonData = data {
        let json = NSString(data: jsonData, encoding:NSUTF8StringEncoding)
        print(json)
    }
})
task.resume()

Output

{
  "authorizationToken": "3_20160409004829_42b8f80ba60fb4323dcaad98_ec81302316fccc2260201cbf17813247f312cf3b_000_uplg",
  "fileId": "4_ze73ede9c9c8412db49f60715_f100b4e93fbae6252_d20150824_m224353_c900_v8881000_t0001",
  "uploadUrl": "https://pod-000-1016-09.backblaze.com/b2api/v1/b2_upload_part/4_ze73ede9c9c8412db49f60715_f100b4e93fbae6252_d20150824_m224353_c900_v8881000_t0001/0037"
}

Code

require 'json'
require 'net/http'

api_url = "" # Provided by b2_authorize_account
file_id = "" # Provided by b2_start_large_file
account_authorization_token = "" # Provided by b2_authorize_account
uri = URI.join("#{api_url}/b2api/v1/b2_get_upload_part_url")
req = Net::HTTP::Post.new(uri)
req.add_field("Authorization","#{account_authorization_token}")
req.body = "{\"fileId\":\"#{file_id}\"}"
http = Net::HTTP.new(req.uri.host, req.uri.port)
http.use_ssl = true
res = http.start {|http| http.request(req)}
case res
when Net::HTTPSuccess then
    res.body
when Net::HTTPRedirection then
    fetch(res['location'], limit - 1)
else
    res.error!
end

Output

{
  "authorizationToken": "3_20160409004829_42b8f80ba60fb4323dcaad98_ec81302316fccc2260201cbf17813247f312cf3b_000_uplg",
  "fileId": "4_ze73ede9c9c8412db49f60715_f100b4e93fbae6252_d20150824_m224353_c900_v8881000_t0001",
  "uploadUrl": "https://pod-000-1016-09.backblaze.com/b2api/v1/b2_upload_part/4_ze73ede9c9c8412db49f60715_f100b4e93fbae6252_d20150824_m224353_c900_v8881000_t0001/0037"
}

Code

using System;
using System.Net;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;

String apiUrl = ""; // Provided by b2_authorize_account
String fileId = ""; // Provided by b2_start_large_file
String authorizationToken = ""; // Provided by b2_authorize_account

// Get Upload URL
String getUploadUrlJsonStr = "{\"fileId\":\"" + fileId + "\"}";
byte[] getUloadUrlJsonData = Encoding.UTF8.GetBytes(getUploadUrlJsonStr);
HttpWebRequest getUploadUrlRequest = (HttpWebRequest)WebRequest.Create(apiUrl + "/b2api/v1/b2_get_upload_part_url");
getUploadUrlRequest.Method = "POST";
getUploadUrlRequest.Headers.Add("Authorization", authorizationToken);
getUploadUrlRequest.ContentType = "application/json; charset=utf-8";
getUploadUrlRequest.ContentLength = getUloadUrlJsonData.Length;
using (Stream stream = getUploadUrlRequest.GetRequestStream())
{
    stream.Write(getUloadUrlJsonData, 0, getUloadUrlJsonData.Length);
    stream.Close();
}

// Handle the response and print the json
try
{
    HttpWebResponse getUploadUrlResponse = (HttpWebResponse)getUploadUrlRequest.GetResponse();
	using(StringReader responseReader = new StreamReader(getUploadUrlResponse.GetResponseStream()))
    {
    	String json = responseReader.ReadToEnd();
    	Console.WriteLine(json);
    }
    getUploadUrlResponse.Close();    
}
catch (WebException e)
{
    using (HttpWebResponse errorResponse = (HttpWebResponse)e.Response)
    {
        Console.WriteLine("Error code: {0}", errorResponse.StatusCode);
        using (StreamReader reader = new StreamReader(errorResponse.GetResponseStream()))
        {
            String text = reader.ReadToEnd();
            Console.WriteLine(text);
        }
    }
}

Output

under construction

Code

<?php

$file_id = "" // Obtained from b2_start_large_file
$account_auth_token = "" // Obtained from b2_authorize_account
$data = array("fileId" => $file_id);
$post_fields = json_encode($data);

// Setup headers
$headers = array();
$headers[] = "Accept: application/json";
$headers[] = "Authorization: " . $account_auth_token;

//  Setup curl to do the post
$session = curl_init($api_url . "/b2api/v1/b2_get_upload_part_url");
curl_setopt($session, CURLOPT_HTTPHEADER, $headers);  // Add headers
curl_setopt($session, CURLOPT_POSTFIELDS, $post_fields); 
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // Receive server response
$server_output = curl_exec($session);
curl_close ($session);
print $server_output;

?>

Output

{
  "authorizationToken": "3_20160409004829_42b8f80ba60fb4323dcaad98_ec81302316fccc2260201cbf17813247f312cf3b_000_uplg",
  "fileId": "4_ze73ede9c9c8412db49f60715_f100b4e93fbae6252_d20150824_m224353_c900_v8881000_t0001",
  "uploadUrl": "https://pod-000-1016-09.backblaze.com/b2api/v1/b2_upload_part/4_ze73ede9c9c8412db49f60715_f100b4e93fbae6252_d20150824_m224353_c900_v8881000_t0001/0037"
}