b2_update_file_retention

Update the File Lock retention settings for an existing file.

Modifies the File Lock retention settings for an existing file. After enabling file retention for a file in a File Lock-enabled bucket, any attempts to delete the file or make any changes to it before the end of the retention period will fail.

File retention settings can be configured in either governance or compliance mode. For files protected in governance mode, file retention settings can be deleted or the retention period shortened only by clients with the appropriate application key capability (i.e., bypassGovernance). File retention settings for files protected in compliance mode cannot removed by any user, but their retention dates can be extended by clients with appropriate application key capabilities.

Request

Request HTTP Headers

Authorization

required

An account authorization token, obtained from b2_authorize_account. The token must have the writeFileRetentions capability.

Request HTTP Message Body Parameters

fileName

required

The name of the file.

fileId

required

The ID of the file, as returned by b2_upload_file, b2_list_file_names, or b2_list_file_versions.

fileRetention

required

Specifies the file retention settings for Backblaze B2 to use for this file. See File Lock for details.

bypassGovernance

optional

Must be specified and set to true if deleting an existing governance mode retention setting or shortening an existing governance mode retention period. See File Lock for details.

Response

Response HTTP Status 200

File Lock retention settings successfully updated. The JSON response will contain:

fileId

The unique identifier for this version of this file.

fileName

The name of this file.

fileRetention

The updated file retention settings.

Response Errors

File Lock retention settings not updated. 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, or the bucket is not File Lock-enabled. The message returned with the error will describe the problem.

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

access_denied

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.

403

cap_exceeded

Usage cap exceeded.

405

method_not_allowed

This operation is not allowed because the specified file is a "hide marker."

Sample Code

Code

curl \
    -H "Authorization: ACCOUNT_AUTHORIZATION_TOKEN" \
    -d '{"fileName": "FILE_NAME", "fileId": "FILE_ID", "fileRetention": { "mode": "governance", retainUntilTimestamp: 1628942493000 }' \
    https://apiNN.backblaze.com/b2api/v2/b2_update_file_retention

Output

{
    "fileId": "4_h4a48fe8875c6214145260818_f000000000000472a_d20210515_m032022_c001_v0000123_t0104",
    "fileName": "typing_test.txt",
    "fileRetention": {
      "mode": "governance",
      "retainUntilTimestamp": 1628942493000
    }  
}

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 fileId = "";  // The fileId of the file you want to update
String fileName = ""; // Name of the file you want to update
String accountAuthorizationToken = ""; // Provided by b2_authorize_account
HttpURLConnection connection = null;
byte postData[] = "{\"fileId\":\"" + fileId + "\",\"fileName\":\"" + fileName + "\"," +
    "\"fileRetention\": { \"mode\": \"governance\", \"retainUntilTimestamp\": 1628942493000 }}"
    .getBytes(StandardCharsets.UTF_8);
try {
    URL url = new URL(apiUrl + "/b2api/v2/b2_update_file_retention");
    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 responsJson = 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

{
    "fileId": "4_h4a48fe8875c6214145260818_f000000000000472a_d20210515_m032022_c001_v0000123_t0104",
    "fileName": "typing_test.txt",
    "fileRetention": {
      "mode": "governance",
      "retainUntilTimestamp": 1628942493000
    }  
}

Code

import json
import urllib2

api_url = "" # Provided by b2_authorize_account
account_authorization_token = "" # Provided by b2_authorize_account
file_name = "" # The name of the file you want to update
file_id = "" # The fileId of the file you want to update

request = urllib2.Request(
    '%s/b2api/v2/b2_update_file_retention' % api_url,
    json.dumps({ 'fileName' : file_name, 'fileId' : file_id, 'fileRetention' : { 'mode' : 'governance', 'retainUntilTimestamp' : 1628942493000 } }),
    headers = { 'Authorization': account_authorization_token }
    )
response = urllib2.urlopen(request)
response_data = json.loads(response.read())
response.close()

Output

{
    "fileId": "4_h4a48fe8875c6214145260818_f000000000000472a_d20210515_m032022_c001_v0000123_t0104",
    "fileName": "typing_test.txt",
    "fileRetention": {
      "mode": "governance",
      "retainUntilTimestamp": 1628942493000
    }  
}

Code

import Foundation

let apiUrl = "" // Provided by b2_authorize_account
let accountAuthorizationToken = "" // Provided by b2_authorize_account
let fileName = "" // The name of the file you want to update
let fileId = "" // The fileId of the file you want to update

// Create the request
var request = URLRequest(url: URL(string: "\(apiUrl)/b2api/v2/b2_update_file_retention")!)
request.httpMethod = "POST"
request.addValue(accountAuthorizationToken, forHTTPHeaderField: "Authorization")
request.httpBody = "{\"fileName\":\"\(fileName)\",\"fileId\":\"\(fileId)\",\"fileRetention\":{\"mode\":\"governance\",\"retainUntilTimestamp\":1628942493000}}".data(using: .utf8)

// Create the task
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
	if let data = data {
		let json = String(data: data, encoding: .utf8)
		print("\(json!)")
	}
}

