February 15, 2023
O. Wolfson
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:
You will also need to have a basic understanding of Node.js and Express.
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:
bashnpm init -y
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:
bashnpm install express @google-cloud/storage multer dotenv cors
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:
bashGOOGLE_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=8080
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.
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:
javascript// Import required modules
const express = require("express");
const multer = require("multer");
const { Storage } = require("@google-cloud/storage");
require("dotenv").config();
const cors = require("cors");
//CORS is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served. In other words, it allows you to make requests to a server from a different domain. This is useful for web applications that are hosted on different domains, but need to communicate with each other. For example, if you have a web app hosted on a domain like example.com, and you want to make requests to a server hosted on a different domain like api.example.com, you will need to enable CORS for the API server.
// Create a new Express app
const app = express();
// Enable CORS (Cross-Origin Resource Sharing) for all routes
app.use(cors());
// Create a new Storage instance using the credentials from the environment variables
const storage = new Storage({
projectId: process.env.GOOGLE_CLOUD_PROJECT_ID,
credentials: {
type: "service_account",
project_id: process.env.GOOGLE_CLOUD_PROJECT_ID,
private_key_id: process.env.GOOGLE_CLOUD_PRIVATE_KEY_ID,
private_key: process.env.GOOGLE_CLOUD_PRIVATE_KEY,
client_email: process..,
: process..,
: ,
: ,
: ,
: process..,
},
});
app.(, {
res.();
});
bucket = storage.(process..);
upload = ({
: multer.(),
: {
: * * ,
},
});
app.(, (req, res, next) => {
{
filename = req..;
(!filename) {
res.().();
}
file = bucket.(filename);
[exists] = file.();
res.().({ exists });
} (err) {
(err);
}
});
app.(, upload.(), (req, res, next) => {
{
(!req.) {
res.().();
}
blob = bucket.(req..);
blobStream = blob.({
: ,
});
blobStream.(, {
(err);
});
blobStream.(, () => {
publicUrl = ;
res.().();
});
blobStream.(req..);
} (err) {
(err);
}
});
app.(, (req, res, next) => {
{
filename = req..;
(!filename) {
res.().();
}
file = bucket.(filename);
file.();
res.().({ : });
} (err) {
(err);
}
});
= process.. || ;
app.(, {
.();
});
To launch the app, run the following command:
bashnode app.js