将图像从网页上传到谷歌云存储

Uploading an image to Google Cloud storage from webpage

本文关键字:谷歌 存储 图像 网页      更新时间:2023-09-26

我正在使用谷歌在这里提供的文档,尝试将图像从网页上传到谷歌云存储。我认为存储设置正确,我认为我得到了正确的值。

使用时,图像会被识别,但由于某些原因,无法上传。我不确定是代码问题还是环境设置问题。此外,没有报告任何错误消息。我知道图像将通过事件进入函数。

javascript是…

        /**
         * Google Cloud Storage API request to insert an object into
         * your Google Cloud Storage bucket.
         */
        function insertObject(event) {
            try {
                //var MyFile = document.getElementById("take-picture").files;
                var fileData = event.target.files[0];
                //var fileData = document.getElementById("take-picture").files;
            } 
            catch(e) {
                //'Insert Object' selected from the API Commands select list
                //Display insert object button and then exit function
                //filePicker.style.display = 'block';
                return;
            }
          var boundary = '-------314159265358979323846';
          var delimiter = "'r'n--" + boundary + "'r'n";
          var close_delim = "'r'n--" + boundary + "--";
            var reader = new FileReader();
            reader.readAsBinaryString(fileData);
            reader.onload = function(e) {
                var contentType = fileData.type || 'application/octet-stream';
                var metadata = {
                    'name': fileData.name,
                    'mimeType': contentType
                };
                var base64Data = btoa(reader.result);
                var multipartRequestBody =
                  delimiter +
                  'Content-Type: application/json'r'n'r'n' +
                  JSON.stringify(metadata) +
                  delimiter +
                  'Content-Type: ' + contentType + ''r'n' +
                  'Content-Transfer-Encoding: base64'r'n' +
                  ''r'n' +
                  base64Data +
                  close_delim;
                //Note: gapi.client.storage.objects.insert() can only insert
                //small objects (under 64k) so to support larger file sizes
                //we're using the generic HTTP request method gapi.client.request()
                var request = gapi.client.request({
                    'path': '/upload/storage/' + API_VERSION + '/b/' + BUCKET + '/o',
                    'method': 'POST',
                    'params': {'uploadType': 'multipart'},
                    'headers': {
                        'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
                    },
                    'body': multipartRequestBody});
                //Remove the current API result entry in the main-content div
                //listChildren = document.getElementById('main-content').childNodes;
                //if (listChildren.length > 1) {
                //    listChildren[1].parentNode.removeChild(listChildren[1]);
                //} 
                //look at http://stackoverflow.com/questions/30317797/uploading-additional-metadata-as-part-of-file-upload-request-to-google-cloud-sto
                try{
                    //Execute the insert object request
                    executeRequest(request, 'insertObject');
                    //Store the name of the inserted object 
                    object = fileData.name;         
                }
                catch(e) {
                    alert('An error has occurred: ' + e.message);
                }
            }
        }

不太确定问题出在哪里。

但在我的工作代码中,我使用

var metadata = {
    'title': fileData.fileName || fileData.name,
    'parents': [{
    "kind": "drive#fileLink",
            "id": folderId
    }],
    'mimeType': contentType
};

因此,您的代码可能缺少文件夹的ID

API请求:

var request = gapi.client.request({
        'path': '/upload/drive/v2/files',
        'method': 'POST',
        'params': {'uploadType': 'multipart'},
        'headers': {
          'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
        },
        'body': multipartRequestBody
});

FYR。