使用Google云端存储POST对象XML API

时间:2018-06-13 08:22:26

标签: google-cloud-platform google-cloud-storage

我已经创建了一个使用POST对象XML API将文件上传到Google云端存储的表单。

除了超过50KB的文件外,此工作正常。我已经在Google Chrome和Firefox上进行了测试。

这是HTML

    <form id="upload-form"
          method="POST"
          role="search">
      <div class="form-group">
        <label class="col-sm-2 control-label">File input</label>
        <div class="col-sm-10">
          <input type="file" class="filestyle"
                 name="file">
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-4 col-sm-offset-2">
          <button type="submit" class="btn btn-primary">Upload</button>
        </div>
      </div>
    </form>

这是JavaScript

$(function () {
    $('#upload-form').submit(function (e) {
        e.preventDefault();
        $.post("/experimental/upload/signed-url").then(function (info) {

            var form = $('#upload-form')[0];
            var formData = new FormData(form);
            formData.append('googleAccessId', info.googleAccessId);
            formData.append('policy', info.policy);
            formData.append('signature', info.signature);
            formData.append('bucket', info.bucket);
            formData.append('key', info.key);
            formData.append('success_action_status', info.successActionStatus);
            $.ajax({
                url: info.url,
                data: formData,
                type: 'POST',
                contentType: false,
                processData: false,
            }).then(function (res2) {
                console.log(res2);
            });
        });
    });
});

此端点/ experimental / upload / signed-url会创建类似这样的响应

    {
      "googleAccessId" : "xxxx",
      "policy" : "REDACTED",
      "signature" : "REDACTED",
      "successActionStatus" : "201",
      "url" : "https://storage.googleapis.com/my-bucket",
      "bucket" : "my-bucket",
      "key" : "path/file-name",
      "clientName" : null,
      "publicUrl" : "https://storage.googleapis.com/my-bucket/path/file-name"
    }

创建签名的代码如下所示

    try {
        byte[] policy = policyDocument.toJsonBytes();
        System.out.println(policyDocument.toJson());
        String encodedPolicy = BaseEncoding.base64().encode(policy);
        ServiceAccountCredentials cred = ServiceAccountCredentials
            .fromStream(new FileInputStream(googleCredentialsFile));
        byte[] signatureBytes = cred.sign(encodedPolicy.getBytes(Charsets.UTF_8));
        String signature = BaseEncoding.base64().encode(signatureBytes);
        info.setGoogleAccessId(cred.getAccount());
        info.setPolicy(encodedPolicy);
        info.setSignature(signature);
        info.setSuccessActionStatus(STATUS_CODE);
        info.setUrl(String.format(END_POINT_URL_FORMAT, info.getBucket()));
        return info;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

这是政策文件的样子

  {
    "conditions": [
      {
        "success_action_status": "201"
      },
      {
        "bucket": "XXXXX"
      },
      {
        "key": "path/file-name"
      },
      [
        "content-length-range",
        0,
        1895825408
      ]
    ],
    "expiration": "2018-06-13T12:51:45.143+03:00"
  }

对于超过50 KB的文件,我获得了此主体的HTTP状态

<?xml version="1.0" encoding="UTF-8"?>
<Error>
   <Code>InvalidArgument</Code>
   <Message>Invalid argument.</Message>
   <Details>Cannot create buckets using a POST.</Details>
</Error>

是否有人可以帮我上传超过50KB的内容?

2 个答案:

答案 0 :(得分:1)

我有GCP支持。使用基于this documentation的POST请求,我们可以得到相同的错误消息,而与文件大小无关。

通过添加对象"url" : "https://storage.googleapis.com/my-bucket",

,我们将等价物更改为"url" : "https://storage.googleapis.com/my-bucket/my-file.ext",,此问题已得到解决。

然后使用POST不断收到一些错误消息,因此我们将方法更改为PUT。然后它起作用了。

卷曲请求无效:

curl -d @[FILENAME.EXT] @[JSONFILE.json] -X POST -H 'Authorization: Bearer [TOKEN]' http://[BUCKET_NAME].storage.googleapis.com/ 

工作卷曲请求:

curl -v H 'Authorization: Bearer [TOKEN]' -F upload=@[FILENAME.EXT] -X PUT http://[BUCKET_NAME].storage.googleapis.com/[FILENAME.EXT]

我还要求通过总结经验教训来改进文档。

答案 1 :(得分:0)

将文件字段设置为表单的最后一个字段,即

  var frm = new FormData(this);
  var file_path = uploadForm.get("file");
  uploadForm.delete("file");
  uploadForm.append("file", file_path);
  ajax.send(uploadForm);