[英]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端点:
POST /upload/storage/v1beta2/b/myBucket/o
. POST /upload/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.