- Print
- DarkLight
Configure Cache-Control Policies with the Native API
- Print
- DarkLight
Backblaze B2 Cloud Storage lets you configure cache-control policies when you create or update a bucket.
Set the policy key to Cache-Control
which is case-sensitive as shown in the following java example of b2_create_bucket
.
import java.io.*;
import java.util.*;
import java.net.HttpURLConnection;
import java.net.URL;
String apiUrl = ""; // Provided by b2_authorize_account
String accountId = ""; // Obtained from your B2 account page.
String accountAuthorizationToken = ""; // Provided by b2_authorize_account
HttpURLConnection connection = null;
try {
URL url = new URL(apiUrl + "/b2api/v2/b2_create_bucket");
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(
"{" +
"\"accountId\":\"" + accountId + "\"," +
"\"bucketName\":\"photos\",
\"bucketType\":\"allPrivate\",
\"bucketInfo\": {\"Cache-Control\":\"max-age=600\"}"
+ "}"
.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
} finally {
connection.disconnect();
}
When you download a file, the cache-control response header is set according to the cache-control policy in the bucket Info. If the bucket has no cache-control policy setting, then the default behavior is determined by the creation time of the bucket. All of the buckets that were created on or after September 8, 2021 have no default cache-control header included with file downloads. Buckets that were created before September 8, 2021 have a default cache-control header value of "max-age=0, no-cache, no-store"
. The bucket's cache-control policy setting (or default behavior) can be overridden per file by specifying the X-Bz-Info-b2-cache-control
header at file upload time (see b2_upload_file
).