Fixing Google Cloud Storage Error: ‘Permission Denied for storage.buckets.get’ in PHP
The error message you’re encountering suggests that the Google Cloud service account (xxxxx@xxxxxxxxx.iam.gserviceaccount.com
) doesn’t have the necessary permissions to access the specified Google Cloud Storage bucket. Specifically, the permission 'storage.buckets.get'
is missing, which is required to access or retrieve information about the bucket.
PHP Fatal error: Uncaught Google\Cloud\Core\Exception\ServiceException: {"error":{"code":403,"message":"tts-655@sunny-wavelet-424212-s5.iam.gserviceaccount.com does not have storage.buckets.get access to the Google Cloud Storage bucket. Permission 'storage.buckets.get' denied on resource (or it may not exist).","errors":[{"message":"tts-655@sunny-wavelet-424212-s5.iam.gserviceaccount.com does not have storage.buckets.get access to the Google Cloud Storage bucket. Permission 'storage.buckets.get' denied on resource (or it may not exist).","domain":"global","reason":"forbidden"}]}}
Here are the steps to resolve this issue:
1. Grant Permissions to the Service Account
You need to ensure that the service account has the required permissions to access the Cloud Storage bucket.
a. Identify the correct bucket
Make sure that the bucket you’re trying to access exists and that you have the right bucket name.
b. Assign Storage Object Viewer
or Storage Admin
role
- Go to the Google Cloud Console: https://console.cloud.google.com/.
- Navigate to IAM & Admin > IAM.
- Find the service account (
xxxxx@xxxxxxxxx.iam.gserviceaccount.com
) in the list. - Click Edit (the pencil icon) next to the service account.
- In the Role section, click + Add another role.
- Select either:
- Storage Object Viewer: If you just need read-only access.
- Storage Admin: If you need full access to manage storage buckets.
- Save the changes.
c. Verify that the correct project is being used
Ensure that the service account is accessing the correct Google Cloud project where the bucket resides.
2. Verify the Storage Bucket Exists
Make sure the Google Cloud Storage bucket you’re trying to access exists in the project. You can verify this by going to Cloud Storage in the Google Cloud Console and checking the list of buckets.
3. Double-check Bucket Policies
If the bucket has any special policies or access controls, ensure that the service account is included in these policies with the necessary permissions.
Once the correct permissions are set, the error should be resolved. Let me know if you need further assistance!