【问题标题】:Uploading multiple files from a Google Cloud VM to Google Cloud Storage using node.js and Glob使用 node.js 和 Glob 将多个文件从 Google Cloud VM 上传到 Google Cloud Storage
【发布时间】:2019-04-10 15:19:56
【问题描述】:

我正在尝试 Node.js 将多个文件从我的 Google Compute Engine VM 本地目录上传到我已经创建的 GCS 存储桶。每次运行脚本时都会出现以下错误。

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type function

脚本:

 `// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
const fs = require ('fs');
const glob = require('glob');

// The name of the bucket to access, e.g. "my-bucket"
   const bucketName = "myBucket";


// Instantiates a client
const storage = new Storage({
  projectId: 'myprojectID', 
  keyFilename: 'my GCS service key'
});

//get files in the local directory of VM 

var allfiles = glob('folder/*.js', function (err, files) { 
    if (err) { 
        console.log(err); 
    }  
});


// Uploads VM local dir files  to the bucket
storage
  .bucket(bucketName)
  .upload(allfiles)
  .then(() => {
    console.log(`${allfiles} uploaded to ${bucketName}.`);
  })
  .catch(err => {
    console.error('ERROR:', err);
  });'

显然,“上传”过程需要文件路径名作为字符串。但这就是 Glob 函数应该做的事情。仍然为什么是错误?

任何帮助将不胜感激!

【问题讨论】:

    标签: node.js api npm google-cloud-storage glob


    【解决方案1】:

    您的错误是您使用allfiles 作为glob 的返回值。这是不正确的,文件名在回调中可用(因为 glob 是异步的),而不是在返回值中。

    glob('folder/*.js', function (err, files) { 
        if (err) { 
            console.log(err); 
        }
    
        var allfiles = files;
    
        // Uploads VM local dir files  to the bucket
       storage
      .bucket(bucketName)
      .upload(allfiles)
      .then(() => {
        console.log(`${allfiles} uploaded to ${bucketName}.`);
      })
      .catch(err => {
        console.error('ERROR:', err);
      });'  
    });
    

    【讨论】:

    • 嗨@mihai,非常感谢您的回答。我相信它正确设置了代码的上下文,但是在实施您的建议后我仍然遇到同样的错误......
    • console.log(allfiles) 打印什么?
    • 正在打印文件路径。
    • 你确定你得到的是同样的错误,而不是别的错误吗?
    【解决方案2】:

    所以我终于让脚本工作了。问题是,文件路径被作为对象捕获,但谷歌云存储服务需要路径作为字符串。

    然后必须使用 JSON.stringify 将路径更改为字符串,然后拆分结果数组以仅选择文件路径内容。

    // Imports the Google Cloud client library
        const {Storage} = require('@google-cloud/storage');
        const fs = require('fs');
        var bucketName = 'intended GCS bucketname';	
    
        const storage = new Storage({
         projectId: 'full project Id',  keyFilename: 'cloud service key'
        });
    
      //Invoke Glob for reading filepath in local/vm
      
         var glob = require('glob');
     
         glob('path to local/vm directory', function (err, files) {
     
        if (err) { 
            console.log(err); 
        }    
    	var list = JSON.stringify(files)         ; 
          list = list.split('"')   ;    
          var listone = list[1] ;
           
    storage
      .bucket(bucketName)
      .upload(listone)
      .then(() => {
    	      console.log(`${listone} uploaded to ${bucketName}.`);
    	    })
    	       .catch(err => {
    		           console.error('ERROR:', err);
    		         });  
    
     
    });

    【讨论】:

      最近更新 更多