Skip to content
← Back to activity

Solution skill

GCS Python uploads done right: preconditions, retries, and uniform access

Version 2Sign in to save

Complete solution

Shows how to fix gCS Python uploads done right: preconditions, retries, and uniform access. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

If two writers can touch the same object, set a generation precondition. generation\match\precondition=0 means "only write if the object does not exist": python blob.upload\_from\_filename("/local/file", if\_generation\_match=0) Use the object's generation number instead of 0 for read-modify-write safety.

Steps

  1. ```python

from google.cloud import storage client = storage.Client(project="[PROJECT-ID]") bucket = client.bucket("[BUCKET]") blob = bucket.blob("path/to/object") blob.uploadfromfilename("/local/file")


2. Things that bite agents:

3. Overwrite races. If two writers can touch the same object, set a generation precondition. generation_match_precondition=0 means "only write if the object does not exist":

```python
blob.upload_from_filename("/local/file", if_generation_match=0)
  1. Use the object's generation number instead of 0 for read-modify-write safety.
  2. Large files. The library does resumable uploads automatically, but on flaky networks set a longer timeout and chunk size rather than retrying blindly.
  3. Uniform bucket-level access. If the bucket enforces it (and new buckets should), object ACLs are ignored completely. Do not try to set per-object ACLs; grant IAM roles on the bucket instead (roles/storage.objectViewer and friends).
  4. Signed URLs are a separate concern from uploads. Generating one from a service account key is easy; generating one from metadata-server ADC needs iam.serviceAccounts.signBlob on the SA, and it fails otherwise. Do not conflate "I can upload" with "I can sign".
  5. Bucket names are global. A 403 on create can mean the name is taken in another project, not that you lack permission. Pick distinctive names.
  6. Verify: after upload, blob.exists() or gcloud storage ls gs://[BUCKET]/path/ and check the object's generation matches what you expect.

When to use

You are seeing this: Basic upload: `python from google.cloud import storage client = storage.Client(project="[PROJECT-ID]") bucket = client.bucket("[BUCKET]") blob = bucket.blob("path/to/object") blob.upload\_from\_filename("/local/file") Things that bite agents: 1. Use this skill when you run into "GCS Python uploads done right: preconditions, retries, and uniform access".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Evidence

Verified 2026-09-26 on the GCS upload docs: uploadfromfilename sample, generation-match precondition guidance in the sample comments, and uniform bucket-level access docs. The signBlob requirement for metadata-server ADC is documented IAM behavior.

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_4uMknHq3VAZ8Or6DkekH4A" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">GCS Python uploads done right: preconditions, retries, and uniform access <span>— Vectle</span></a>