Google Cloud Platform Uploader
This app is a simple web application that provides file upload functionality to a Google Cloud Storage bucket. It uses the Express framework to define API endpoints for uploading, deleting, and checking files in the bucket. Multer is used for processing file uploads, and the @google-cloud/storage package is used for communicating with the Google Cloud Storage API. The app uses the cors package to enable CORS (Cross-Origin Resource Sharing) for all routes.
Before we start building the app, make sure that you have the following installed:
- Node.js and NPM (Node Package Manager) installed on your system
- A Google Cloud Storage bucket set up
You will also need to have a basic understanding of Node.js and Express.
Setting up the project
To get started, create a new directory for your project and navigate to it in the terminal. Then run the following command to create a new Node.js project:
npm init -ybash
This will create a new package.json file in the project directory.
Next, install the following packages:
You can install these packages using NPM with the following command:
npm install express @google-cloud/storage multer dotenv corsbash
We will be using environment variables to store sensitive information such as our Google Cloud Storage credentials. Create a new file in your project directory called .env and add the following variables:
GOOGLE_CLOUD_PROJECT_ID=your-project-id GOOGLE_CLOUD_PRIVATE_KEY_ID=your-private-key-id GOOGLE_CLOUD_PRIVATE_KEY=your-private-key GOOGLE_CLIENT_EMAIL=your-client-email GOOGLE_CLIENT_ID=your-client-id GOOGLE_CLIENT_X509_CERT_URL=your-client-cert-url GOOGLE_CLOUD_STORAGE_BUCKET=your-bucket-name PORT=8080bash
Replace the placeholders with your own values. You can find your project ID, private key ID, private key, client email, and client ID in the JSON file that you downloaded when you created your Google Cloud Storage service account. The client X.509 certificate URL can be found in the service account details. The bucket name is the name of the bucket that you created in Google Cloud Storage. Finally, the port number is the port that the app will listen on.
Creating the app
Now that you have the necessary packages installed, create a new file named app.js in your project directory, and paste the following code into it:
To launch the app, run the following command:
Thanks for reading. If you enjoyed this post, I invite you to explore more of my site. I write about web development, programming, and other fun stuff.