【问题标题】:Listing buckets in cloud storage project using PHP API使用 PHP API 列出云存储项目中的存储桶
【发布时间】:2020-12-14 12:39:25
【问题描述】:

我正在尝试列出我的存储项目中的所有(甚至一些)存储桶。如果我知道存储桶的名称,“存储桶”函数将获取存储桶。但是我不能使用“buckets”来列出我项目中的bucket:

$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

我的服务帐户具有“存储管理员”角色。我很困惑。 注意:我使用的是 PHP 5.6,以防万一。 Composer 安装 GCS 库没有问题,所以我认为没问题。

好的,所以我一定遗漏了一些东西。使用我的获取单个存储桶的测试用例,然后我使用了 $one_bucket->object(),并成功获取了一个对象。但是如果我尝试 $one_bucket->objects(),我又什么也得不到。因此,无论是存储桶还是对象,GCS 中实体的多种情况都对我不起作用。也许这是一个线索。

更多信息: 命中迭代器时出现异常(foreach $buckets as $bucket):

异常 'Exception' 和消息 'exception 'Google\Cloud\Core\Exception\ServiceException' 和消息 '4096:Argument 2 传递给 Google\Auth\CredentialsLoader::makeCredentials() 必须是数组类型,对象给定,在第 158 行的 /home/httpd/idxv3/vendor/google/cloud-core/src/RequestWrapperTrait.php 中调用,并在第 135 行的 /home/httpd/idxv3/vendor/google/auth/src/CredentialsLoader.php 中定义' 在 /home/httpd/idxv3/vendor/google/cloud-core/src/RequestWrapper.php:362

不确定身份验证为何会导致问题。

【问题讨论】:

    标签: php api google-cloud-storage


    【解决方案1】:

    我为您的第一个问题找到了link,希望这对您有所帮助。它描述了如何列出项目中的存储桶。

    【讨论】:

    • 谢谢,但我已经看过了,但没有帮助。如果您查看那里的 PHP 代码示例,它在功能上与我所拥有的相同(并且完全忽略构造函数参数,专门用于身份验证)。令人沮丧的部分是 $client->bucket() 像宣传的那样工作,但 $client->buckets() 似乎没有。
    【解决方案2】:

    好的,我发现了问题所在。它在创建存储客户端。我对 json_decode 的调用缺少一个参数。在我的原始代码中,传递给构造函数的是一个 stdClass 对象,在代码的深处并不喜欢它。在对json_decode的调用中添加“,true”,传入的是一个数组,这是需要的:

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

    这解决了我遇到的问题。不知道为什么我之前没有收到错误,比如在构造函数中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-14
      • 2021-01-19
      • 2019-06-17
      • 2022-01-23
      • 2022-11-30
      • 1970-01-01
      • 2015-02-14
      • 2020-02-27
      相关资源
      最近更新 更多