You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何读取GCS子目录/前缀下文件及指定具体文件路径?

Understanding GCS Prefixes/Delimiters & Correctly Referencing Your Files

Hey there! Let's unpack this step by step to clear up the confusion around GCS's prefix/delimiter system and fix your file path issue.

First: What Are Prefixes & Delimiters in GCS?

Unlike your local filesystem, GCS doesn't have actual "folders"—everything is an object stored in a bucket. Prefixes and delimiters are just conventions to simulate a directory structure:

  • A prefix is a string that starts an object's name (like Folder1/). When you use list_blobs(prefix='Folder1'), you're asking for all objects whose names start with that string.
  • A delimiter (almost always /) tells GCS to treat objects as if they're in "subdirectories". For example, using list_blobs(prefix='Folder1/', delimiter='/') will only return objects directly under the "Folder1" simulated folder, not any objects in subfolders under it.

Fixing Your GCS File Path Issue

The HTTPS URL you tried (https://storage.cloud.google.com/MYBUCKETNAME/Folder1/doc3.csv?supportedpurview=project) is a console preview link—it's meant for viewing files in the browser, not for referencing objects in your code.

Instead, GCS uses the object key (which matches the blob's name attribute) to reference files. For your doc3.csv, the object key is Folder1/doc3.csv—that's all you need!

Example Code to Access doc3.csv

Here's how to target that specific file in your existing code:

from google.cloud import storage
import os

client = storage.Client()
bucket = client.bucket('mybucketname')

# Reference the specific blob using its object key
target_blob = bucket.blob('Folder1/doc3.csv')

# Now you can perform actions like downloading it to your local machine:
target_blob.download_to_filename('C:/Users/admin/Documents/Folder1/doc3.csv')

# Or get a signed URL for temporary access (if the file is private):
# signed_url = target_blob.generate_signed_url(expiration=3600)  # Expires in 1 hour

# Or get the public URL (if the file is set to public):
# public_url = target_blob.public_url

Why Your Original List Code Works

When you ran bucket.list_blobs(prefix='Folder1'), it returned all objects starting with Folder1—which includes Folder1/doc1.csv, Folder1/doc2.csv, and Folder1/doc3.csv. Each of these is an object with a unique key that you can directly reference with bucket.blob("<object-key>").

Quick Recap

  • Skip the browser URL in your code—use the object key (like Folder1/doc3.csv) instead.
  • Prefixes let you filter objects by their name's starting string; delimiters help simulate folder hierarchies.
  • To reference any file, just pass its full object key to bucket.blob().

内容的提问来源于stack exchange,提问作者NoobCoder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 07:17:27