Google Drive API (v106.0.0) problem uploading files

70 views Asked by At

I am using the googleapis library in Node.js (v14) to upload files to Google Drive. My code runs without errors, and I receive a 200 response code, but the problem is that the uploaded file turns out to be empty in the destination repository.

I am using version 106.0.0 of the googleapis library. Here is the code I am using:

 async uploadFile({ name, parents = [], mimeType, pathFile } = {}) {
        try {

            const KEYFILEPATH = path.join(__dirname, "cred.json");
            const SCOPES = ["https://www.googleapis.com/auth/drive"];

            const auth = new google.auth.GoogleAuth({
                keyFile: KEYFILEPATH,
                scopes: SCOPES,
            });
            const drive = google.drive({ version: "v3", auth });

            const dataFile = {
                requestBody: {
                    name,
                    mimeType,
                    parents
                },
                media: {
                    mimeType,
                    body: await fs.createReadStream(pathFile)
                }
            }
            const response = await drive.files.create(dataFile);
            return { success: true, data: response }
        } catch (error) {
            return { success: false, message: 'Error while creating new file' }
        }

Archive image in Google Drive

I tried uploading files to Google Drive using the googleapis library in Node.js (v14) with version 106.0.0 of the library. I expected the files to be successfully uploaded, but despite receiving a 200 response code, the uploaded files turned out to be empty in the Google Drive destination.

I attempted various solutions, including checking the file size, ensuring proper stream closure, and verifying file content before uploading. However, none of these measures resolved the issue, and the files remained empty on Google Drive.

Please share any insights or recommendations on how to troubleshoot and resolve this problem.

0

There are 0 answers