是否可以仅使用 Authorization Bearer Token 和 Bucket 名称将文件上传到 google 云存储? (php 代码)

Is it possible to upload files to google cloud storage with just Authorization Bearer Token and Bucket name? (php code)

目前我可以使用我的服务帐户凭据在 google 云存储中上传文件。

但理想情况下,如果可能的话,我想上传 google 云存储中的文件,只包含不记名令牌和存储桶名称。

我相信您正在寻找的是 JSON 和 XML API。这些允许仅使用授权持有人 header 上传到 Google 云存储。您可以在 Uploading objects 文档中找到有关如何通过这些 API 上传 objects 的更多信息。

您需要从 PHP 卷曲以上传文件,方法是执行类似下面的代码片段:

$ch = curl_init();
$image = file_get_contents('my_image.jpeg');

curl_setopt($ch, CURLOPT_URL, "https://storage.googleapis.com/upload/storage/v1/b/[BUCKET_NAME]/o?uploadType=media&name=my_image.jpeg");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $image); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: image/jpeg', 'Authorization: Bearer ' . $oauth_token)); 

$result = curl_exec($ch);
curl_close($ch);