InvalidPolicyDocument,缺少政策错误谷歌云存储

时间:2016-06-22 21:33:22

标签: upload google-cloud-storage google-authentication gsutil axios

我正在尝试使用axios访问谷歌云存储桶以上传文件:

我将存储桶中的CORS策略设置为:

[
    {
      "origin": ["http://localhost:8000", "localhost"],
      "responseHeader": ["Access-Control-Allow-Origin", "Content-Type"],
      "method": ["GET", "HEAD", "DELETE", "PUT", "POST"],
      "maxAgeSeconds": 3600
    }
]

然后我使用这个gsutil命令生成一个签名的URL:

gsutil signurl -m RESUMABLE -d 1h my-key.json gs://test-bucket/

然后我最后发送了这个axios POST请求:

var startLink = "signed url from gsutil"
var data = {
  'Content-Length': 0,
  'Content-Type': 'text/plain',
  'x-goog-resumable': 'start',
  host: 'test-django-bucket.storage.googleapis.com',
};

axios.post(startLink, data)
  .then(function(response) {
    console.log(respone);
  });

我得到的结果是:

<?xml version='1.0'
encoding='UTF-8'?><Error><Code>InvalidPolicyDocument</Code><Message>The content of the form does not meet the conditions specified in the
policy document.</Message><Details>Missing policy</Details></Error>

我到底做错了什么?我按照here发现的说明进行操作。

更新 关于我必须解决的问题的一些注意事项,以便在@BrandonYarbrough下面的某些时间之后使所有工作正常工作:

首先axios请求是错误的,它应该是:

var data = {
  headers: {
    'content-type': 'text/plain',
    'x-goog-resumable': 'start',
  }
};
axios.post(startLink, {}, data)
  .then(function(response) {
    console.log(response);
  });

接下来我必须按如下所述更新gstuil命令:

gsutil signurl -m RESUMABLE -d 10h -c "text/plain" mykey.json gs://test-bucket

1 个答案:

答案 0 :(得分:2)

您需要向gsutil提供另外两条信息以添加到签名中:Content-Type和您正在创建的对象的名称。试试这个命令:

gsutil signurl -m RESUMABLE -d 1h -c "text/plain" my-key.json gs://test-bucket/object-name.txt

此外,gsutil可能会输出一个类似&#34; storage.googleapis.com/test-django-bucket/your_object?lotsOfUrlParameters&#34;的网址。如果您在指定&#34; test-django-bucket.storage.googleapis.com&#34;的主机标题时转到该URL,则看起来您实际上想要一个名为&#34的对象; test-django -bucket / your_object&#34;在一个名为&#34; test-django-bucket&#34;的桶内。删除主机标头并直接点击storage.googleapis.com,或者编辑gsutil返回的URL以删除&#34; test-django-bucket&#34;位。

此外,我认为您正在将标头作为数据发送。我认为axios标题是使用&#34;标题&#34;设置的。配置部分。