0
0
GcpHow-ToBeginner · 4 min read

How to Use Google Cloud Storage with Node.js Easily

To use 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.
javascript
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.

javascript
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);
Output
File uploaded to your-bucket-name File downloaded as downloaded-test.txt
⚠️

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.

javascript
/* 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:

OperationCode Example
Create clientconst storage = new Storage();
Access bucketconst bucket = storage.bucket('bucket-name');
Upload fileawait bucket.upload('local-file.txt');
Download fileawait bucket.file('file.txt').download({destination: 'local.txt'});
Delete fileawait bucket.file('file.txt').delete();
List filesconst [files] = await bucket.getFiles();

Key Takeaways

Install and import @google-cloud/storage to interact with Google Cloud Storage in Node.js.
Set up authentication with a service account key and the GOOGLE_APPLICATION_CREDENTIALS environment variable.
Use the Storage client to access buckets and perform file operations like upload and download.
Ensure correct bucket names, file paths, and permissions to avoid common errors.
Refer to the quick reference for common commands to manage files in Cloud Storage.