These predefined roles contain
the permissions required to create and manage object contexts. To see the exact permissions that are
required, expand theRequired permissionssection:
Required permissions
The following permissions are required to create and manage object contexts:
importcom.google.cloud.storage.Blob;importcom.google.cloud.storage.BlobId;importcom.google.cloud.storage.BlobInfo;importcom.google.cloud.storage.BlobInfo.ObjectContexts;importcom.google.cloud.storage.BlobInfo.ObjectCustomContextPayload;importcom.google.cloud.storage.Storage;importcom.google.cloud.storage.StorageOptions;importcom.google.common.collect.Maps;importjava.util.Map;publicclassSetObjectContexts{publicstaticvoidsetObjectContexts(StringprojectId,StringbucketName,StringobjectName,Stringkey,Stringvalue)throwsException{// The ID of your GCP project// String projectId = "your-project-id";// The ID of your GCS bucket// String bucketName = "your-unique-bucket-name";// The ID of your GCS object// String objectName = "your-object-name";// The context key-value you want to add// String key = "your-context-key";// String value = "your-context-value";try(Storagestorage=StorageOptions.newBuilder().setProjectId(projectId).build().getService()){BlobIdblobId=BlobId.of(bucketName,objectName);Blobblob=storage.get(blobId);if(blob==null){System.out.println("The object "+objectName+" was not found in "+bucketName);return;}// Recommended: Set a generation-match precondition to avoid potential race// conditions and data corruptions. The request to update returns a 412 error if// the object's generation number does not match your precondition.Storage.BlobTargetOptionprecondition=Storage.BlobTargetOption.generationMatch();// This section demonstrates how to upsert, delete all, and delete a specific context.// To upsert a context (if the key already exists, its value is replaced;// otherwise, a new key-value pair is added):ObjectCustomContextPayloadpayload=ObjectCustomContextPayload.newBuilder().setValue(value).build();Map<String,ObjectCustomContextPayload>custom=Maps.newHashMap();custom.put(key,payload);ObjectContextscontexts=ObjectContexts.newBuilder().setCustom(custom).build();/** To delete all existing contexts:* ObjectContexts contexts = ObjectContexts.newBuilder().setCustom(null).build();*//** To delete a specific key from the context:* Map<String, ObjectCustomContextPayload> custom = Maps.newHashMap();* custom.put(key, null);* ObjectContexts contexts = ObjectContexts.newBuilder().setCustom(custom).build();*/BlobInfopendingUpdate=blob.toBuilder().setContexts(contexts).build();storage.update(pendingUpdate,precondition);System.out.println("Updated custom contexts for object "+objectName+" in bucket "+bucketName);}}}
JSON_FILE_NAMEis the path to the file
that includes the object contexts information.
BUCKET_NAMEis the name of the bucket
that contains the object for which you want to edit context. For
example,my-bucket.
OBJECT_NAMEis the URL-encoded name of the
object. For example,employees.txt.
Alternatively, you can replace an object's context with aPUTObjectrequest. ThePUTobject request also replaces other
object metadata. Therefore, we don't recommend using thePUTobject request.
View object contexts
You can view an object's contexts by listing object metadata or describing a specific object.
importcom.google.cloud.storage.Blob;importcom.google.cloud.storage.BlobInfo.ObjectContexts;importcom.google.cloud.storage.BlobInfo.ObjectCustomContextPayload;importcom.google.cloud.storage.Storage;importcom.google.cloud.storage.StorageOptions;importjava.util.Map;publicclassGetObjectContexts{publicstaticvoidgetObjectContexts(StringprojectId,StringbucketName,StringobjectName)throwsException{// The ID of your GCP project// String projectId = "your-project-id";// The ID of your GCS bucket// String bucketName = "your-unique-bucket-name";// The ID of your GCS object// String objectName = "your-object-name";try(Storagestorage=StorageOptions.newBuilder().setProjectId(projectId).build().getService()){Blobblob=storage.get(bucketName,objectName);if(blob==null){System.out.println("The object "+objectName+" was not found in "+bucketName);return;}ObjectContextsobjectContexts=blob.getContexts();if(objectContexts!=null){Map<String,ObjectCustomContextPayload>customContexts=objectContexts.getCustom();if(customContexts==null){System.out.println("No custom contexts found for object: "+objectName);return;}// Print blob's object contextsSystem.out.println("\nCustom Contexts:");for(Map.Entry<String,ObjectCustomContextPayload>custom:customContexts.entrySet()){System.out.println(custom.getKey()+"="+custom.getValue());}}}}}
Filter objects by the existence of object context keys or their
specific values. Filtering objects by contexts helps to locate and manage
particular groups of objects efficiently. For details, seeFilter objects by contexts.
Manage object contexts during object operations
By default, Cloud Storage preserves object contexts when you copy, rewrite, compose, move, or restore objects.
Copy objects
By default, Cloud Storage preserves object contexts from the source
object during a copy operation, even if you override other metadata.
To modify object contexts during a copy operation, complete the following steps:
The--custom-contextsflag to set new contexts for the destination object.
The--clear-custom-contextsflag to prevent contexts from the source
object from being attached to the destination object.
A combination of the--update-custom-contextsand--remove-custom-contextsflags to modify individual contexts from the
source object before attaching them to the destination object.
To set new contexts when you copy an object, use thegcloud storage cpcommand:
SOURCE_BUCKET_NAMEis the name of the bucket containing the object to copy. For example,my-source-bucket.
SOURCE_OBJECT_NAMEis the name of the object to copy. For example,pets/dog.png.
DESTINATION_BUCKET_NAMEis the name of the bucket to copy the object to. For example,my-destination-bucket.
DESTINATION_OBJECT_NAMEis the name of the destination object. For example,pets/cat.png.
To modify individual contexts from the source object when you copy an object,
use thegcloud storage cpcommand with--update-custom-contextsand--remove-custom-contexts:
For information about how contexts behave during copy operations, see thedropContextGroupsquery parameter.
Rewrite objects
By default, Cloud Storage preserves object contexts from the source
object during a rewrite operation, even if you override other metadata.
To modify object contexts during a rewrite operation, complete the following steps:
Command line
Thegcloud storage cpcommand,gcloud storage rsynccommand,
andgcloud storage mvcommands perform rewrites automatically when
necessary, for example, when copying objects between different locations or
storage classes.gcloud storage cpandgcloud storage rsyncresult in a
source and destination object, whilegcloud storage mvcreates the destination
object and removes the source object. Because these operations create a new
object, you can also modify or attach contexts as part of the same command by
usinganyof the following flags:
The--custom-contextsflag to set new contexts for the destination object.
The--clear-custom-contextsflag to prevent contexts from the source
object from being attached to the destination object.
A combination of the--update-custom-contextsand--remove-custom-contextsflags to modify individual contexts from the
source object before attaching them to the destination object.
To set new contexts when you copy an object, use thegcloud storage cpcommand:
SOURCE_BUCKET_NAMEis the name of the bucket containing the object to copy. For example,my-source-bucket.
SOURCE_OBJECT_NAMEis the name of the object to copy. For example,pets/dog.png.
DESTINATION_BUCKET_NAMEis the name of the bucket to copy the object to. For example,my-destination-bucket.
DESTINATION_OBJECT_NAMEis the name of the destination object. For example,pets/cat.png.
To modify individual contexts from the source object when you copy an object,
use thegcloud storage cpcommand with--update-custom-contextsand--remove-custom-contexts:
For information about how contexts behave during rewrite operations, see thedropContextGroupsquery parameter.
Compose objects
Thegcloud storage objects composecommand and theJSON API
composemethod merge contexts from the source objects and attach them to
the destination objects by default. Cloud Storage resolves conflicts by
prioritizing contexts from source objects processed later. For more information
about the object context behavior during a compose operation, seeComposite object contexts.
Command line
To specify new contexts for the destination object when composing objects, use the--contextsflag:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-04-06 UTC."],[],[]]