CodexBloom - Programming Q&A Platform

GCP Compute Engine VM how to to access Cloud Storage bucket with 'Access Denied' despite correct service account roles

👀 Views: 1 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-09
gcp google-cloud-storage compute-engine Python

Quick question that's been bugging me - I'm having a hard time understanding I'm working on a project and hit a roadblock... I keep running into I'm running a Compute Engine VM instance that needs to access a Cloud Storage bucket for file uploads. I've set up a service account with the 'Storage Object Admin' role and attached it to the VM instance. However, when I try to upload a file using the Python `google-cloud-storage` library, I get the following behavior: `google.api_core.exceptions.Forbidden: 403 GET https://www.googleapis.com/storage/v1/b/my-bucket/o?prefix=myfile.txt: Access Denied`. I've verified that the service account has the correct permissions and the VM is indeed using this service account. My `requirements.txt` includes `google-cloud-storage==2.5.0`, and I'm using the following code to upload the file: ```python from google.cloud import storage client = storage.Client() bucket = client.bucket('my-bucket') blob = bucket.blob('myfile.txt') with open('localfile.txt', 'rb') as file_obj: blob.upload_from_file(file_obj) ``` I've also checked that the bucket policy allows access from the service account, and I can confirm that the bucket name is correct. I've tried reattaching the service account and even restarting the VM, but the same behavior continues. Is there something I'm missing with regard to IAM roles or bucket permissions? Any insights would be greatly appreciated. For context: I'm using Python on Windows. I've been using Python for about a year now. Am I missing something obvious? Cheers for any assistance! This is happening in both development and production on Ubuntu 22.04. Has anyone dealt with something similar?