b2_cancel_large_file

Cancels the upload of a large file, and deletes all of the parts that have been uploaded.

This will return an error if there is no active upload with the given file ID.

Request

Request HTTP Headers

Authorization

required

An account authorization token, obtained from b2_authorize_account.

Request HTTP Message Body Parameters

fileId

required

The ID returned by b2_start_large_file.

Response

Response HTTP Status 200

Large file cancelled. The JSON response will contain:

fileId

The ID of the file whose upload that was canceled.

accountId

The account that the bucket is in.

bucketId

The unique ID of the bucket.

fileName

The name of the file that was canceled.

Response Errors

Large file not cancelled. If possible the server will return a JSON error structure. Errors include:

status

code

message

400

bad_request

Invalid fileId: <fileId>

400

bad_request

No active upload for: <fileId>

400

bad_request

No active upload for large file (<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

Sample Code

Code

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

Output

{
  "accountId": "YOUR_ACCOUNT_ID",
  "bucketId": "4a48fe8875c6214145260818",
  "fileId": "4_za71f544e781e6891531b001a_f200ec353a2184825_d20160409_m004829_c000_v0001016_t0028",
  "fileName": "bigfile.dat"
}

Code

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

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

// Create Json
JsonObject finishLargeFileJsonObj = Json.createObjectBuilder()
        .add("fileId", fileId)
        .build();

StringWriter sw = new StringWriter();
JsonWriter jw = Json.createWriter(sw);
jw.write(finishLargeFileJsonObj);
jw.close();
postData = sw.toString();

try {
    URL url = new URL(apiUrl + "/b2api/v1/b2_cancel_large_file");
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Authorization", accountAuthorizationToken);
    connection.setRequestProperty("Charset", "UTF-8");
    connection.setRequestProperty("Content-Type", "application/json");
    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 ex) {
    ex.printStackTrace();
}

// 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

{
  "accountId": "YOUR_ACCOUNT_ID",
  "bucketId": "4a48fe8875c6214145260818",
  "fileId": "4_za71f544e781e6891531b001a_f200ec353a2184825_d20160409_m004829_c000_v0001016_t0028",
  "fileName": "bigfile.dat"
}

Code

import json
import urllib2

file_id = "" # Provided by b2_start_large_file
account_authorization_token = "" # Provided by b2_authorize_account
request = urllib2.Request(
    '%s/b2api/v1/b2_cancel_large_file' % 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

{
  "accountId": "YOUR_ACCOUNT_ID",
  "bucketId": "4a48fe8875c6214145260818",
  "fileId": "4_za71f544e781e6891531b001a_f200ec353a2184825_d20160409_m004829_c000_v0001016_t0028",
  "fileName": "bigfile.dat"
}

Code

import Foundation

let fileId = "" // Provided from b2_start_large_file
let apiUrl = "" // Provided from b2_authorize_account
let accountAuthorizationToken = "" // Provided by b2_authorize_account
let jsonBodyData: Dictionary<String,AnyObject> = ["fileId":fileId]
if let url = NSURL(string: "\(apiUrl)/b2api/v1/b2_cancel_large_file") {
    let request = NSMutableURLRequest(URL: url)
    request.HTTPMethod = "POST"
    request.addValue(accountAuthorizationToken, forHTTPHeaderField: "Authorization")
    do {
        request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(jsonBodyData, options: NSJSONWritingOptions.PrettyPrinted)
    } catch let error as NSError {
        print(error.localizedDescription)
    }
    let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
    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

{
  "accountId": "YOUR_ACCOUNT_ID",
  "bucketId": "4a48fe8875c6214145260818",
  "fileId": "4_za71f544e781e6891531b001a_f200ec353a2184825_d20160409_m004829_c000_v0001016_t0028",
  "fileName": "bigfile.dat"
}

Code

require 'json'
require 'net/http'
require 'digest/sha1'

file_id = "" # Provided by b2_start_large_file
account_authorization_token = "" # Provided by b2_authorize_account
api_url = "" # Provided from b2_authorize_account
uri = URI.join("#{api_url}/b2api/v1/b2_cancel_large_file")
puts uri
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
    json = res.body
    puts "start: #{json}"
when Net::HTTPRedirection then
    fetch(res['location'], limit - 1)
else
    res.error!
end

Output

{
  "accountId": "YOUR_ACCOUNT_ID",
  "bucketId": "4a48fe8875c6214145260818",
  "fileId": "4_za71f544e781e6891531b001a_f200ec353a2184825_d20160409_m004829_c000_v0001016_t0028",
  "fileName": "bigfile.dat"
}

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_cancel_large_file");
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 = ""; // Provided by b2_start_large_file
$api_url = ""; // Provided by b2_authorize_account
$account_auth_token = ""; // Provided by b2_authorize_account
$data = array("fileId" => $file_id,); 
$post_fields = json_encode($data);
$session = curl_init($api_url . "/b2api/v1/b2_cancel_large_file");
curl_setopt($session, CURLOPT_POSTFIELDS, $post_fields); 
// Add headers
$headers = array();
$headers[] = "Accept: application/json";
$headers[] = "Authorization: " . $account_auth_token;
curl_setopt($session, CURLOPT_HTTPHEADER, $headers);  // Add headers
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // Receive server response
$server_output = curl_exec($session);
curl_close ($session);
print $server_output;

?>

Output

{
  "accountId": "YOUR_ACCOUNT_ID",
  "bucketId": "4a48fe8875c6214145260818",
  "fileId": "4_za71f544e781e6891531b001a_f200ec353a2184825_d20160409_m004829_c000_v0001016_t0028",
  "fileName": "bigfile.dat"
}