简体   繁体   English

使用javascript将请求批量上传到Google云端存储

[英]Batch upload requests to Google Cloud Storage using javascript

I'm trying to upload multiple images to google cloud storage in a batch request using javascript. 我正在尝试使用javascript在批量请求中将多个图像上传到Google云端存储。 I'm using https://developers.google.com/storage/docs/json_api/v1/how-tos/batch#example as reference. 我使用https://developers.google.com/storage/docs/json_api/v1/how-tos/batch#example作为参考。

I have an input file where the user can select multiple files, and an 'upload' btn to upload to GCS like so: 我有一个输入文件,用户可以选择多个文件,并上传'上传'btn上传到GCS,如下所示:

<input type="file" name="fileName" id="fileInput" multiple="multiple" onchange="javascript: loadFiles()"/> 
<input type="button" name="upload-btn" id="upload" value="Upload"/>

When the user selects the images, the 'loadFiles' function creates the 'body' of the batch request. 当用户选择图像时,'loadFiles'功能创建批量请求的“主体”。

var tok = <token>;
var boundary = "---======= foo_bar_baz12034245623562346 ===";
var consolidated_request = '';

function loadFiles()
{
        var input = $('fileInput');
        for (var i = 0; i < input.files.length; i++) 
        {
            var f = input.files[i];

            var reader = new FileReader();
            reader.readAsBinaryString(f);

            reader.onload = function(e){

                var fbinary = e.target.result;
                var fsize = f.size;

                var url = 'https://www.googleapis.com/upload/storage/v1beta2/b/<mybucket>/o?';
                url += 'uploadType=media&name='+f.name+ ' HTTP/1.1';

                var req = boundary + 
                '\r\nContent-Type: application/http'+
                '\r\nContent-Transfer-Encoding: binary'+
                '\r\n\nPOST ' + url +
                '\r\nContent-Type: image/jpeg'+
                '\r\nContent-Length: '+ f.size +
                '\r\nAuthorization: '+tok+
                '\r\n\n'+ fbinary + '\n';

                consolidated_request += req;
            };

        }
}

When the user clicks on upload : 当用户点击上传时:

$('upload').onclick = function(e){

    var xhr = new XMLHttpRequest();
    xhr.open("POST", 'https://www.googleapis.com/batch', true);
    xhr.setRequestHeader("Authorization", tok);
    xhr.setRequestHeader("Content-Type", "multipart/mixed;boundary=" + boundary);
    xhr.send(consolidated_request);
 };

Here is a sample of the POST generated (using firebug): 以下是生成的POST示例(使用firebug):

Header: 标题:

Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Authorization   Bearer ya29.AHES6ZQgu6gFurD6y7Bo2Mao1RNCFwyqNZcwvgDZ82RXIbQ4
Content-Length  159866
Content-Type    multipart/mixed; charset=UTF-8;boundary=---======= foo_bar_baz12034245623562346 ===
Host    www.googleapis.com

Body: 身体:

 --======= foo_bar_baz12034245623562346 === 
Content-Type: application/http 
Content-Transfer-Encoding: binary 

POST https://www.googleapis.com/upload/storage/v1beta2/b/<mybucket>/o?uploadType=media&name=myimage.jpg HTTP/1.1 
Content-Type: image/jpeg 
Content-Length: 69436 
Authorization: Bearer ya29.AHES6ZQgu6gFurD6y7Bo2Mao1RNCFwyqNZcwvgDZ82RXIbQ4 

ÿØÿà�JFIF���d�d��ÿì�Ducky�����<��ÿî�Adobe�dÀ���ÿÛ�� 
...

The problem is that there is no response because I get 400 bad request msg. 问题是没有响应,因为我得到400个错误的请求消息。 What is wrong with that request? 这个请求有什么问题?

You are sending POST request as - 您正在发送POST请求 -

POST https://www.googleapis.com/upload/storage/v1beta2/b/<mybucket>/o?uploadType=media&name=myimage.jpg HTTP/1.1 

As stated in upload objects with POST request - POST请求的上传对象中所述 -

To upload media, you use a special URI. 要上传媒体,请使用特殊URI。 POST methods support media uploads have two URI endpoints: POST方法支持媒体上传有两个URI端点:

  1. The /upload URI , for the media. / upload URI ,用于媒体。 The format of the upload endpoint is the standard resource URI with an “/upload” prefix. 上传端点的格式是带有“/ upload”前缀的标准资源URI。 Use this URI when transferring the media data itself. 传输媒体数据本身时使用此URI。 Example: POST /upload/storage/v1beta2/b/myBucket/o . 示例: POST /upload/storage/v1beta2/b/myBucket/o
  2. The standard resource URI , for the metadata. 标准资源URI ,用于元数据。 If the resource contains any data fields, those fields are used to store metadata describing the uploaded file. 如果资源包含任何数据字段,则这些字段用于存储描述上载文件的元数据。 You can use this URI when creating or updating metadata values. 您可以在创建或更新元数据值时使用此URI。 Example: POST /storage/v1beta2/b/myBucket/o. 示例:POST / storage / v1beta2 / b / myBucket / o。

So you need to send POST request as - 所以你需要发送POST请求 -

POST /upload/storage/v1beta2/b/myBucket/o?uploadType=media&name=myimage.jpg HTTP/1.1

Try sending POST request by omitting prefix https://www.googleapis.com from url. 尝试从url中省略前缀https://www.googleapis.com来发送POST请求。

Thanks 谢谢

Neelam Sharma 尼拉姆夏尔马

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JavaScript从客户端浏览器上传到Google云端存储 - Upload from Client Browser to Google Cloud Storage Using JavaScript 通过javascript将文件上传到Google云存储 - Upload file to google cloud storage by javascript 使用 javascript 上传到谷歌云存储签名 url - upload to google cloud storage signed url with javascript 如何使用蚂蚁设计上传组件在JavaScript中实现XMLHttpRequest,以发送到Google云存储? - How do you implement XMLHttpRequest in javascript using ant design Upload component to send to google cloud storage? Google Cloud Storage - 如何直接从浏览器上传内容 (JavaScript) - Google Cloud Storage - How to Upload content directly from browser (JavaScript) 使用AngularJS多部分表单数据将文件上传到Google云端存储 - Upload file to Google Cloud Storage using AngularJS multipart form data 使用 Java 和 JS 可恢复上传到 Google Cloud Storage - Resumable upload to Google Cloud Storage using Java and JS 使用PHP将文件上传到Google Cloud Storage Bucket - Upload files to Google Cloud Storage Bucket using PHP Google云端存储Javascript使用情况 - Google Cloud Storage Javascript Usage 创建上传文件到谷歌云存储 - Create an upload file to google cloud storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM