如何仅使用 OAuth 2.0 不记名令牌和存储桶名称在 google 云存储中生成已签名的 URL 文件? (php 代码)

How to generate a Signed URL of file in google cloud storage with just OAuth 2.0 bearer token and bucket name? (php code)

我已经尝试了下面的代码,它工作正常。但是,如果我仅使用 OAuth 2.0 访问令牌和存储桶名称就可以获得 Signed URL,那将是理想的选择。

我尝试了很多东西,但仍然无处可去。

function getSignedURL($file, $expiryMinutes = 30)
{

    $privateKeyFileContent = '{
     // service account generated key
    }';

    $storage = new StorageClient([
        'keyFile' => json_decode($privateKeyFileContent, true)
    ]);
    $bucket = $storage->bucket("as-portal");
    $object = $bucket->object($file);
    $url = $object->signedUrl(
    # This URL is valid for 15 minutes
        new \DateTime("$expiryMinutes min"),
        [
            'version' => 'v4',
        ]
    );
}

如有任何帮助,我们将不胜感激!

目前所有可用于签署 URL 的方法都需要服务帐户的私钥,如 Options for generating a signed URL 中所述。因此,如果没有服务帐户的私钥,就无法在本地签署 URL。

但是,您可以按照文档 this example. I would suggest having an App Engine service sign the urls for you, like explained in the Signing strings with Google Cloud tools 部分中的说明对其进行远程签名。