CodexBloom - Programming Q&A Platform

Getting 'Permission Denied' scenarios When Accessing Google Cloud Storage with Service Account

👀 Views: 68 đŸ’Ŧ Answers: 1 📅 Created: 2025-05-31
google-cloud node.js storage JavaScript

I'm optimizing some code but I'm sure I'm missing something obvious here, but I'm trying to implement I'm working on a personal project and I'm currently working on a Node.js application that needs to upload files to Google Cloud Storage. I've set up a service account with the necessary permissions, but I'm running into a 'Permission Denied' behavior when attempting to upload a file. Here's the relevant code snippet that I'm using: ```javascript const { Storage } = require('@google-cloud/storage'); const storage = new Storage({ keyFilename: 'path/to/service-account.json' }); async function uploadFile(bucketName, filename) { await storage.bucket(bucketName).upload(filename, { gzip: true, metadata: { cacheControl: 'public, max-age=31536000', }, }); console.log(`${filename} uploaded to ${bucketName}.`); } uploadFile('my-bucket', 'local/path/to/file.txt'); ``` The service account JSON file is correctly configured, and I made sure to grant the 'Storage Object Creator' role to the service account in the IAM settings of the Google Cloud Console. However, I'm still getting the following behavior: ``` behavior: Permission denied at ApiError.parseHttpResponse (...) at ... ``` I've verified that the bucket name is correct and that the service account has permission to write to that bucket. I've even tried setting the bucket's permissions directly to allow public access, but the behavior continues. Is there something I'm missing in the configuration or best practices for using service accounts with Google Cloud Storage? Any suggestions would be greatly appreciated! This is part of a larger service I'm building. For context: I'm using Javascript on CentOS. I'm open to any suggestions. This is happening in both development and production on Windows 10. Any ideas what could be causing this? Could someone point me to the right documentation?