Gcloud API文件.save()数据格式



我在Nodejs web服务器上使用gcloud API上传文件。我更希望这些文件不要上传到客户端,而是上传到服务器上。目前,我正在客户端生成一个blob,然后将其转换为文本,并通过POST请求将其传递给服务器。所有信息都按预期成功地从客户端传递到服务器。这些数据也被上传到gcloud,然而,当我下载它时,gcloud不会将其识别为有效文件,我的电脑也不会。

从服务器端将文件内容获取到gcloud的最佳方式是什么?我尝试过使用dataURI和通过文本读取原始文件,两者都会产生类似的问题。我还研究了从服务器端的blob管道传输readFileStream,但节点本身不支持blob,所以我还没有这样做。

客户端

function readSingleFile(e, func, func2){
var file = e.target.files[0];
if(!file){
return; // Add error msg_here
}
var reader = new FileReader();
reader.onload = function(e){
let contents = e.target.result;
let img = document.createElement('img')
let cvs = document.createElement('canvas');
img.onload = ()=>{
cvs.width = img.width;
cvs.height= img.height;
let ctx = cvs.getContext('2d');
ctx.drawImage(img,0,0);
cvs.toBlob((res)=>{res.text().then((text)=>{func2(text)})}, "image/jpeg", 0.92);
}
img.src=contents;
func(contents);
}
reader.readAsDataURL(file);
}

服务器端

function publishPrintjob(dataObj){
try{
var newElemKey = database.ref().child('queue').push().key; // Get random Key
// Create a new blob in the bucket and upload the file data.
const gcloudFile = storage.file('images/' + newElemKey + '.jpg');
gcloudFile.save(dataObj.sockImageFile, function(err) {
if (!err) {
Console.log("File Uploaded!")
}
});
var data = {
date: dataObj.Date,
email: dataObj.email,
design: dataObj.Design,
author: dataObj.Author,
address: dataObj.address,
imageKey: newElemKey,
}
admin.database().ref('queue/' + newElemKey).set(data);
} catch(err){
console.log(err)
}
}

注意:func只是在客户端显示图像,func2只是将内容添加到POST对象中。

使用云存储库中的storage.bucket(bucketName).upload()函数,直接从计算机上传文件是最简单的。但,这会在本地使用文件的位置,因此除非先将文件传输到服务器并保存,否则将无法工作。这可以使用多部分表单数据来实现。使用多部分或本地上传是上传到谷歌存储的更好方法。

相反,我首先将图像转换为dataURI,通过GET请求的主体将数据URI发送到服务器,然后将其转换为具有可读流的缓冲区,该缓冲区可以通过管道传输到谷歌存储。

客户端

let formData = getFormData('myForm');

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
}
};
xhttp.open("POST", "dashboard", true);
xhttp.setRequestHeader('Content-Type', 'application/json');
xhttp.send(JSON.stringify(formData));
xhttp.onload = ()=> {
console.log(JSON.parse(xhttp.response))
// Handle server response here
};
}

服务器

// DataObject is the body of the GET request, the property imageFile is the URI from readFileAsURI
function uploadImageOnServer(dataObj){
try{
var newElemKey = database.ref().child('queue').push().key; // Get random Key to use as filename
// Create a new blob in the bucket and upload the file data.
const gcloudFile = storage.file('images/' + newElemKey + '.jpeg');


var fs = require('fs'); // Should be required at the top of the file
var string = dataObj.ImageFile; 
var regex = /^data:.+/(.+);base64,(.*)$/;

var matches = string.match(regex);
var ext = matches[1];
var data = matches[2];
var buffer = Buffer.from(data, 'base64');

// Create the readstream
const readableInstanceStream = new Readable({
read() {
this.push(buffer);
this.push(null);
}
});
readableInstanceStream.pipe(gcloudFile.createWriteStream()) // link to gcloud storage api
.on('error', function(err) {
console.log('error')
})
.on('finish', function() {
console.log('upload complete')
});
} catch(err){
console.log(err)
}

}

最新更新