POST上传后无法在Google Cloud Storage中打开文件

时间:2018-11-20 14:07:25

标签: xml google-cloud-storage

我在使用REST API(此处解释:https://cloud.google.com/storage/docs/json_api/v1/how-tos/multipart-upload)将pdf文件上传到我的Google Cloud Storage存储桶时遇到了一些麻烦。响应给我一个http状态200,但是当我尝试从存储桶中打开或下载pdf文件时,它给出了无效或损坏的PDF文件。在下面查看我正在使用的代码:

var hostname = 'https://www.googleapis.com';
var file = 'pathToFile';

var file = WScript.CreateObject('ADODB.Stream');
  file.Type = 1;  
  file.Open();
  file.LoadFromFile(file);
  file.Position = 0;  

var http = WScript.CreateObject('Msxml2.ServerXMLHTTP.6.0');

http.open('POST', hostname + '/upload/storage/v1/b/[myBucket]/o', false);

var boundary = 'foo_bar_baz';

http.setRequestHeader('Content-Type', 'multipart/related; boundary=' + boundary);
http.setRequestHeader('Authorization', [myToken]);

var xml = WScript.CreateObject("MSXML2.DOMDocument");
var converter = xml.appendChild( xml.createElement("converter") );
    converter.dataType = "bin.base64";
    converter.nodeTypedValue = file.Read(file.Size);

boundary = '--' + boundary;

var body = boundary + '\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n' + '{"name":"my.pdf", "mimeType": "application/pdf"}' + '\r\n'
    body += '\r\n' + boundary + '\r\nContent-Type: application/pdf\r\n\r\n' + converter.text + '\r\n';
    body += '\r\n' + boundary + '--'

http.send( body );

---- UPDATE ----

请参见下面的正文

--foo_bar_baz
Content-Type: application/json; charset=UTF-8

{"name":"myPDF.pdf", "mimeType": "application/pdf"}

--foo_bar_baz
Content-Type: application/pdf

[base64 binary string]

--foo_bar_baz--

谢谢!

0 个答案:

没有答案