首页 > 解决方案 > php:对谷歌云存储删除操作使用批处理

问题描述

我正在尝试设置批量删除 GoogleCloudStorage 对象。后

$config = [
            'projectId' => <MY Project ID>
            'keyFile'  => json_decode(file_get_contents( <my json key file> ), true)
          ];
$google_client = new Google_Client($config);
$google_client->setUseBatch(true);

$delete_batch_storage_client = new Google_Service_Storage($google_client);

$delete_batch = new Google_Http_Batch($google_client, false, NULL, "batch/storage");


$storage_client = new StorageClient($config);
$bucket = $storage_client->bucket(<my bucket name>);

$params = [
    'prefix' => <my image path>,
    'fields' => 'items/name, items/size, items/updated'
];

foreach ( $bucket->objects($params) as $object )
{
    $delete_batch->add($delete_batch_storage_client->objects->delete($bucket->name(), $object->name()));
}

$result = $delete_batch->execute();

但是在运行我的脚本时,它会构建请求,运行它,然后失败。就我所见,最好的问题就在这里:

"message": "Anonymous caller does not have storage.objects.delete access to the Google Cloud Storage object."

但是,检查 Google_Http_Batch 对象 execute() 函数的源代码,没有创建身份验证标头。我在设置中错过了什么?我会认为用 Google_Client 实例化 Google_Http_Batch 对象会提供该信息吗?但显然不是。

根据下面 John Hanley 的注释,转到控制台并查看我的服务帐户,它看起来具有以下角色:

Editor
Firebase Service Management Service Agent
Owner
Service Account Token Creator
Service Account User
Viewer

我原以为编辑者或所有者都有删除权限。

我在代码中只有一个明确的 USE 命令:

使用 Google\Cloud\Storage\StorageClient;

以下是我系统上当前 google 库的版本:

google/apiclient v2.4.1 google/apiclient-services v0.132 google/auth v1.8.0 google/cloud-core v1.39.0 google/cloud-storage v1.23.0PHP google/common-protos 1.3.1 google/crc32 v0. 1.0 google/grpc-gcp 0.1.5 google/protobuf v3.17.3 grpc/grpc 1.30.0 guzzlehttp/guzzle 6.5.3 guzzlehttp/promises v1.3.1 guzzlehttp/psr7 1.6.1 psr/cache 1.0.1 psr/http-message 1.0.1 psr/日志 1.1.3

我查看了批处理的内容,似乎每个删除请求上都应该有授权标头,或者(我认为更好)一个在顶层,但它们不存在。所以在实例化对象时我没有做正确的事情。

标签: phpgoogle-cloud-storage

解决方案


推荐阅读