Sample code for 30+ languages & platforms
Java

Google Cloud Storage: Update Object Metadata

See more Google Cloud Storage Examples

Demonstrates how to update (edit) the metadata associated with an object in a Google Cloud Storage bucket.

Chilkat Java Downloads

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkHttp http = new CkHttp();

    // Implements the following CURL command:

    // curl -X PATCH --data-binary @JSON_FILE_NAME \
    //   -H "Authorization: Bearer OAUTH2_TOKEN" \
    //   -H "Content-Type: application/json" \
    //   "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME"

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    CkBinData bdRequestBody = new CkBinData();
    success = bdRequestBody.LoadFile("JSON_FILE_PATH");
    if (success != true) {
        System.out.println("Failed to load JSON_FILE_PATH");
        return;
        }

    // Adds the "Authorization: Bearer OAUTH2_TOKEN" header.
    http.put_AuthToken("OAUTH2_TOKEN");

    CkHttpResponse resp = new CkHttpResponse();
    success = http.HttpBd("PATCH","https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME",bdRequestBody,"application/json",resp);
    if (success == false) {
        System.out.println(http.lastErrorText());
        return;
        }

    CkStringBuilder sbResponseBody = new CkStringBuilder();
    resp.GetBodySb(sbResponseBody);

    CkJsonObject jResp = new CkJsonObject();
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    System.out.println("Response Body:");
    System.out.println(jResp.emit());

    int respStatusCode = resp.get_StatusCode();
    System.out.println("Response Status Code = " + respStatusCode);
    if (respStatusCode >= 400) {
        System.out.println("Response Header:");
        System.out.println(resp.header());
        System.out.println("Failed.");
        return;
        }
  }
}