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:

JavaScript

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:

JavaScript

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