How to Manage GCS with @aws-sdk/client-s3
This guide walks you through configuring Google Cloud Storage (GCS) to work with the AWS SDK for JavaScript (@aws-sdk/client-s3) by enabling S3 interoperability, creating HMAC keys, and securely scoping access to a single bucket. By the end, you’ll be able to upload and access objects in GCS via S3-compatible code.
Table of Contents
- Prerequisites
- Step-by-Step Setup
- Verification & Validation
- Troubleshooting & Pitfalls
- FAQ
- Security Best Practices
- Useful Links
Prerequisites
- A Google Cloud account with billing enabled
- An existing GCS bucket (example:
gcs-snapix-test) - Access to Google Cloud Console
- [Ability to use Cloud Shell (built-in terminal)]
- [Node.js and the
@aws-sdk/client-s3package installed locally or on your server]
Note: Replace all credentials with placeholders like
YOUR_SECRET_VALUE_HERE.
Step-by-Step Setup
1. Open Cloud Shell in the Google Cloud Console
- Navigate to Google Cloud Console.
- Click the terminal icon in the top-right corner to activate Cloud Shell.
- Run:
gsutil ls
This verifies your bucket exists.
2. Make the GCS Bucket Public
Enable uniform bucket-level access and allow the public to view objects:
BUCKET_NAME=gcs-snapix-test
# Enable uniform access
gsutil uniformbucketlevelaccess set on gs://$BUCKET_NAME
# Grant read-only access to all users
gsutil iam ch allUsers:objectViewer gs://$BUCKET_NAME
Now all objects in the bucket can be accessed via URLs like:
https://storage.googleapis.com/gcs-snapix-test/test-117.webp
⚠️ Warning: This makes every object in this bucket publicly accessible. Don’t store sensitive data here.
3. Create a Bucket-Scoped Service Account
-
Go to IAM & Admin → IAM in the Google Cloud Console.
-
Remove any broad roles like Editor from this service account.
-
Go to Cloud Storage → Buckets → gcs-snapix-test → Permissions.
-
Click + Grant Access:
- Enter your service account email.
- Choose Storage Object Admin (or Storage Object Creator for upload-only access).
- Save changes.
Confirm permissions with:
gsutil iam get gs://gcs-snapix-test
4. Create HMAC Credentials
Generate HMAC keys for this service account:
SERVICE_ACCOUNT=[email protected]
gcloud storage hmac create $SERVICE_ACCOUNT
Example output:
metadata:
accessId: GOOG1E257VODVI4M2JWSOX5WX3QKMZBKE5YRM74Z4SUEWADE5QSBWCB5SIWT7
secret: YOUR_SECRET_VALUE_HERE
accessId→accessKeyIdin AWS SDK.secret→secretAccessKeyin AWS SDK.
⚠️ Important: The secret is only shown once. Save it securely.
5. Configure @aws-sdk/client-s3 for GCS
Install the AWS SDK for JavaScript (v3):
npm install @aws-sdk/client-s3
Then configure it to use GCS:
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import fs from "fs";
const s3 = new S3Client({
region: "auto",
endpoint: "https://storage.googleapis.com",
credentials: {
accessKeyId: "YOUR_ACCESS_KEY_VALUE_HERE", // from accessId
secretAccessKey: "YOUR_SECRET_VALUE_HERE", // from secret
},
forcePathStyle: true,
});
6. Upload a File via SDK
async function uploadFile() {
const fileContent = fs.readFileSync("test-117.webp");
const uploadParams = {
Bucket: "gcs-snapix-test",
Key: "test-117.webp",
Body: fileContent,
};
await s3.send(new PutObjectCommand(uploadParams));
console.log("Uploaded successfully!");
}
uploadFile();
Verification & Validation
-
Run:
gsutil ls gs://gcs-snapix-testYour file should be listed.
-
Open:
https://storage.googleapis.com/gcs-snapix-test/test-117.webpThe file should load in your browser.
Troubleshooting & Pitfalls
| Issue | Cause | Fix |
|---|---|---|
| Public bucket risk | Public access grants internet-wide visibility. | Use private buckets for sensitive data. |
| Lost secret | GCP only shows HMAC secret once. | Delete the key and recreate it. |
| Missing features | GCS S3 API isn’t fully AWS-compatible. | Use @google-cloud/storage for advanced features. |
FAQ
Q: Can I use the AWS SDK for all GCS features? A: No. GCS’s S3 interoperability supports basic operations like uploading, downloading, and listing objects. Advanced features may require Google’s native SDK.
Q: Why do I need HMAC keys? A: The AWS SDK uses signature-based authentication that requires access keys and secrets, provided by HMAC credentials in GCS.
Security Best Practices
-
Use least privilege
- Restrict the service account to only the buckets it needs.
- Use roles like
Storage Object CreatororStorage Object Viewerfor tighter control.
-
Rotate HMAC keys regularly
- Create a new key, update apps, and then delete the old one.
-
Never hardcode secrets
- Store credentials in environment variables or a secrets manager (e.g., GCP Secret Manager, AWS Secrets Manager).
-
Monitor access logs
- Enable Cloud Audit Logs to see who accessed what.
-
Avoid public buckets for sensitive data
- Use signed URLs or identity-aware proxies for controlled access.
-
Use separate service accounts per app
- Makes it easier to audit and revoke access if needed.
