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

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

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

        BinData bdRequestBody = new BinData();
        try {
            bdRequestBody.loadFile("JSON_FILE_PATH");
        } catch (ChilkatException e) {
            System.out.println("Failed to load JSON_FILE_PATH");
            return;
        }

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

        HttpResponse resp = new HttpResponse();
        try {
            http.httpBd("PATCH", "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME", bdRequestBody, "application/json", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        StringBuilder sbResponseBody = new StringBuilder();
        resp.getBodySb(sbResponseBody);

        JsonObject jResp = new JsonObject();
        jResp.loadSb(sbResponseBody);
        jResp.setEmitCompact(false);

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

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