Skip to content
Advertisement

Listing buckets in cloud storage project using PHP API

I’m trying to list out all (some, even) of the buckets in my storage project. If I know the name of a bucket, the “bucket” function will get the bucket. But I can’t use “buckets” to list the buckets in my project:

$client = new StorageClient(
                             [
                              'projectId' => <my project id>,
                              'keyFile' => json_decode(file_get_contents(<my json file>))
                             ]
                           );

$bucket_name = 'idx-mls-info-gs-ihouseprd.com';
$one_bucket = $client->bucket( $bucket_name );
print "GOT BUCKET: " . $one_bucket->name() . "n";
// NOTE: this works


$prefix = 'idx-';
print "Getting buckets (prefix: $prefix)n";
            $buckets = $client->buckets( ['prefix' => $prefix] );
            foreach ( $buckets as $bucket )
            {
                printf('Bucket: %s' . PHP_EOL, $bucket->name());
            }
print "done with buckets"
// THIS DOES NOTHING

My service account has the “Storage Admin” role. I am perplexed. NOTE: I am using PHP 5.6, in case that matters. Composer didn’t have a problem installing the GCS library, so I assumed that was ok.

Ok, so I must be missing something. Using my test case of getting a single bucket, I have then used $one_bucket->object(), and successfully gotten an object. But if I try $one_bucket->objects(), I again get nothing. So the multiple case for entities in the GCS is not working for me, whether buckets or objects. Perhaps that’s a clue.

Further information: There is an exception when hitting the iterator (foreach $buckets as $bucket):

exception ‘Exception’ with message ‘exception ‘GoogleCloudCoreExceptionServiceException’ with message ‘4096:Argument 2 passed to GoogleAuthCredentialsLoader::makeCredentials() must be of the type array, object given, called in /home/httpd/idxv3/vendor/google/cloud-core/src/RequestWrapperTrait.php on line 158 and defined in /home/httpd/idxv3/vendor/google/auth/src/CredentialsLoader.php on line 135’ in /home/httpd/idxv3/vendor/google/cloud-core/src/RequestWrapper.php:362

Not sure why the authentication is causing problems.

Advertisement

Answer

Ok, I found out what the problem was/is. It is in the creation of the storage client. My call the json_decode was missing a parameter. As in my original code, what gets passed into the constructor is a stdClass Object, which is not liked down in the depths of the code. Adding “, true” to the call to json_decode, what gets passed in is then an array, which is desired:

$client = new StorageClient(
                         [
                          'projectId' => <my project id>,
                          'keyFile' => json_decode(file_get_contents(<my json file>), true)
                         ]
                       );

This fixes the problems I was having. Not sure why I didn’t get errors earlier on, like in the constructor.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement