how to prepend value in content-disposition metadata for all files of GCP bucket wth gs util

51 Views Asked by At

I am having files in GCP bucket already uploaded and want to prepend "attachment" value for all files content-disposition property using gs utils.

Below command we have for updating property

gcloud storage objects update gs://bucket_name/*.* --content-disposition="Attachment" 

But I want to prepend "Attachment" value in existing property value.

I can loop to read all file's metadata using below script:

for FILEPATH in $(gsutil ls gs://dellord)
do  
echo $FILEPATH 
  for METAPROP in $(gsutil ls -L $FILEPATH)
do
  echo $METAPROP 
done

But how to read particular content-disposition property and prepend in individual file property.

1

There are 1 best solutions below

0
mhouglum On

You might want to try printing the output as JSON, using jq to filter it and print the name and new content-disposition of each object, and then having another program read in those JSON objects and update the metadata for each one. This command:

gcloud --format="json(name,content_disposition)" storage objects list gs://BUCKETNAME/** \
  | jq '.[] | if .content_disposition == null then .content_disposition = "attachment" else .content_disposition = "attachment; " + .content_disposition end' \
  > objects.json

Creates an objects.json file that might look something like this, with the updated content-disposition value and each object's name:

{
  "content_disposition": "attachment; filename=\"object1\"",
  "name": "object1"
}
{
  "content_disposition": "attachment"
  "name": "object2",
}

You could then feed that objects.json file to a script that uses a loop to run a gcloud storage objects update command to update the content disposition of each object to the new specified value.