Google-cloud-php: Custom metadata in google cloud storage

Created on 10 Aug 2017  路  9Comments  路  Source: googleapis/google-cloud-php

Is there any way to add custom metadata when uploading. I can't find how to add metadata using php. Here is my upload codes

$storage = new StorageClient( [
   'keyFilePath' => ZB_AREA51. '/cloud_key.json',
   'projectId' => '424325245454',
] );
$file = fopen($source, 'r');
$bucket = $storage->bucket($this->bucket_name);
$bucket->
$result = $bucket->upload($file, [
   'name' => 'four.jpg',
   'metadata' => array(
      'custom_key1' => 'Custom Value One',
      'custom_key2' => 'Custom Value Two',
   ),
]);
printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $this->bucket_name, 'four.jpg');
storage question

Most helpful comment

Hey @hlaporthein,

Custom metadata can be provided through the metadata key like so:

$result = $bucket->upload($file, [
    'name' => 'four.jpg',
    'metadata' => [
        'metadata' => [
            'custom_key1' => 'Custom Value One',
            'custom_key2' => 'Custom Value Two'
        ]
    ]
]);

Best,
Dave

All 9 comments

Hey @hlaporthein,

Custom metadata can be provided through the metadata key like so:

$result = $bucket->upload($file, [
    'name' => 'four.jpg',
    'metadata' => [
        'metadata' => [
            'custom_key1' => 'Custom Value One',
            'custom_key2' => 'Custom Value Two'
        ]
    ]
]);

Best,
Dave

@hlaporthein does https://github.com/GoogleCloudPlatform/google-cloud-php/pull/627 help make the documentation clearer for you? :)

Woo! Thanks @dwsupplee , It worked both upload & getResumableUploader functions. Thanks.

This is not working.

@floatbeta can you share a code snippet?

I cannot seem to be able to update metadata via the Admin SDK (PHP) on objects ( uploaded by various platform users ).

{
    "domain": "global",
    "reason": "forbidden",
    "message": "<redacted>@frisk-js.iam.gserviceaccount.com does not have storage.objects.update access to <redacted>.appspot.com/XpFekbUZDdYjtajfj1ndyiRL6Iv2/conversations/XYkMdQYUhpyEx67O8RRp_out.jpg."
}

This is the service account that was generated from the Firebase admin dashboard. Do I need to do any additional changes or anything ?

@EduardJS you need to grant the proper IAM roles to the service account. Refer to Cloud IAM Roles for Cloud Storage for a reference to available roles, and Granting, changing and revoking access to resources for a guide to granting the proper permissions to your service account.

$result = $bucket->upload($file, [
    'name' => 'four.jpg',
    'metadata' => [
        'metadata' => [
            'custom_key1' => 'Custom Value One',
            'custom_key2' => 'Custom Value Two'
        ]
    ]
]);

This works to set custom_key1 and custom_key2. But why the double metadata keys? It seems redundant. Can anything go in the outermost metadata key besides metadata?

The reason I ask is that, although it works, it seems unstable, that Google might change it once they realize that it seems redundant, metadata => metadata => custom_key1, instead of just metadata => custom_key1.

I hope my question makes sense. If you can just explain the reasoning behind this array structure, I will be satisfied.

Well, I would also advise you to add an example to the documentation, to really drive it home. It is not clear because it seems like a mistake.

@abanks-asc the outermost metadata key corresponds to the request body outlined in the documentation. The PHP docblock mentions this.

It is a fairly common convention in Google Cloud PHP, but I agree it may seem odd here, as one of the available options in this case is an array called metadata.

The code in question is stable and is covered by semantic versioning guarantees that we won't make any breaking changes to it within a minor version. You can use it without concern.

Was this page helpful?
0 / 5 - 0 ratings