使用Signed URL和dropzone(Vuejs)上传到Google云存储

时间:2018-11-15 17:45:48

标签: vue.js google-cloud-storage axios dropzone.js vue-dropzone

我能够使用下面的表单提交直接上传到Google云存储(在使用Nodejs获得签名的URL之后)

<form action="https://<%=fields.bucket%>.storage.googleapis.com" method="post" enctype="multipart/form-data">
    <input type="hidden" name="key" value="<%=fields.key%>">
    <input type="hidden" name="bucket" value="<%=fields.bucket%>">
    <input type="hidden" name="GoogleAccessId" value="<%=fields.GoogleAccessId%>">
    <input type="hidden" name="policy" value="<%=fields.policy%>">
    <input type="hidden" name="signature" value="<%=fields.signature%>">
    <input type="hidden" name="Content-Type" value="<%=fields['Content-Type']%>">

    <input name="file" type="file">
    <input type="submit" onclick="myFunction(event)" value="Upload">
</form>

但是当使用以下方法使用dropzone时,出现400错误。我成功获得了我的签名URL,并且没有任何问题。问题出在axios post方法上。

 uploadOptions: {
    url: "/",
    uploadMultiple: false,
    method: "PUT",
    parallelUploads: 1,
    headers: {
              "Content-Type": "multipart/form-data"
            },
    autoProcessQueue: false,
    autoDiscover: false,
    acceptedFiles: "image/*",
    maxFilesize: 5,
    maxFiles: 10,
    clickable: "#upload",
    addRemoveLinks: true,
    preventDuplicates: true,
    dictDuplicateFile: "Duplicate Files Cannot Be Uploaded",
    previewsContainer: ".dropzone-previews",
    dictDefaultMessage: "",
    thumbnailWidth: 200,
     accept: function(file, done) {
      var self = this;


      var FileSize = file.size;
      console.log(" In accept");

      k1Object.$axios
        .$get("my-board?operation=fileUpload&name=" + file.name)
        .then(function(gcmData) {
          let postData = gcmData[1];
          let actionURL =
            "https://" + postData.bucket + ".storage.googleapis.com";
          file.uploadURL = actionURL;
          k1Object.k1FormData = gcmData[1];
                    done();
          setTimeout(function() {
          self.processFile(file);
          }, 0)
        });


    }, 
     init: function() {
      var self = this;
      this.on("processing", function(file) {
        self.options.url = file.uploadURL;
        console.log("processing");
      });
      this.on("sending", function(file, xhr, formData) {
        console.log(k1Object.k1FormData.key)
        formData.append("key", k1Object.k1FormData.key);
        formData.append("bucket", k1Object.k1FormData.bucket);
        formData.append("GoogleAccessId", k1Object.k1FormData.GoogleAccessId);
        formData.append("policy", k1Object.k1FormData.policy);
        formData.append("signature", k1Object.k1FormData.signature);
        formData.append("Content-Type", k1Object.k1FormData["Content-Type"]);

         var _send = xhr.send;
        xhr.send = function() {
          _send.call(xhr, file);
        }; 
      });
    } 
  }

400错误

<?xml version='1.0' encoding='UTF-8'?><Error><Code>MissingSecurityHeader</Code><Message>Your request was missing a required header.</Message><Details>Authorization</Details></Error>

我不确定它期望什么字段,甚至为存储桶设置了CORS规则。谢谢。

1 个答案:

答案 0 :(得分:0)

这篇文章帮助我解决了我的问题。基本上,错误的是我的文件输入是表单的第一个孩子,而不是结尾。请查看下面的stackoverflow链接以获取完整答案。

https://stackoverflow.com/a/17524079/274715

我仍然不确定为什么将文件移到末尾对我有帮助。