Upload Files To Google Cloud Storage In Node.js
Last Updated :
13 Feb, 2024
+When there is some requirement for storing files, we need to store the data in some reliable place to access and retrieve it when needed. That's where Google Storage comes into place which provides a secure and reliable place to store our files with ease. Also, the use of Node.js makes it super easy to handle the file operations.
What Is Cloud Storage?
Cloud Storage is a storage service that can store all your files and can be accessed from anywhere in the world with the safety storing of your files. Similar to the cloud storage of Google, Amazon provides an S3 bucket and Azure provides Blob Storage for storing all the files like images, videos, documents, etc.
Why To Use Node.js For Uploading Files?
Node.js is a very popular runtime environment for server-side applications. With the help of different npm packages, it is very easy to handle all the file operations by using Node.js. The following are a few uses of Node.js for uploading files:
1. Efficient File Uploads
Node.js allows asynchronous operations and streaming of data which helps to efficiently upload large files without taking a large memory space. This approach helps to overcome the traditional approach of uploading large files taking a lot of memory space. By using nodejs we can use stream or multer packages for uploading files making it efficient to handle file operations.
2. File Access Management
Node.js provides a flexible environment for implementing access controls on files. You can easily manage who has access to uploaded files and define specific permissions, such as read, write, or delete. This level of control is crucial for ensuring the security and privacy of your stored data.
3. Integration With Your App
As we can use javascript in everywhere from front end to backend, it's very flexible for the developer to integrate file upload functionality very easily with the API integration on the front end. Also, handle all the logic of file validation to file upload inside the API. This simplifies the process and makes it efficient to use the feature.
4. Scalable Storage Solutions
Node.js is made on event-driven, non-blocking architecture which helps to handle large no of concurrent connections efficiently. As the storage requirements increase, Nodejs handle all concurrent connections without any limitations. Also, Nodejs provide a large no of libraries and modules for handling Google Cloud Platform and other platform storage services.
Step By Step Process - Upload Files In Google Cloud Storage(Node.js)
Step 1: Get the Credential File And Give Access To Storage Communication
- Go to Google Cloud Console.
- First, if you don't have any project active on the Google Cloud Platform, you must create a project with some name.
- Enable the IAM API.
- Go to the Service Account tab of IAM & Admin
- Click on Creating New Service Account

- Give the service account details with name and service account ID automatically created.
- Grant this service account access to the project. Search for Cloud Storage and give access to Full management of Cloud Storage for Firebase.
- You can ignore any other user's access to the service account and get things done. Here your service account is ready to use.
- Go to the Keys tab of the service account and click on Add key with JSON format then you will get the JSON key for your service account. It's ready to use. Just include it in the base dir of your project folder.
Step 2: JSON For Your Service Account
- It something look like this “service-account.json”. It contains JSON data like below:
{
"type": "service_account",
"project_id": "projectname",
"private_key_id": "randomalphanumericid",
"private_key": "-----BEGIN PRIVATE KEY-----\nsomethingbigalphanumerickey\n-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
"client_id": "somerandomnumber",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/[email protected]"
}
Step 3: Create A Bucket
- Navigate to again Google Cloud Console
- Go to the Cloud Storage tab of GCP with the bucket list.
- Inside Buckets click on Create New Bucket. Give the bucket a name and continue.

- Choose your region to store the bucket data and also choose the storage class for the data.
- Choose how to control access to the objects where Uncheck the public access prevention for the project and provide access for fine-grained.
- Choose how to protect object data and then continue to create for the bucket by clicking create button.
Step 4: Give Access To The The Service Account
- Go inside the permission tab of the bucket.
- Inside the permission tab, Click on Grant Access.

