https://github.com/vbrazo/gcp-cloud-storage-upload-example
A Node.js GCP Cloud Storage upload example for Alexandre
https://github.com/vbrazo/gcp-cloud-storage-upload-example
Last synced: 11 months ago
JSON representation
A Node.js GCP Cloud Storage upload example for Alexandre
- Host: GitHub
- URL: https://github.com/vbrazo/gcp-cloud-storage-upload-example
- Owner: vbrazo
- Created: 2025-07-11T19:12:01.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-07-11T19:24:18.000Z (11 months ago)
- Last Synced: 2025-07-11T21:20:32.558Z (11 months ago)
- Language: JavaScript
- Homepage:
- Size: 1.7 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# GCP Cloud Storage Demo (Node.js)
This project demonstrates how to upload files to a Google Cloud Storage bucket using Node.js and the official `@google-cloud/storage` SDK.
## π Features
- Upload a local file to a GCP bucket
- Simple setup using service account authentication
- Easy to extend for downloading or listing files
---
## π Requirements
- Node.js (v14+ recommended)
- A Google Cloud project with **Cloud Storage API** enabled
- A **service account key** with `Storage Object Admin` permissions
---
## π¦ Installation
1. **Clone the repository:**
```bash
git clone https://github.com/your-username/gcp-storage-demo.git
cd gcp-storage-demo
```
2. **Install dependencies:**
```bash
npm install
```
## π Authentication
This project uses a service account to authenticate with GCP.
- Go to IAM & Admin > Service Accounts in the Google Cloud Console.
- Create a service account (if you donβt already have one).
- Grant it the Storage Admin or Storage Object Admin role.
- Create and download a JSON key.
- Set the environment variable:
```bash
export GOOGLE_APPLICATION_CREDENTIALS="/absolute/path/to/your-key.json"
```
## π Prepare a Test File
```bash
echo "Hello from GCP Storage!" > test-upload.txt
```
## π Run the Upload Script
Make sure to edit index.js and set your bucket name:
```js
const BUCKET_NAME = 'your-bucket-name'; // <-- replace this
```
Then run:
```bash
node index.js
```
If successful, you should see:
```
β
File test-upload.txt uploaded to uploaded-test.txt
```
## π Project Structure
```
.
βββ index.js # Main upload script
βββ upload.test.js # Mocha test for uploading and cleanup
βββ test-upload.txt # Sample file to upload
βββ package.json
βββ README.md
```
## π§ͺ Run Tests
This project includes a test that uploads a file, checks if it exists, and deletes it from the bucket.
- Update upload.test.js with your bucket name.
- Run the test:
```bash
npm install --save-dev mocha
npm test
```
Or manually with
```bash
npx mocha upload.test.js
```
## π Notes
- Make sure your bucket name is globally unique
- If using this in production, do not commit your JSON key
- You can extend this to include file download, list, and delete operations