0
  • About Us
  • Disclaimer
  • DMCA
  • Contact Us
  • Privacy Policy

Archives

  • April 2022
  • March 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021

Categories

  • Android
  • Best Inormation
  • Computer
  • Diwali
  • Entertenment
  • Festival
  • goverment
  • groups
  • Health
  • Hosting
  • Movies
  • News
  • Questions
  • Questions Hub
  • Quotes
  • Review
  • social
  • SPORTS
  • technology
  • Tools
  • wishes
Best Information
0
Best Information
0

Best Information

Movies ,entertenment, Review, Festival, News, Quotes, Health, Goverment News, Groups All About Information Blog

  • About Us
  • Disclaimer
  • DMCA
  • Contact Us
  • Privacy Policy
0
Trending Now

8 best keyword research tools For Seo, uncover our keyword

8 months Ago
iOS 15

Apple Studio Display iOS 15.4 . runs the full version of

2 months Ago
Apple Peak Performance Event Live Wallpaper

Apple Peak Performance Event 2022 Live Wallpaper

3 months Ago

Semrush Review – “10 ways to find untapped keywords”

8 months Ago

Best Information

  • About Us
  • Disclaimer
  • DMCA
  • Contact Us
  • Privacy Policy
Trending Now

8 best keyword research tools For Seo, uncover our keyword

8 months Ago
iOS 15

Apple Studio Display iOS 15.4 . runs the full version of

2 months Ago
Apple Peak Performance Event Live Wallpaper

Apple Peak Performance Event 2022 Live Wallpaper

3 months Ago

Semrush Review – “10 ways to find untapped keywords”

8 months Ago
0
Best Information > technology > How to Upload Files to Google Drive with a Service Account

How to Upload Files to Google Drive with a Service Account

admin
Posted by admin
Create a Google Cloud Project
Contents
1. Create a Google Cloud Project
2. Enable Google API
3. Create a Service Account
3a. describe service account
3b. create a key file
4. Share a Drive Folder
5. Configure Node.js App
5a. Create an Authorized OAuth2 Client
5b. write file uploader
6. Run File Uploader

This tutorial explains how you can connect to Google Drive or any other Google API from a service account without user authentication.

This step-by-step guide will guide you on how to upload files to Google Drive with a service account Node.js, For this example, we have a folder on your local hard drive that contains many files and we need to upload these files to a specific folder in Google Drive.

1. Create a Google Cloud Project

go for cloud.google.com And create a new Google Cloud Project. Give your project a name, change the project id and click Create switch.


Create a Google Cloud Project

2. Enable Google API

choosing APIs & Services Click on More from the left menu Enable APIs and Services To enable various Google APIs. If you plan to upload files to Google Drive then you need to enable Drive API. If you want to use Google Cloud Storage API you need to enable Storage API.


google drive api

3. Create a Service Account

in APIs & Services section, click on Credentials and click Create credentials To create a service account.


create service account

3a. describe service account

Give your service account a name and a service account ID. This is like an email address and will be used to identify your service account in the future. Click Done To finish creating a service account.


service account statement

3b. create a key file

In the Cloud Console, go to the IAM & Admin > Service Accounts page. Click the email address of the service account for which you want to create a key. press Keys tab. press Add key drop-down menu, then select Create new key,

choosing JSON as the key type and then click Create. This will download a JSON file containing your private key. Do not commit this file to Github repository.


service account key

4. Share a Drive Folder

For this example, we are looking to upload files from a local folder to a specific folder in Google Drive.

Go to your Google Drive and create a new folder. Right-click the folder, choose Share, and add the email address of the service account you created in Step 3 as an editor to this folder.

This way your Node.js application will be able to access this folder and upload files to it. The application will not have access to any other resources on your Google Drive.


share google drive folders

5. Configure Node.js App

Now that the service account is setup, we need to setup a Node.js app that will upload files to Google Drive. We will run this app from the command line but you can also convert it into a web app with Google Cloud Run and Docker.

5a. Create an Authorized OAuth2 Client

To change service.json Your service account key with the name of the file that you created in step 3b.


const { google } = require('googleapis');
const path = require('path');

const getDriveService = () => {
  const KEYFILEPATH = path.join(__dirname, 'service.json');
  const SCOPES = ['https://www.googleapis.com/auth/drive'];

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

module.exports = getDriveService;

5b. write file uploader

Replace the parent folder with the folder ID of the Google Drive folder you want to upload to. After the file is uploaded, we will also move the local file to the Trash.


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({
    resource: {
      name: fileName,
      parents: [folderId],
    },
    media: {
      mimeType: 'application/pdf',
      body: fs.createReadStream(filePath),
    },
    fields: 'id,name',
  });
  console.log('File Uploaded', name, id);
};

const scanFolderForFiles = async (folderPath) => {
  const folder = await fs.promises.opendir(folderPath);
  for await (const dirent of folder) {
    if (dirent.isFile() && dirent.name.endsWith('.pdf')) {
      await uploadSingleFile(dirent.name, path.join(folderPath, dirent.name));
      await fs.promises.rm(filePath);
    }
  }
};

module.exports = scanFolderForFiles;

6. Run File Uploader

Now that everything is setup, create a index.js file and run node index.js Command to upload files to Google Drive.


const scanFolderForFiles = require('./scan');

scanFolderForFiles('local-folder').then(() => {
  console.log('🔥 All files have been uploaded to Google Drive successfully!');
});

you can consider using https://www.googleapis.com/auth/drive.file scope instead of too wide https://www.googleapis.com/auth/drive scope. In that case, the initial parent folder must also be created with the same app otherwise it will not have permission to write to the folder.

0 Shares
Share on Facebook Share on Twitter Share on Pinterest Share on WhatsApp Share on WhatsApp Share on Email
admin April 9, 2022

Leave a Review

Cancel reply

Your email address will not be published. Required fields are marked *

Please select a rating

5 × one =

CATEGORIES

  • Android
  • Best Inormation
  • Computer
  • Diwali
  • Entertenment
  • Festival
  • goverment
  • groups
  • Health
  • Hosting
  • Movies
  • News
  • Questions
  • Questions Hub
  • Quotes
  • Review
  • social
  • SPORTS
  • technology
  • Tools
  • wishes

You Might Also Enjoy

Create a Google Cloud Project
technology

How to send email with Gmail API and Node.js

Posted by admin admin 2 months Ago
READ MORE
ifixit google pixel
Review

Google iFixit. Announced DIY Repair for Pixel Smartphones in partnership with

Posted by admin admin 2 months Ago
READ MORE
Google Maps is afraid of the green
Review

Google Maps for iOS Gets More Details, Toll Estimates, and Standalone Apple Watch Support

Posted by admin admin 2 months Ago
READ MORE
apple business essentials
Review

Apple’s ‘Business Essentials’ service extends to all small businesses

Posted by admin admin 2 months Ago
READ MORE
Load More
  • Jobs
  • Insurance
  • movies
  • India News
  • Spain News
  • English News

© Copyright Best Information - | News & Magazine

Our website uses cookies to improve your experience. Learn more about: Cookie Policy

Accept