user54097
user54097

Reputation: 45

How to set custom metadata for Google Cloud Storage using JSON?

I am trying to add custom metadata to all uploads from my site to GCS.

I found something but I can't get it to work, in the Service Account JSON I added:

  "metadata":  {
   "customMeta": "text here"

}

But it is not working, am I missing something here? Thannks.

UPDATE

I am using Wordpress and a plugin called WP-Stateless, I asked the plugin author and he directed me to [link] (https://github.com/wpCloud/wp-stateless/blob/v2.2/lib/classes/class-utility.php) I tried adding a couple of lines, but after I saved it and tried uploading something, I checked the GCS console and there is no new metadata.

/* Add Google Storage metadata to our attachment */
          $fileLink = $bucketLink . '/' . ( !empty($media['name']) ? $media['name'] : $file );
          $cloud_meta = array(
            'id' => $media[ 'id' ],
            'name' => !empty($media['name']) ? $media['name'] : $file,
            'fileLink' => $fileLink,
            'storageClass' => $media[ 'storageClass' ],
            'mediaLink' => $media[ 'mediaLink' ],
            'selfLink' => $media[ 'selfLink' ],
            'bucket' => ud_get_stateless_media()->get( 'sm.bucket' ),
            'object' => $media,
            'sizes' => array(),
            'newDATA' => $_newData[ 'some text' ],
          );

But I uploaded something and check GCS but there was now newDATA some text in the metadata for the file I uploaded.

Upvotes: 3

Views: 6823

Answers (2)

Binita Mehta
Binita Mehta

Reputation: 31

Here is an example that worked for me (notice the metadata within metadata)

const file = bucket.file(filename);     
await file.save(JSON.stringify(data), { metadata: { metadata: { user: 'user1' }}});

Upvotes: 3

mvmn
mvmn

Reputation: 4057

As the documentation says, request body can have metadata field with JSON object having key/value pairs for metadata. Or metadata.key fields with specific values:

metadata - object - User-provided metadata, in key/value pairs.

metadata.(key) - string - An individual metadata entry. writable

Upvotes: 1

Related Questions