Last active
November 6, 2024 17:53
-
-
Save trulymittal/fd9c4bfd1f22fb9c62847a351dcbf0a5 to your computer and use it in GitHub Desktop.
Gist to demonstrate Google Drive API using NodeJs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Google Drive API: | |
Demonstration to: | |
1. upload | |
2. delete | |
3. create public URL of a file. | |
required npm package: googleapis | |
*/ | |
const { google } = require('googleapis'); | |
const path = require('path'); | |
const fs = require('fs'); | |
const CLIENT_ID = 'YOUR CLIENT ID'; | |
const CLIENT_SECRET = 'YOUR CLIENT SECRET'; | |
const REDIRECT_URI = 'https://developers.google.com/oauthplayground'; | |
const REFRESH_TOKEN = 'YOUR REFRESH TOKEN GENERATED FROM oauthplayground'; | |
const oauth2Client = new google.auth.OAuth2( | |
CLIENT_ID, | |
CLIENT_SECRET, | |
REDIRECT_URI | |
); | |
oauth2Client.setCredentials({ refresh_token: REFRESH_TOKEN }); | |
const drive = google.drive({ | |
version: 'v3', | |
auth: oauth2Client, | |
}); | |
/* | |
filepath which needs to be uploaded | |
Note: Assumes example.jpg file is in root directory, | |
though this can be any filePath | |
*/ | |
const filePath = path.join(__dirname, 'example.jpg'); | |
async function uploadFile() { | |
try { | |
const response = await drive.files.create({ | |
requestBody: { | |
name: 'example.jpg', //This can be name of your choice | |
mimeType: 'image/jpg', | |
}, | |
media: { | |
mimeType: 'image/jpg', | |
body: fs.createReadStream(filePath), | |
}, | |
}); | |
console.log(response.data); | |
} catch (error) { | |
console.log(error.message); | |
} | |
} | |
// uploadFile(); | |
async function deleteFile() { | |
try { | |
const response = await drive.files.delete({ | |
fileId: 'YOUR FILE ID', | |
}); | |
console.log(response.data, response.status); | |
} catch (error) { | |
console.log(error.message); | |
} | |
} | |
// deleteFile(); | |
async function generatePublicUrl() { | |
try { | |
const fileId = 'YOUR FILE ID'; | |
await drive.permissions.create({ | |
fileId: fileId, | |
requestBody: { | |
role: 'reader', | |
type: 'anyone', | |
}, | |
}); | |
/* | |
webViewLink: View the file in browser | |
webContentLink: Direct download link | |
*/ | |
const result = await drive.files.get({ | |
fileId: fileId, | |
fields: 'webViewLink, webContentLink', | |
}); | |
console.log(result.data); | |
} catch (error) { | |
console.log(error.message); | |
} | |
} | |
// generatePublicUrl(); |
mimeType: 'image/jpg', i want to upload all type of file..how i will write syntax. i tried this mimeType: / work but preview can't work. thanks lot.
bro use ,
` async function uploadFile() {
try {
const response = await drive.files.create({
requestBody: {
name: 'Jai Shree Ram- PDF', //This can be name of your choice
// mimeType: 'image/jpg',
mimeType: 'application/octet-stream'
},
media: {
// mimeType: 'image/jpg',
body: fs.createReadStream(filePath),
},
});
console.log("Uploaded successfully",response.data);
} catch (error) {
console.log(error.message);
}
}
uploadFile(); `
Thanks for it 👍 but I have a question that instead of fs.readStream , can we use buffer directly from multer file in incoming request ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
mimeType: 'image/jpg',
i want to upload all type of file..how i will write syntax.
i tried this mimeType: / work but preview can't work.
thanks lot.