File size is zero bytes on uploading image on google cloud storage — nodejs?

天涯浪子 提交于 2021-02-08 05:10:14

问题


This is my code for

const fs = require("fs");
const uuidv4 = require("uuid/v4");
const {Storage} = require("@google-cloud/storage");
const CLOUD_BUCKET = "priomark";
const storage = new Storage({
    projectId: 'priomark-v3',
    keyFilename: '../myapp/service.json'
})
const bucket = storage.bucket(CLOUD_BUCKET);

function upload(filess,cb) {
   var options = {
       metadata: {
          contentType: filess.mimeType
        }
   };
var file = filess;
var gcsname = uuidv4() + file.name;
// console.log(file.type)
var files = bucket.file(gcsname);
const blod = files.createWriteStream(options);
blod.on('error',(err)=>{
    console.log(err);
})

blod.on('finish', ()=>{
    console.log("file Uploaded",gcsname);
    cb(gcsname);
})

blod.end(file.buffer);
}

module.exports = {upload:upload};

Now code is working file is uploading but size of file is zero and on downloading its opening as a text file. Please Help

来源:https://stackoverflow.com/questions/52616023/file-size-is-zero-bytes-on-uploading-image-on-google-cloud-storage-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!