我正在尝试将.jpeg
文件上传到Google云端存储,但我的请求失败,并出现以下401错误。
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Anonymous users does not have storage.objects.create access to bucket XXXtest.",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Anonymous users does not have storage.objects.create access to bucket XXXtest."
}
}
我的POST请求看起来像这样。
<form id="upload_image_form" class="" action="https://www.googleapis.com/upload/storage/v1/b/XXXtest/o?uploadType=media&name=myObject" method="post" enctype="multipart/form-data">
<input id="image" name="image" type="file" accept="image/x-png, image/jpeg, image/png" onchange="">
<input type="hidden" name="Content-Type" value="image/jpeg">
<input type="hidden" name="name" value="my_name">
<input type="hidden" name="Accept" value="application/json, text/json, text/x-json, text/javascript">
<input type="hidden" name="host" value="www.googleapis.com">
<input type="hidden" name="Transfer-Encoding" value="chunked">
<input type="hidden" name="Authorization" value="<?php echo 'Bearer'.' '.$_SESSION['access_token']['access_token']; ?>">
<input type="hidden" name="Expect" value="100-continue">
<input type="hidden" name="Accept-Encoding" value="gzip, deflate">
<input type="hidden" name="Connection" value="Keep-Alive">
<input type="hidden" name="acl" value="bucket-owner-writer">
<input id="upload" name="upload" value="Upload" type="submit" style="position: absolute;">
</form>
根据这个documentation,你能告诉我我错过了什么吗?
答案 0 :(得分:1)
要允许用户通过表单POST将对象上传到GCS,您需要创建一个&#34;策略文档&#34;描述用户可以上传的对象类型,然后对其进行签名,然后将该签名文档作为参数传递给表单。您可以在此处阅读有关通过POST上传到GCS的更多信息:https://cloud.google.com/storage/docs/xml-api/post-object
请特别注意有关签署的政策文件的部分:https://cloud.google.com/storage/docs/xml-api/post-object#policydocument
以下是一份示例政策文件:
{"expiration": "2010-06-16T11:11:11Z",
"conditions": [
["starts-with", "$key", "" ],
{"acl": "bucket-owner-read" },
{"bucket": "travel-maps"},
{"success_action_redirect": "http://www.example.com/success_notification.html" },
["eq", "$Content-Type", "image/jpeg" ],
["content-length-range", 0, 1000000]
]
}
然后,您将该文档作为base64,使用您希望用户为上载目的而使用的服务帐户的私钥对结果进行签名,然后在隐藏字段中提供这些内容,如下所示:
<input type="hidden" name="policy" value="eyJleHBpcmF0aW9uIjogIjIwMTAtMDYtMTZUMTE6MTE6MTFaIiwNCiAiY29uZGl0aW9ucyI6IFsNCiAgWyJzdGFydHMtd2l0aCIsICJrZXkiLCAiIiBdLA0KICB7ImFjbCI6ICJidWNrZXQtb3duZXItcmVhZCIgfSwNCiAgeyJidWNrZXQiOiAidHJhdmVsLW1hcHMifSwNCiAgeyJzdWNjZXNzX2FjdGlvbl9yZWRpcmVjdCI6ICJodHRwOi8vd3d3LmV4YW1wbGUuY29tL3N1Y2Nlc3Nfbm90aWZpY2F0aW9uLmh0bWwiIH0sDQogIFsiZXEiLCAiQ29udGVudC1UeXBlIiwgImltYWdlL2pwZWciIF0sDQogIFsiY29udGVudC1sZW5ndGgtcmFuZ2UiLCAwLCAxMDAwMDAwXQ0KICBdDQp9">
<input type="hidden" name="signature" value="BSAMPLEaASAMPLE6SAMPLE+SAMPPLEqSAMPLEPSAMPLE+SAMPLEgSAMPLEzCPlgWREeF7oPGowkeKk7J4WApzkzxERdOQmAdrvshKSzUHg8Jqp1lw9tbiJfE2ExdOOIoJVmGLoDeAGnfzCd4fTsWcLbal9sFpqXsQI8IQi1493mw=">
答案 1 :(得分:0)
我使用以下代码成功完成了此操作。我使用此处Resumable uploading to Google Cloud Storage using PHP API
中提供的云api
方法做到了这一点