GAE PHP 7 403 FORBIDDEN 错误,同时 URL 上传文件

GAE PHP 7 403 FORBIDDEN error while got URL to upload file

我在管理从 PHP5 到 PHP7 的迁移时遇到了一些问题。

实际上,我用 php5 做的事情是这样的:URL 上传文件的地方:

$options = [ 'gs_bucket_name' => 'mybucket.appspot.com' ];
$myvar = $_GET['myvar'];

$upload_url = CloudStorageTools::createUploadUrl($myvar, $options);

return $upload_url;

我已经上传了库,现在我正在尝试使用

Google\Cloud\Storage\StorageClient

我已经将上面的代码转换成这样:

$bucketName = 'mybucket.appspot.com';
$objectName =  $_GET['myvar'];

$storage = new StorageClient([
        'scopes' => [
        'https://www.googleapis.com/auth/iam',
        'https://www.googleapis.com/auth/devstorage.full_control',
        ]
    ]);
    
 $bucket = $storage->bucket($bucketName);
 $object = $bucket->object($objectName);
 $upload_url = $object->signedUrl(
            new \DateTime('90 min'),
            [
                    'version' => 'v4',
            ]
    );

 return $upload_url;

我收到 403 FORBIDDEN 错误

Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `POST https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/myproject@appspot.gserviceaccount.com:signBlob?alt=json` resulted in a `403 Forbidden` response:
{
  "error": {
    "code": 403,
    "message": "IAM Service Account Credentials API has not been used in project 123456 (truncated...) 
....

有什么建议吗?在 PHP5 版本中上传工作正常(使用旧代码),所以我想我的应用程序 angine 服务帐户设置了正确的权限。

谢谢

通过传递 .json 文件进行身份验证解决。

$storage = new StorageClient([
    'projectId' => "my_project",
    'keyFilePath' => 'key/serviceaccount.json', 
]);