node-google-drive icon indicating copy to clipboard operation
node-google-drive copied to clipboard

can you share a working example of create

Open shmuel-web opened this issue 5 years ago • 5 comments

Hi

thanks for the library google's original API is kind of low level

for some reason, I am having trouble uploading files create method is not on the googleDriveInstane and there is no example in the readme for using it

thanks

shmuel-web avatar May 05 '20 11:05 shmuel-web

Hello,

I have the same problem. I figured out that there isn't any create function into the library. Instead there is function writeFile and writePDFFile that I am using because I want to upload a PDF file.

But it has a bug. It can't handle the file path

(node:51) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be one of type string, Buffer, or URL. Received type object
    at Object.open (fs.js:409:3)
    at P (/home/node/app/node_modules/pify/index.js:49:6)
    at new Promise (<anonymous>)
    at Object.open (/home/node/app/node_modules/pify/index.js:11:9)
    at module.exports (/home/node/app/node_modules/read-chunk/index.js:12:13)
    **at NodeGoogleDrive.writeFile (/home/node/app/node_modules/node-google-drive/index.js:595:10)**
    at ClientService.ExampleOperations (/home/node/app/src/services/client.ts:271:52)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:51) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:51) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I think it should be something related to this https://stackoverflow.com/a/57371234

ediaz-inteliti avatar May 13 '20 17:05 ediaz-inteliti

The problem is that the version in NPM repository is outdated.

The author looks like isn`t avaiable. I forked the repository and published a new one project to NPM

https://www.npmjs.com/package/google-drive-connect

npm i google-drive-connect

    const NodeGoogleDrive = require('google-drive-connect');

    const YOUR_ROOT_FOLDER = '******',
	PATH_TO_CREDENTIALS = path.resolve(`credentials.json`);

    const creds_service_user = require(PATH_TO_CREDENTIALS);

	const googleDriveInstance = new NodeGoogleDrive({
		ROOT_FOLDER: YOUR_ROOT_FOLDER
	});

	let gdrive = await googleDriveInstance.useServiceAccountAuth(
		creds_service_user
	);

    const fileName = `${__dirname}/tmp/file.pdf`;

    let uploadResponse = await googleDriveInstance.create({

        source:fs.createReadStream(fileName),
        name: 'MyDocument.pdf',
        parentFolder: YOUR_ROOT_FOLDER,
        mimeType: 'application/pdf'
      });

This code worked for me.

ediaz-inteliti avatar May 13 '20 19:05 ediaz-inteliti

@ediaz-inteliti . thank you very much. I was also having problem with the create method.

aubierahul avatar Aug 02 '22 10:08 aubierahul

@ediaz-inteliti However there is one issue with your version, whenever I pass ROOT_FOLDER = null, it throws an error. While the old version (original node-google-drive) works perfectly when I pass ROOT_FOLDER = null. Can you please look into this issue. It will be really helpful

aubierahul avatar Aug 02 '22 12:08 aubierahul

The problem is that the version in NPM repository is outdated.

The author looks like isn`t avaiable. I forked the repository and published a new one project to NPM

https://www.npmjs.com/package/google-drive-connect

npm i google-drive-connect

    const NodeGoogleDrive = require('google-drive-connect');

    const YOUR_ROOT_FOLDER = '******',
	PATH_TO_CREDENTIALS = path.resolve(`credentials.json`);

    const creds_service_user = require(PATH_TO_CREDENTIALS);

	const googleDriveInstance = new NodeGoogleDrive({
		ROOT_FOLDER: YOUR_ROOT_FOLDER
	});

	let gdrive = await googleDriveInstance.useServiceAccountAuth(
		creds_service_user
	);

    const fileName = `${__dirname}/tmp/file.pdf`;

    let uploadResponse = await googleDriveInstance.create({

        source:fs.createReadStream(fileName),
        name: 'MyDocument.pdf',
        parentFolder: YOUR_ROOT_FOLDER,
        mimeType: 'application/pdf'
      });

This code worked for me.

Hey, I tried using your package but it also gave another error

\node_modules\node-google-drive\index.js:605
      return _this.service.files.createAsync({
                                 ^

TypeError: Cannot read properties of null (reading 'createAsync')

LMeiraba avatar Feb 17 '23 15:02 LMeiraba