// Start the task
task.resume()

// Swift 4.1 (swiftlang-902.0.48 clang-902.0.37.1) Xcode 9.3.1 (9E501)

Output

{
    "fileId": "4_h4a48fe8875c6214145260818_f000000000000472a_d20210515_m032022_c001_v0000123_t0104",
    "fileName": "typing_test.txt",
    "fileRetention": {
      "mode": "governance",
      "retainUntilTimestamp": 1628942493000
    }  
}

Code

require 'json'
require 'net/http'

api_url = "" # Provided by b2_authorize_account
account_authorization_token = "" # Provided by b2_authorize_account
file_name = "" # The name of the file you want to update
file_id = "" # The fileId of the file you want to update
uri = URI("#{api_url}/b2api/v2/b2_update_file_retention")
req = Net::HTTP::Post.new(uri)
req.add_field("Authorization","#{account_authorization_token}")
req.body = "{\"fileName\":\"#{file_name}\", \"fileId\":\"#{file_id}\", \"fileRetention\":{\"mode\":\"governance\",\"retainUntilTimestamp\":1628942493000}}"
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

{
    "fileId": "4_h4a48fe8875c6214145260818_f000000000000472a_d20210515_m032022_c001_v0000123_t0104",
    "fileName": "typing_test.txt",
    "fileRetention": {
      "mode": "governance",
      "retainUntilTimestamp": 1628942493000
    }  
}

Code

String apiUrl = "API_URL"; //Provided by b2_authorize_account
String accountAuthorizationToken = "ACCOUNT_AUTHORIZATION_TOKEN"; //Provided by b2_authorize_account
String fileName = "FILE_NAME"; //The name of the file
String fileId = "FILE_ID"; //The unique file ID
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(apiUrl + "/b2api/v2/b2_update_file_retention");
string body =
   "{\"fileName\":\"" + fileName + "\",\n" +
   "\"fileId\":\"" + fileId + "\",\n" +
   "\"fileRetention\": {\n" +
   "  \"mode\": \"governance\",\n" +
   "  \"retainUntilTimestamp\": 1628942493000\n" +
   "}}";
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

{
    "fileId": "4_h4a48fe8875c6214145260818_f000000000000472a_d20210515_m032022_c001_v0000123_t0104",
    "fileName": "typing_test.txt",
    "fileRetention": {
      "mode": "governance",
      "retainUntilTimestamp": 1628942493000
    }  
}

Code

$api_url = ""; // From b2_authorize_account call
$auth_token = ""; // From b2_authorize_account call
$file_id = "";  // The ID of the file you want to update
$file_name = ""; // The file name of the file you want to update

$session = curl_init($api_url .  "/b2api/v2/b2_update_file_retention");

// Add post fields
$data = array("fileId" => $file_id,
              "fileName" => $file_name,
              "fileRetention" => array("mode" => "governance",
                                       "retainUntilTimestamp": 1628942493000));
$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

{
    "fileId": "4_h4a48fe8875c6214145260818_f000000000000472a_d20210515_m032022_c001_v0000123_t0104",
    "fileName": "typing_test.txt",
    "fileRetention": {
      "mode": "governance",
      "retainUntilTimestamp": 1628942493000
    }  
}