File Upload With Encryption

Uploading Files To Drive in NodeJs With Encryption

Dikshita Shirodkar
TheLeanProgrammer
5 min readJul 7, 2021

--

This article is an extension to my previous article generate a front-end performance report, where we generated performance. In this article, we are going to upload the reports to Google Drive.

Why upload files to Google Drive?

So as testers, we always face challenges when it comes to reporting. Reporting can be in any form, but today we are going to talk about reports generated when we run our e2e automation suites or reports which are generated when we run performance tests.

Where do we store them?

If running the e2e tests via a CI/CD pipeline, let’s take an example of GitActions. GitAction spins a server when a workflow is triggered and it does the necessary jobs, provides us a report which is mostly preferred to in html format. But where to store them or how to access those reports?

There are various options available, like posting into a database or uploading the reports to google drive.

Yes, you heard that correctly!!. Google drive provides API which allows you to upload files in any format onto GoogleDrive.

So in today’s blog, we are going to learn how to upload files to google drive.

Before we start, here are some prerequisite:

Enable the google drive API, and generate a client credentials file and download the file on your local machine.

  1. Enable the google API
Google console API

2. Generate the credentials File, that will have a clientID & clientSecret.

Credentials File

Note: This file should not be exposed to others for security reasons.

Since the leak of such files is risky, let’s encrypt these files using a json encryption package.

Install the package using the below command:

3. Create a cryptography.js that will help us to encrypt and decrypt any file which is in json format. Also, the below code will automatically identify whether the file needs to be encrypted or decrypted based on the extension of the file.

Note: Encrypted files will always have .enc extension in the filename.

  • Here’s a command to encrypt or decrypt the file

Example:

The above command will encrypt or decrypt the file in a .enc extension

4. Now, let's install the Google API package, simply by using the command:

5. Here’s the script gdrive-auth-util.js which exports an authorize function to access google drive.

6. Finally create a file uploadToDrive.js which uploads the html Report to google drive. Also, you can pass the parentID (path of the specific folder) you want to upload in.

A token is generated the very first time the authentication is made with Google drive (by executing the uploadToDrive.js). Copy the token and paste it into the token.json file. Encrypt the token.json file token.json.enc for more security.

Note: modifying the scopes or changing the permission to access your google drive will alter the user’s access and refresh tokens, which is created automatically when the authorization flow completes for the first time.

Run the command to upload the file to google drive:

There you go... Files will be uploaded to the appropriate folder…

Thank you for reading.

Don’t forget to follow The Lean Programmer Publication for more such articles, and subscribe to our newsletter tinyletter.com/TheLeanProgrammer

--

--