Google Drive Api Upload File to Email Address
This tutorial explains how you can connect to Google Bulldoze or any other Google API from a service account without user authentication.
This stride past step guide volition guide you on how to upload files to Google Bulldoze with a service account using Node.js
. For this example, nosotros take a folder on your local hard bulldoze containing multiple files and we are required to upload these files to a specific binder in Google Drive.
1. Create a Google Deject Projection
Get to cloud.google.com
and create a new Google Cloud project. Requite your projection a name, change the project ID and click the Create
button.
ii. Enable Google APIs
Choose APIs & Services
from the left carte and click on Enable APIs and Services
to enable the various Google APIs. If you planning to upload files to Google Drive and so you lot will need to enable Bulldoze API. If you wish to utilize the Google Cloud Storage API then you will demand to enable Storage API.
3. Create a Service Account
In the APIs & Services
section, click on Credentials
and click on Create credentials
to create a service account.
3a. Describe the Service Account
Requite your service account a name and a service account ID. This is similar an email address and will be used to identify your service account in the futurity. Click Done
to terminate creating the service account.
3b. Create a Key File
In the Deject Console, go to IAM and Admin > Service accounts page. Click the email address of the service business relationship that you want to create a key for. Click the Keys
tab. Click the Add key
drib-down carte, then select Create new key
.
Select JSON
as the Fundamental type and then click Create. This will download a JSON file that will incorporate your private key. Do not commit this file to the Github repository.
For this example, we are looking to upload files from a local binder to a specific folder in Google Drive.
Get to your Google Bulldoze and create a new folder. Right-click the binder, choose Share and add the email address of the service account yous created in step iii as an editor to this folder.
Thus your Node.js awarding volition exist able to access this folder and upload files to information technology. The application will non have access to any other resources on your Google Drive.
Tip: If you are working with the Google Calendar API, you tin also share the Google calendar with the service account to have read and write admission to the calendar events.
5. Configure Node.js App
At present that the service business relationship is setup, we need to setup a Node.js app that volition upload files to Google Bulldoze. We will run this app from the command line but yous can also convert information technology to a web app with Google Cloud Run and Docker.
Replace service.json
with the name of your service account key file that you created in footstep 3b.
// service.js const { google } = crave ( 'googleapis' ) ; const path = require ( 'path' ) ; const getDriveService = ( ) => { const KEYFILEPATH = path. bring together (__dirname, 'service.json' ) ; const SCOPES = [ 'https://www.googleapis.com/auth/drive' ] ; const auth = new google.auth.GoogleAuth ( { keyFile : KEYFILEPATH , scopes : SCOPES , } ) ; const driveService = google. bulldoze ( { version : 'v3' , auth } ) ; return driveService; } ; module.exports = getDriveService;
5b. Write File Uploader
Supervene upon parent folder with the folder ID of the Google Bulldoze folder you want to upload to. After the file is uploaded, we'll motion the local file to trash as well.
// upload.js const fs = require ( 'fs' ) ; const getInvoiceFolder = require ( './folder' ) ; const drive = require ( './service' ) ; const uploadSingleFile = async ( fileName, filePath ) => { const folderId = 'DRIVE_FOLDER_ID' ; const { data : { id, name } = { } } = await drive.files. create ( { resources : { proper name : fileName, parents : [folderId] , } , media : { mimeType : 'awarding/pdf' , body : fs. createReadStream (filePath) , } , fields : 'id,proper name' , } ) ; console. log ( 'File Uploaded' , name, id) ; } ; const scanFolderForFiles = async ( folderPath ) => { const folder = await fs.promises. opendir (folderPath) ; for await ( const dirent of binder) { if (dirent. isFile ( ) && dirent.proper name. endsWith ( '.pdf' ) ) { await uploadSingleFile (dirent.name, path. bring together (folderPath, dirent.name) ) ; await fs.promises. rm (filePath) ; } } } ; module.exports = scanFolderForFiles;
6. Run File Uploader
Now that everything is setup, create an alphabetize.js
file and run the node index.js
command to upload files to Google Drive.
// index.js const scanFolderForFiles = require ( './scan' ) ; scanFolderForFiles ( 'local-binder' ) . so ( ( ) => { console. log ( '🔥 All files have been uploaded to Google Drive successfully!' ) ; } ) ;
You may consider using the
https://world wide web.googleapis.com/auth/drive.file
scope instead of the much broaderhttps://www.googleapis.com/auth/drive
scope. In that case, the initial parent binder should also be created with the aforementioned app else it volition not have permission to write to the folder.
Source: https://www.labnol.org/google-api-service-account-220404
0 Response to "Google Drive Api Upload File to Email Address"
Postar um comentário