我想让我的用户直接将大文件上传到 Google 存储。根据此文档,我应该能够使用 PHP SDK 生成签名的上传 url。我这样做是这样的:
$storage = new StorageClient(['keyFilePath' => $myKeyPath ]);
$bucket = $storage->bucket($bucketname);
$object = $bucket->object($relativepath);
$sessionURI = $object->signedUploadUrl(
# This URL is valid for 15 minutes
new \DateTime('15 min'),
[
'method' => 'PUT',
'contentType' => 'application/octet-stream',
'version' => 'v4',
]
);
文档说我需要POST
返回$sessionURI
才能获得我可以上传到的实际网址。文档:
签名上传网址
为此对象创建一个签名上传 URL。
...
为了上传数据,必须通过向此方法返回的 URL 发送 HTTP POST 请求来获取会话 URI。
但是,当我尝试到POST
时$sessionURI
,我收到错误:
POST object expects Content-Type multipart/form-data
所以,我明确地设置了Content-Type
,multipart/form-data
然后我得到了错误:
Bad content type. Please use multipart.
我已经尝试从代码中执行此操作以及在命令行上使用 curl ,并且我尝试设置Content-Type
但似乎没有任何效果(我添加了一些新行以使其可读,但最初都是一行):
/usr/bin/curl -X POST -H "Content-Type: multipart/form-data"
https://storage.googleapis.com/my-project-id.appspot.com/uploads/uploadfilename.jpg?GoogleAccessId=myserviceaccount
@mydomain.com&Expires=1591657512&Signature=veryverylongsignatureasdfasdfasdfasdfadfa
sdfasdfasdfasdfasdfasdfasdfasdfasdfadsfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfa
sdfasdfasdfasdfasdfasdfasdfasdfasdf
我尝试了一堆不同的示例,并且尝试完全按照文档中的说明进行操作,但似乎没有任何效果。
我错过了什么?我究竟做错了什么?
- - 更新
我尝试使用 Google 的示例代码回到开头,但仍然无法正常工作。这是 Google 的示例代码给我的 curl 命令:
curl -X PUT -H 'Content-Type: application/octet-stream' --upload-file /path/to/file.jpg
https://storage.googleapis.com/my-project-id.appspot.com/uploads/uploadfilename.jpg?GoogleAccessId=myserviceaccount
@mydomain.com&Expires=1591657512&Signature=veryverylongsignatureasdfasdfasdfasdfadfa
sdfasdfasdfasdfasdfasdfasdfasdfasdfadsfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfa
sdfasdfasdfasdfasdfasdfasdfasdfasdf
那给了我:
Anonymous caller does not have storage.objects.create access to the Google Cloud Storage object.