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!
node.js api npm google-cloud-storage glob
add a comment |
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!
node.js api npm google-cloud-storage glob
add a comment |
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!
node.js api npm google-cloud-storage glob
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
node.js api npm google-cloud-storage glob
edited Nov 7 at 8:11
mihai
22.3k63968
22.3k63968
asked Nov 7 at 7:47
AliveToLearn
115
115
add a comment |
add a comment |
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);
});'
});
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 doesconsole.log(allfiles)
print ?
– mihai
Nov 8 at 8:54
add a comment |
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);
});'
});
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 doesconsole.log(allfiles)
print ?
– mihai
Nov 8 at 8:54
add a comment |
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);
});'
});
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 doesconsole.log(allfiles)
print ?
– mihai
Nov 8 at 8:54
add a comment |
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);
});'
});
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);
});'
});
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 doesconsole.log(allfiles)
print ?
– mihai
Nov 8 at 8:54
add a comment |
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 doesconsole.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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password