b2_update_bucket

Update an existing bucket.

Modifies the bucketType of an existing bucket. Can be used to allow everyone to download the contents of the bucket without providing any authorization, or to prevent anyone from downloading the contents of the bucket without providing a bucket auth token.

Request

Request HTTP Headers

Authorization

required

An account authorization token, obtained from b2_authorize_account.

Request HTTP Message Body Parameters

accountId

required

The account that the bucket is in.

bucketId

required

The unique ID of the bucket.

bucketType

optional

One of: "allPublic", "allPrivate". "allPublic" means that anybody can download the files is the bucket; "allPrivate" means that you need an authorization token to download them.
If not specified, setting will remain unchanged.

bucketInfo

optional

User-defined information to be stored with the bucket: a JSON object mapping names to values. See Buckets.
If specified, the existing bucket info will be replaced with the new info. If not specified, setting will remain unchanged.
Cache-Control policies can be set here on a global level for all the files in the bucket.

corsRules

optional

The list of CORS rules for this bucket. See CORS Rules for an overview and the rule structure.
If specified, the existing CORS rules will be replaced with this new list. If not specified, setting will remain unchanged.

lifecycleRules

optional

The list of lifecycle rules for this bucket. Structure defined below. See Lifecycle Rules.
If specified, the existing lifecycle rules will be replaced with this new list. If not specified, setting will remain unchanged.

ifRevisionIs

optional

When set, the update will only happen if the revision number stored in the B2 service matches the one passed in. This can be used to avoid having simultaneous updates make conflicting changes.

Lifecycle Rule Structure

daysFromUploadingToHiding

optional

After a file is uploaded, the number of days before it can be hidden.

daysFromHidingToDeleting

optional

After a file is hidden, the number of days before it can be deleted.

fileNamePrefix

optional

The rule applies to files whose names start with this prefix.

Response

Response HTTP Status 200

Bucket successfully updated. The JSON response will contain:

accountId

The account that the bucket is in.

bucketId

The unique ID of the bucket.

bucketName

The unique name of the bucket

bucketType

One of: "allPublic", "allPrivate", "snapshot", or other values added in the future. "allPublic" means that anybody can download the files is the bucket; "allPrivate" means that you need an authorization token to download them; "snapshot" means that it's a private bucket containing snapshots created on the B2 web site.

bucketInfo

The user data stored with this bucket.

lifecycleRules

The list of lifecycle rules for this bucket.

revision

A counter that is updated every time the bucket is modified, and can be used with the ifRevisionIs parameter to b2_update_bucket to prevent colliding, simultaneous updates.

Response Errors

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

status

code

message

400

bad_request

AccountId invalid

400

bad_request

Account <accountId> does not exist

400

bad_request

invalid_bucket_id

400

bad_request

BucketId not valid for account

400

bad_request

Cannot change a bucket to a snapshot bucket

400

bad_request

bucket_not_updated

401

unauthorized

Not authorized

409

conflict

The ifRevisionIs test failed.

Sample Code

Code

curl \
    -H 'Authorization: ACCOUNT_AUTHORIZATION_TOKEN' \
    -d '{"accountId": "ACCOUNT_ID", "bucketId": "BUCKET_ID", "bucketType": "allPublic"}' \
    https://apiNNN.backblazeb2.com/b2api/v1/b2_update_bucket

Output

