How to Use Google Cloud Storage with Node.js Easily
Google Cloud Storage with Node.js, install the @google-cloud/storage package, create a storage client, and use it to upload or download files from buckets. This lets your Node.js app store and retrieve files in the cloud easily.Syntax
First, install the Google Cloud Storage client library for Node.js. Then, create a Storage client object. Use this client to access buckets and files with methods like bucket(), upload(), and file().
Here is the basic syntax:
const {Storage} = require('@google-cloud/storage');- imports the library.const storage = new Storage();- creates a client.storage.bucket('bucket-name')- accesses a bucket.bucket.upload('local-file-path')- uploads a file.bucket.file('file-name').download()- downloads a file.
const {Storage} = require('@google-cloud/storage'); async function main() { // Creates a client const storage = new Storage(); // Reference a bucket const bucket = storage.bucket('your-bucket-name'); // Upload a file await bucket.upload('local-file-path'); // Download a file const file = bucket.file('file-name'); await file.download({destination: 'local-destination-path'}); } main().catch(console.error);
Example
This example shows how to upload a file named test.txt from your local folder to a Google Cloud Storage bucket, then download it back with a new name downloaded-test.txt.
const {Storage} = require('@google-cloud/storage'); async function main() { // Creates a client const storage = new Storage(); // Name of your bucket const bucketName = 'your-bucket-name'; const bucket = storage.bucket(bucketName); // Upload local file await bucket.upload('test.txt'); console.log('File uploaded to', bucketName); // Download the file const file = bucket.file('test.txt'); await file.download({destination: 'downloaded-test.txt'}); console.log('File downloaded as downloaded-test.txt'); } main().catch(console.error);
Common Pitfalls
1. Missing authentication: You must set up Google Cloud credentials properly, usually by setting the GOOGLE_APPLICATION_CREDENTIALS environment variable to your service account key file.
2. Wrong bucket name: Make sure the bucket exists and the name is correct.
3. File path errors: Use correct local file paths and ensure files exist before uploading.
4. Permissions: Your service account needs storage permissions like Storage Object Admin to upload and download files.
/* Wrong way: No authentication setup */ const {Storage} = require('@google-cloud/storage'); async function main() { const storage = new Storage(); // This will fail if credentials are missing or invalid await storage.bucket('wrong-bucket').upload('nofile.txt'); } main().catch(console.error); /* Right way: Set GOOGLE_APPLICATION_CREDENTIALS env variable and correct bucket */ // export GOOGLE_APPLICATION_CREDENTIALS="/path/to/key.json" // Then run the Node.js script with correct bucket and file paths
Quick Reference
Here is a quick cheat sheet for common Google Cloud Storage operations in Node.js:
| Operation | Code Example |
|---|---|
| Create client | const storage = new Storage(); |
| Access bucket | const bucket = storage.bucket('bucket-name'); |
| Upload file | await bucket.upload('local-file.txt'); |
| Download file | await bucket.file('file.txt').download({destination: 'local.txt'}); |
| Delete file | await bucket.file('file.txt').delete(); |
| List files | const [files] = await bucket.getFiles(); |