Upload a file to Google Cloud, in a specific directory

前端 未结 6 1655
借酒劲吻你
借酒劲吻你 2021-02-04 04:31

How to upload a file on Google Cloud, in a specific bucket directory (e.g. foo)?

\"use strict\";

const gcloud = require(\"gcloud\");

const PROJECT         


        
6条回答
  •  既然无缘
    2021-02-04 05:15

    If accessing from the same project projectId , keyFilename,.. not required,I use the below code for both upload and download , it works fine.

    // Imports the Google Cloud client library
    const Storage = require('@google-cloud/storage');
    const storage = new Storage();
    var destFilename = "./test";
    var bucketName = 'cloudtesla';
    var srcFilename = 'test';
    
      const options = {
        destination: destFilename,
      };
    
    
    //upload file
    console.log("upload Started");
    storage.bucket(bucketName).upload(srcFilename, {}, (err, file) => {
    
            if(!err)
            console.log("upload Completed");
            else
            console.log(err);
    });
    
    
    //Download file
    console.log("Download Started");
      storage
        .bucket(bucketName)
        .file(srcFilename)
        .download(options)
        .then(() => {
          console.log("Download Completed");
        })
        .catch(err => {
          console.error('ERROR:', err);
        });
    

提交回复
热议问题