{
	"accountId" : "010203040506",
	"bucketId" : "4a48fe8875c6214145260818",
	"bucketInfo" : {},
	"bucketName" : "any-name-you-pick",
	"bucketType" : "allPrivate",
	"lifecycleRules" : []
}

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 accountId = ""; // The 12 digit ID of the account. 
String bucketId = ""; // The ID of the bucket you want to update
String bucketType = ""; // The type of bucket: "allPublic" or "allPrivate"
String postParams = "{\"accountId\":\"" + accountId + "\",\"bucketId\":\"" + bucketId + "\",\"bucketType\":\"" + bucketType + "\"}";
byte postData[] = postParams.getBytes(StandardCharsets.UTF_8);
try {
    URL url = new URL(apiUrl + "/b2api/v1/b2_update_bucket");
    HttpURLConnection 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 = myInputStreamReader(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

{
	"accountId" : "010203040506",
	"bucketId" : "4a48fe8875c6214145260818",
	"bucketInfo" : {},
	"bucketName" : "any-name-you-pick",
	"bucketType" : "allPrivate",
	"lifecycleRules" : []
}

Code

import json
import urllib2

api_url = "" # Provided by b2_authorize_account
account_authorization_token = ""  # Provided by b2_authorize_account
account_id = "" # The 12 digit ID of the account. 
bucket_id = "" # The ID of the bucket you want to update
bucket_type = "" # The type of bucket: "allPublic" or "allPrivate"
request = urllib2.Request(
	'%s/b2api/v1/b2_update_bucket' % api_url,
	json.dumps({ 'accountId' : account_id, 'bucketId' : bucket_id, 'bucketType' : bucket_type }),
	headers = { 'Authorization': account_authorization_token }
	)
response = urllib2.urlopen(request)
response_data = json.loads(response.read())
response.close()

Output

{
    "accountId" : "010203040506",
    "bucketId" : "4a48fe8875c6214145260818",
    "bucketInfo" : {},
    "bucketName" : "any-name-you-pick",
    "bucketType" : "allPrivate",
    "lifecycleRules" : []
}

Code

import Foundation

let apiUrl = "" // Provided by b2_authorize_account
let accountAuthorizationToken = "" // Provided from b2_authorize_account
let accountId = "" // The 12 digit ID of the account. 
let bucketId = "" // The ID of the bucket you want to update
let bucketType = "" // The type of bucket: "allPublic" or "allPrivate"
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL:NSURL(string: "\(apiUrl)")!.URLByAppendingPathComponent("/b2api/v1/b2_update_bucket"))
request.HTTPMethod = "POST"
request.addValue(accountAuthorizationToken, forHTTPHeaderField: "Authorization")
request.HTTPBody = "{\"accountId\":\"\(accountId)\", \"bucketId\":\"\(bucketId)\", \"bucketType\":\"\(bucketType)\"}".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let task = session.dataTaskWithRequest(request, completionHandler:{ (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
    if data != nil {
        let json = NSString(data: data, encoding:NSUTF8StringEncoding)
    }
})
task.resume()

Output

{
	"accountId" : "010203040506",
	"bucketId" : "4a48fe8875c6214145260818",
	"bucketInfo" : {},
	"bucketName" : "any-name-you-pick",
	"bucketType" : "allPrivate",
	"lifecycleRules" : []
}

Code

require 'json'
require 'net/http'

api_url = "" # Provided by b2_authorize_account
account_authorization_token = ""  # Provided by b2_authorize_account
account_id = "" # The 12 digit ID of the account. 
bucket_id = "" # The ID of the bucket you want to update
bucket_type = "" # The type of bucket: "allPublic" or "allPrivate"
uri = URI("#{api_url}/b2api/v1/b2_update_bucket")
req = Net::HTTP::Post.new(uri)
req.add_field("Authorization","#{account_authorization_token}")
req.body = "{\"accountId\":\"#{account_id}\",\"bucketId\":\"#{bucket_id}\",\"bucketType\":\"#{bucket_type}\"}"
http = Net::HTTP.new(req.uri.host, req.uri.port)
http.use_ssl = (req.uri.scheme == 'https')
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

{
	"accountId" : "010203040506",
	"bucketId" : "4a48fe8875c6214145260818",
	"bucketInfo" : {},
	"bucketName" : "any-name-you-pick",
	"bucketType" : "allPrivate",
	"lifecycleRules" : []
}

Code

String apiUrl = "API_URL"; //Provided by b2_authorize_account 
String accountAuthorizationToken = "ACCOUNT_AUTHORIZATION_TOKEN"; //Provided by b2_authorize_account
String accountId = "ACCOUNT_ID"; //B2 Cloud Storage AccountID 
String bucketId = "BUCKET_ID; //The unique bucket ID
String bucketType = "BUCKET_TYPE"; //"allPrivate" - or - "allPublic" 
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(apiUrl + "/b2api/v1/b2_update_bucket");
string body = 
"{\"accountId\":\"" + accountId + "\",\n" +
"\"bucketId\":\"" + bucketId + "\",\n" +
"\"bucketType\":\"" + bucketType + "\"}";
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

{
	"accountId" : "010203040506",
	"bucketId" : "4a48fe8875c6214145260818",
	"bucketInfo" : {},
	"bucketName" : "any-name-you-pick",
	"bucketType" : "allPrivate",
	"lifecycleRules" : []
}

Code

$account_id = ""; // Obtained from your B2 account page
$api_url = ""; // From b2_authorize_account call
$auth_token = ""; // From b2_authorize_account call
$account_id = ""; // The 12 digit ID of the account. 
$bucket_id = "";  // The ID of the bucket you want to update
$bucket_type = "allPublic"; // Either allPublic or allPrivate

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

// Add post fields
$data = array("accountId" => $account_id, "bucketId" => $bucket_id, "bucketType" => $bucket_type);
$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

{
    "accountId" : "010203040506",
    "bucketId" : "4a48fe8875c6214145260818",
    "bucketInfo" : {},
    "bucketName" : "any-name-you-pick",
    "bucketType" : "allPrivate",
    "lifecycleRules" : []
}