b2_get_upload_url

Gets an URL to use for uploading files.

When you upload a file to B2, you must call b2_get_upload_url first to get the URL for uploading. Then, you use b2_upload_file on this URL to upload your file.

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

bucketId

required

The ID of the bucket that you want to upload to.

Response

Response HTTP Status 200

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

bucketId

The unique ID of the bucket.

uploadUrl

The URL that can be used to upload files to this bucket, see b2_upload_file.

authorizationToken

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

Response Errors

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

status

code

message

400

bad_bucket_id

bucket <fileId> does not exist

400

bad_request

snapshot buckets are reserved for Backblaze use

400

bad_request

allow snapshot header must be specified when uploading to a snapshot bucket

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

<various>

Sample Code

Code

curl \
    -H 'Authorization: ACCOUNT_AUTHORIZATION_TOKEN' \
    -d '{"bucketId": "BUCKET_ID"}' \
    https://apiNNN.backblazeb2.com/b2api/v1/b2_get_upload_url

Output

{
    "bucketId" : "4a48fe8875c6214145260818",
    "uploadUrl" : "https://pod-000-1005-03.backblaze.com/b2api/v1/b2_upload_file?cvt=c001_v0001005_t0027&bucket=4a48fe8875c6214145260818",
    "authorizationToken" : "2_20151009170037_f504a0f39a0f4e657337e624_9754dde94359bd7b8f1445c8f4cc1a231a33f714_upld"
}

Code

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

String apiUrl = ""; // Provided by b2_authorize_account
String accountAuthorizationToken = ""; // Provided by b2_authorize_account
String bucketId = ""; // The ID of the bucket you want to upload your file to
HttpURLConnection connection = null;
String postParams = "{\"bucketId\":\"" + bucketId + "\"}";
byte postData[] = postParams.getBytes(StandardCharsets.UTF_8);
try {
    URL url = new URL(apiUrl + "/b2api/v1/b2_get_upload_url");
    connection = (HttpURLConnection)url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Authorization", accountAuthorizationToken);
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("charset", "utf-8");
    connection.setRequestProperty("Content-Length", Integer.toString(postData.length));
    connection.setDoOutput(true);
    DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
    writer.write(postData);
    String jsonResponse = myInputStreamHandler(connection.getInputStream());
    System.out.println(jsonResponse);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    connection.disconnect();
}

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

{
    "bucketId" : "4a48fe8875c6214145260818",
    "uploadUrl" : "https://pod-000-1005-03.backblaze.com/b2api/v1/b2_upload_file?cvt=c001_v0001005_t0027&bucket=4a48fe8875c6214145260818",
    "authorizationToken" : "2_20151009170037_f504a0f39a0f4e657337e624_9754dde94359bd7b8f1445c8f4cc1a231a33f714_upld"
}

Code

import json
import urllib2

api_url = "" # Provided by b2_authorize_account
account_authorization_token = "" # Provided by b2_authorize_account
bucket_id = "" # The ID of the bucket you want to upload your file to
request = urllib2.Request(
	'%s/b2api/v1/b2_get_upload_url' % api_url,
	json.dumps({ 'bucketId' : bucket_id }),
	headers = { 'Authorization': account_authorization_token }
	)
response = urllib2.urlopen(request)
response_data = json.loads(response.read())
response.close()

Output

{
    "bucketId" : "4a48fe8875c6214145260818",
    "uploadUrl" : "https://pod-000-1005-03.backblaze.com/b2api/v1/b2_upload_file?cvt=c001_v0001005_t0027&bucket=4a48fe8875c6214145260818",
    "authorizationToken" : "2_20151009170037_f504a0f39a0f4e657337e624_9754dde94359bd7b8f1445c8f4cc1a231a33f714_upld"
}

Code

import Foundation

