0
votes

I'm trying to get objects in my bucket on Google Cloud Storage. To do so I use the recommended code by Google on its API pages :

https://cloud.google.com/storage/docs/downloading-objects#storage-download-object-php

But when I do, I got a permission denied error.

I configured Composer with the permission (for example my code to list the objects within the bucket works fine) but I can't download anything. I get this error :

Warning: fopen(C:\wamp64\www\test): failed to open stream: Permission denied in C:\wamp64\www\test\vendor\google\cloud-storage\src\StorageObject.php on line 602

I gave myself all the permissions to in the Cloud Storage console but the error still appear.

I also wondered, is it possible to get/copy the content of the object without downloading it?

Thank you for your help!

EDIT : The code is pretty simple :

$storage = new StorageClient($config = [ 
    'keyFilePath' => "[PATH OF MY KEYFILE]",
    'projectId' => $projectId
]);
$bucket = $storage->bucket($bucketName);
$object = $bucket->object($objectName);
$object->downloadToFile($destination);
printf('Downloaded gs://%s/%s to %s' . PHP_EOL,
    $bucketName, $objectName, basename($destination));

}

In the storage I just have buckets with a list of object and I need to get the content/dl one of them to merge it with another Json

2
It might be useful to see more of the design / implementation of the logic you are using to access the data. Also, have a look at Stackdriver logging. This is where all the GCP log entries go which might shed some light on the nature of the error.Kolban
I edited my question ! I'll check my GCP log entriesJeremy Bensoussan
There is no error entry in the Stackdriver, the denial of permission isn't represented. Or I don't know where to lookJeremy Bensoussan
Looking a the docs, I am guessing the eror is occuring at the downloadToFile call? I see that takes a local path on your local file system where the file is stored. What is he value of $destination in your story? Do you have write permissions?Kolban
Indeed it's the downloadToFile call that fails ! The destination is a local folder in my local system. I expected this code to create the file and fill it, in fact I just need to read the content of the object to append it to another (already created) file. How should I do to allow the write in file? Should I create the file and expect this code to fill it?Jeremy Bensoussan

2 Answers

0
votes

To make individual objects publicly readable:

https://cloud.google.com/storage/docs/access-control/making-data-public

Verify the next code:

function download_object($bucketName, $objectName, $destination)

{

$storage = new StorageClient();

$bucket = $storage->bucket($bucketName);

$object = $bucket->object($objectName);

$object->downloadToFile($destination);

printf('Downloaded gs://%s/%s to %s' . PHP_EOL,
    $bucketName, $objectName, basename($destination));

}

0
votes

[SOLUTION] I couldn't download the object outside of the folder where the PHP code is. Destination needs to be DIR . '/my-file.json'

Thank you all