- From the JSON file take the client_email and paste it on add principals, also choose the role as Storage Object Admin to create, read and write to the bucket.
- Finally, you are ready to upload files from your code.
Step 4: Nodejs Setup
- Install the “@google-cloud/storage” npm package. Require it and use it on your code.
const { Storage } = require("@google-cloud/storage");
const storage = new Storage({
projectId: "projectname",
keyFilename: "service-account.json",
});
Step 5: GCP Storage Setup And Related Code
Create a bucket in cloud storage before calling the upload() function. Upload to the Google Cloud storage using the simple “.upload()” function having the proper file path and destination path mentioned.
const uploadToFirebaseStorage = async (filepath, fileName) => {
try {
const gcs = storage.bucket("bucket_name"); // Removed "gs://" from the bucket name
const storagepath = `storage_folder/${fileName}`;
const result = await gcs.upload(filepath, {
destination: storagepath,
predefinedAcl: 'publicRead', // Set the file to be publicly readable
metadata: {
contentType: "application/plain", // Adjust the content type as needed
}
});
return result[0].metadata.mediaLink;
} catch (error) {
console.log(error);
throw new Error(error.message);
}
}
Step 6: Upload The File
- Call the uploadtoStorage() function by passing the file path and name of the file.
let result= await uploadToFirebaseStorage(`${uploadfilepath}`, 'filename.txt');
console.log(result);
You will get the link which looks like below providing access to the file.
"https://storage.googleapis.com/download/storage/v1/b/bucket_name/o/storage_folder%2Ffilename.txt?generation=1685355700228424&alt=media"
Conclusion
By using all the above steps, you can successfully upload any files including images, videos, and documents to cloud storage. Overall it provides an efficient way in terms of flexibility, reliability, and cost-effectiveness. Node.js helps developers upload files to cloud storage efficiently and securely thanks to dedicated libraries like @google-cloud/storage, aws-sdk, and @azure/storage-blob.
Similar Reads
Integrating Google Cloud Storage with WordPress: Storing Media Files Now, before learning how to integrate Google Cloud Storage with WordPress, let's try and understand why you might want to do that. To answer our question, let's imagine that you have built an awesome cat website that allows users to share pics of their cats. The website is getting popular, I mean wh
6 min read
Google Cloud Platform - Ways of Uploading Data to GCS Before you can harness the power of the cloud, to serve your content, you have to get your data into it. In this article, we will look into all the different ways you can upload data. We all know that Google Cloud Storage (GCS) can serve your binary assets to users worldwide at high speed and low pr
3 min read
Cloud Storage in Google Cloud Platform (GCP) Google Cloud Storage is a secure, scalable, and high-performance storage solution that lets businesses store, manage, and retrieve data effortlessly. Itâs designed for big data analytics, media storage, backups, and disaster recovery, making it a go-to option for enterprises looking for cost-effecti
8 min read
Google Cloud Platform - Cloud Storage Google Cloud Storage is unified object storage. In reality, the GCS is the place where you can store and serve static binary assets either for your app to use or directly to your users. But as straightforward, as it sounds, there is a lot going under the hood. Google Cloud Storage The GCP has Bucket
2 min read
Google Cloud Platform - Filestore GCP's Filestore is a managed file storage service for applications that require a file system interface and a shared file system for data. It gives the user a native experience for standing up managed network detached storage with their VM in the compute engine and Google Kubernetes Engine. It offer
3 min read
Google Cloud Platform - Ways of Serving files from GCS When it comes to the cloud, there is more than one way to serve a file. In, this article, we will walk through all the different ways to serve a file from Google Cloud Storage. It is kind of like having a yard sale, but all the stuff stays in your yard or you decide where it goes. You probably alrea
3 min read
Hosting a Static Website on Google Cloud Storage: Step-by-Step Tutorial You have finally built the awesome portfolio website that all recruiters want. But now the question is, where to host it? Well, there are quite a few options for it, and object storage solutions like Google Cloud Storage and AWS S3 are some of the popular ones. You can host your static website in ju
6 min read
How To Create Cloud Storage Buckets In GCP ? It is becoming very important to protect and secure our data since more threats and attacks can be seen nowadays. Cyberattacks and data breaches are very common, and it is very difficult to track them. Some cases of attacks also go unreported and undetected. Before facing such attacks and threats on
8 min read
How to make Storage Partition on Cloud As we learn about the AWS EC2 instance and there storage if we want to make the partition on that storage as we make in our pc like C Drive, E drive, D drive ... etc by using the command line then we have to perform the following steps that are given below :Â Step-1:Â Make the EC2 instance on AWS by g
3 min read
Google Persistent Disk & Google Filestore Services Before we begin learning about Google Persistent Disk & Google Filestore Services, we will talk about different types of Storage Options provided by the Google Cloud Platform. Then, we will study Google Persistent Disk & Google Filestore Services in detail. We will study the features, types,
8 min read