Uploading multiple files from a Google Cloud VM to Google Cloud Storage using node.js and Glob











up vote
1
down vote

favorite












I am trying Node.js to upload multiple files from my Google Compute Engine VM local directory to a GCS bucket that I have already created. I am getting the following error each time I am running the script.



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



The script:






 `// 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);
});'





Apparently the 'Upload' process needs the file pathnames as string. But that's what the Glob function is supposed to do. Still why is the error?



Any help will be seriously appreciated!










share|improve this question




























    up vote
    1
    down vote

    favorite












    I am trying Node.js to upload multiple files from my Google Compute Engine VM local directory to a GCS bucket that I have already created. I am getting the following error each time I am running the script.



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



    The script:






     `// 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);
    });'





    Apparently the 'Upload' process needs the file pathnames as string. But that's what the Glob function is supposed to do. Still why is the error?



    Any help will be seriously appreciated!










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am trying Node.js to upload multiple files from my Google Compute Engine VM local directory to a GCS bucket that I have already created. I am getting the following error each time I am running the script.



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



      The script:






       `// 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);
      });'





      Apparently the 'Upload' process needs the file pathnames as string. But that's what the Glob function is supposed to do. Still why is the error?



      Any help will be seriously appreciated!










      share|improve this question















      I am trying Node.js to upload multiple files from my Google Compute Engine VM local directory to a GCS bucket that I have already created. I am getting the following error each time I am running the script.



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



      The script:






       `// 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);
      });'





      Apparently the 'Upload' process needs the file pathnames as string. But that's what the Glob function is supposed to do. Still why is the error?



      Any help will be seriously appreciated!






       `// 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);
      });'





       `// 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);
      });'






      node.js api npm google-cloud-storage glob






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 at 8:11









      mihai

      22.3k63968




      22.3k63968










      asked Nov 7 at 7:47









      AliveToLearn

      115




      115
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          Your mistake is that you use allfiles as the return value for glob. This is not correct, the file names are available in the callback (since glob is async), not in the return value.



          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);
          });'
          });





          share|improve this answer





















          • Hi @mihai, many thanks for your answer. I believe it sets the context of the code right, but I am still getting the same error after implementing your suggestions...
            – AliveToLearn
            Nov 8 at 8:34










          • what does console.log(allfiles) print ?
            – mihai
            Nov 8 at 8:54











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185331%2fuploading-multiple-files-from-a-google-cloud-vm-to-google-cloud-storage-using-no%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          Your mistake is that you use allfiles as the return value for glob. This is not correct, the file names are available in the callback (since glob is async), not in the return value.



          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);
          });'
          });





          share|improve this answer





















          • Hi @mihai, many thanks for your answer. I believe it sets the context of the code right, but I am still getting the same error after implementing your suggestions...
            – AliveToLearn
            Nov 8 at 8:34










          • what does console.log(allfiles) print ?
            – mihai
            Nov 8 at 8:54















          up vote
          1
          down vote













          Your mistake is that you use allfiles as the return value for glob. This is not correct, the file names are available in the callback (since glob is async), not in the return value.



          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);
          });'
          });





          share|improve this answer





















          • Hi @mihai, many thanks for your answer. I believe it sets the context of the code right, but I am still getting the same error after implementing your suggestions...
            – AliveToLearn
            Nov 8 at 8:34










          • what does console.log(allfiles) print ?
            – mihai
            Nov 8 at 8:54













          up vote
          1
          down vote










          up vote
          1
          down vote









          Your mistake is that you use allfiles as the return value for glob. This is not correct, the file names are available in the callback (since glob is async), not in the return value.



          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);
          });'
          });





          share|improve this answer












          Your mistake is that you use allfiles as the return value for glob. This is not correct, the file names are available in the callback (since glob is async), not in the return value.



          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);
          });'
          });






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 7 at 8:15









          mihai

          22.3k63968




          22.3k63968












          • Hi @mihai, many thanks for your answer. I believe it sets the context of the code right, but I am still getting the same error after implementing your suggestions...
            – AliveToLearn
            Nov 8 at 8:34










          • what does console.log(allfiles) print ?
            – mihai
            Nov 8 at 8:54


















          • Hi @mihai, many thanks for your answer. I believe it sets the context of the code right, but I am still getting the same error after implementing your suggestions...
            – AliveToLearn
            Nov 8 at 8:34










          • what does console.log(allfiles) print ?
            – mihai
            Nov 8 at 8:54
















          Hi @mihai, many thanks for your answer. I believe it sets the context of the code right, but I am still getting the same error after implementing your suggestions...
          – AliveToLearn
          Nov 8 at 8:34




          Hi @mihai, many thanks for your answer. I believe it sets the context of the code right, but I am still getting the same error after implementing your suggestions...
          – AliveToLearn
          Nov 8 at 8:34












          what does console.log(allfiles) print ?
          – mihai
          Nov 8 at 8:54




          what does console.log(allfiles) print ?
          – mihai
          Nov 8 at 8:54


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185331%2fuploading-multiple-files-from-a-google-cloud-vm-to-google-cloud-storage-using-no%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()