let apiUrl = "" // Provided by b2_authorize_account
let accountAuthorizationToken = "" // Provided by b2_authorize_account
let bucketId = "" // The ID of the bucket you want to upload your file to
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL:NSURL(string: "\(apiUrl)")!.URLByAppendingPathComponent("/b2api/v1/b2_get_upload_url"))
request.HTTPMethod = "POST"
request.addValue(accountAuthorizationToken, forHTTPHeaderField: "Authorization")
request.HTTPBody = "{\"bucketId\":\"\(bucketId)\"}".dataUsingEncoding(NSUTF8StringEncoding)
let task = session.dataTaskWithRequest(request, fromData: uploadData, completionHandler:{ (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
    if data != nil {
        let json = NSString(data: data, encoding:NSUTF8StringEncoding)
    }
})
task.resume()

Output

{
    "bucketId" : "4a48fe8875c6214145260818",
    "uploadUrl" : "https://pod-000-1005-03.backblaze.com/b2api/v1/b2_upload_file?cvt=c001_v0001005_t0027&bucket=4a48fe8875c6214145260818",
    "authorizationToken" : "2_20151009170037_f504a0f39a0f4e657337e624_9754dde94359bd7b8f1445c8f4cc1a231a33f714_upld"
}

Code

require 'json'
require 'net/http'

api_url = "" # Provided by b2_authorize_account
account_authorization_token = "" # Provided by b2_authorize_account
bucket_id = "" # The ID of the bucket you want to upload your file to
uri = URI("#{api_url}/b2api/v1/b2_get_upload_url")
req = Net::HTTP::Post.new(uri)
req.add_field("Authorization","#{account_authorization_token}")
req.body = "{\"bucketId\":\"#{bucket_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

{
    "bucketId" : "4a48fe8875c6214145260818",
    "uploadUrl" : "https://pod-000-1005-03.backblaze.com/b2api/v1/b2_upload_file?cvt=c001_v0001005_t0027&bucket=4a48fe8875c6214145260818",
    "authorizationToken" : "2_20151009170037_f504a0f39a0f4e657337e624_9754dde94359bd7b8f1445c8f4cc1a231a33f714_upld"
}

Code

String apiUrl = "API_URL"; //Provided by b2_authorize_account 
String accountAuthorizationToken = "ACCOUNT_AUTHORIZATION_TOKEN" //Provided by b2_authorize_account
String bucketId = "BUCKET_ID"; //The unique bucket ID
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(apiUrl + "/b2api/v1/b2_get_upload_url");
string body = "{\"bucketId\":\"" + bucketId + "\"}";
var data = Encoding.UTF8.GetBytes(body);
webRequest.Method = "POST";
webRequest.Headers.Add("Authorization", accountAuthorizationToken);
webRequest.ContentType = "application/json; charset=utf-8";
webRequest.ContentLength = data.Length;
using (var stream = webRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
stream.Close();
}
WebResponse response = (HttpWebResponse)webRequest.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
response.Close();
Console.WriteLine(responseString);

Output

{
    "bucketId" : "4a48fe8875c6214145260818",
    "uploadUrl" : "https:/pod-000-1005-03.backblaze.com/b2api/v1/b2_upload_file?cvt=c001_v0001005_t0027&bucket=4a48fe8875c6214145260818",
    "authorizationToken" : "2_20151009170037_f504a0f39a0f4e657337e624_9754dde94359bd7b8f1445c8f4cc1a231a33f714_upld"
}

Code

$api_url = ""; // From b2_authorize_account call
$auth_token = ""; // From b2_authorize_account call
$bucket_id = "";  // The ID of the bucket you want to upload to

$session = curl_init($api_url .  "/b2api/v1/b2_get_upload_url");

// Add post fields
$data = array("bucketId" => $bucket_id);
$post_fields = json_encode($data);
curl_setopt($session, CURLOPT_POSTFIELDS, $post_fields); 

// Add headers
$headers = array();
$headers[] = "Authorization: " . $auth_token;
curl_setopt($session, CURLOPT_HTTPHEADER, $headers); 

curl_setopt($session, CURLOPT_POST, true); // HTTP POST
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);  // Receive server response
$server_output = curl_exec($session); // Let's do this!
curl_close ($session); // Clean up
echo ($server_output); // Tell me about the rabbits, George!

Output

{
    "bucketId" : "4a48fe8875c6214145260818",
    "uploadUrl" : "https://pod-000-1005-03.backblaze.com/b2api/v1/b2_upload_file?cvt=c001_v0001005_t0027&bucket=4a48fe8875c6214145260818",
    "authorizationToken" : "2_20151009170037_f504a0f39a0f4e657337e624_9754dde94359bd7b8f1445c8f4cc1a231a33f714_upld"
}