This page describes how to attach and manage contexts on Cloud Storage objects in the form of key-value pairs.
Get the required roles
To get the permissions that you need to create and manage object contexts, ask your administrator to grant you the following IAM roles on object:
- Create objects with contexts: Storage Object Creator 
( roles/storage.objectCreator)
- Attach, update, view, and delete contexts from objects: Storage Object User 
( roles/storage.objectUser)
- View context keys and values attached to objects: Storage Object    Viewer 
( roles/storage.objectViewer)
For more information about granting roles, see Manage access to projects, folders, and organizations .
These predefined roles contain the permissions required to create and manage object contexts. To see the exact permissions that are required, expand the Required permissionssection:
Required permissions
The following permissions are required to create and manage object contexts:
- Create an object with object contexts: -  storage.objects.create
-  storage.objects.createContext
 
-  
- Attach, update, and delete object contexts: -  storage.objects.update
-  storage.objects.createContext
-  storage.objects.updateContext
-  storage.objects.deleteContext
 
-  
- View object contexts: -  storage.objects.get
-  storage.objects.list
 
-  
You might also be able to get these permissions with custom roles or other predefined roles .
Attach contexts to new objects
Attach contexts to objects when you upload new objects to Cloud Storage buckets. Each context consists of a key and a value.
Command line
To attach contexts when you upload objects 
with the  gcloud alpha storage cp 
 
command, use the --custom-contexts 
flag:
gcloud alpha storage cp OBJECT_LOCATION gs:// DESTINATION_BUCKET_NAME --custom-contexts= KEY = VALUE ,...
Where:
-  OBJECT_LOCATIONis the local path to your object. For example,Desktop/dog.png.
-  DESTINATION_BUCKET_NAMEis the name of the bucket to which you are uploading your object. For example,my-bucket.
-  KEYis the context key to attach to an object. For example,Department. You can specify multiple key-value pairs separated by commas.
-  VALUEis the value to associate with the context key. For example,Human resources.
Alternatively, create a JSON file that contains the contexts you want to
attach to the objects, and use the --custom-contexts-file 
flag:
{ " KEY " : { "value" : " VALUE " }, ... }
Where:
-  KEYis the context key to attach to an object. For example,Department. You can specify multiple key-value pairs.
-  VALUEis the value to associate with the context key. For example,Human resources.
To attach contexts when you upload directories 
with the  gcloud alpha storage rsync 
 
command, use the --custom-contexts 
flag or the --custom-contexts-file 
flag:
gcloud alpha storage rsync DIRECTORY_LOCATION gs:// DESTINATION_BUCKET_NAME --recursive --custom-contexts= KEY = VALUE ,...
Where:
-  DIRECTORY_LOCATIONis the local path to your directory. For example,~/my_directory.
-  DESTINATION_BUCKET_NAMEis the name of the bucket to which you are uploading your directory. For example,my-bucket.
-  KEYis the context key to attach to objects. For example,Department. You can specify multiple key-value pairs separated by commas.
-  VALUEis the value to associate with the context key. For example,Human resources.
JSON API
To attach contexts to objects when you upload new objects, use any of the following methods:
As part of the object metadata in JSON format, include the contexts 
field:
{ "contexts" : { "custom" : { " KEY " : { "value" : " VALUE " }, ... } } }
Where:
-  KEYis the context key to attach to an object. For example,Department. You can specify multiple key-value pairs in thecustomobject.
-  VALUEis the value to associate with the context key. For example,Human resources.
Attach or modify contexts to an existing object
You can attach new contexts to your existing objects in the Cloud Storage buckets.
Command line
Use the  gcloud alpha storage objects update 
 
command:
gcloud alpha storage objects update gs:// BUCKET_NAME / OBJECT_NAME CUSTOM_CONTEXTS_FLAG
Where:
-  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 name of the object. For example,pets/dog.png.
-  CUSTOM_CONTEXTS_FLAGis any of the following flags:-  To replace all existing contexts, use --custom-contexts= KEY = VALUE ,...or--custom-contexts-file= CUSTOM_CONTEXTS_FILEWhere: -  KEYis the context key to attach to an object. For example,Department. You can specify multiple key-value pairs separated by commas.
-  VALUEis the value to associate with the context key. For example,Human resources.
-  CUSTOM_CONTEXTS_FILEis the path to the JSON or YAML file that contains the contexts you want to attach to the object.
 
-  
-  To delete all existing contexts, use --clear-custom-contexts.
-  To add, modify, or delete individual contexts, use a combination of --update-custom-contexts= KEY = VALUE ,...and--remove-custom-contexts= KEY ,...Where: -  KEYis the context key that you want to attach to or delete from an object. For example,Department.
-  VALUEis the value to associate with the context key that you want to attach to an object. For example,Human resources.
 
-  
 
-  
If successful, the response looks like the following example:
Patching gs://my-bucket/pets/dog.png#1560574162144861... Completed 1
Client libraries
Java
For more information, see the Cloud Storage Java API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

