GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Storage list_blobs iterator doesn't return prefixes · Issue #1388 · googleapis/python-storage

This repository was archived by the owner on Mar 31, 2026. It is now read-only.
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Storage list_blobs iterator doesn't return prefixes #1388

Copy link
Copy link
Closed
Closed
Storage list_blobs iterator doesn't return prefixes#1388
Copy link
Assignees
cojenco
Labels
api: storageIssues related to the googleapis/python-storage API.priority: p3Desirable enhancement or fix. May not be included in next release.type: questionRequest for information or clarification. Not an issue.

Description

@andtrott
andtrott
opened on Nov 27, 2024
Issue body actions

Title: list_blobs() iterator.prefixes returns empty when API response contains prefixes

Issue Description

When using list_blobs() with a prefix and delimiter to list "directories" in a GCS bucket, the .prefixes property of the returned iterator is empty even though the raw API response shows that prefixes exist.

Reproduction Steps

Here's a complete example using the public bucket gcp-public-data-landsat:

from google.cloud import storage
import logging

# Set up logging to see API responses
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

# Initialize client and bucket
client = storage.Client()
bucket = client.bucket('gcp-public-data-landsat')

prefix = "LC08/"  # This prefix exists in the landsat bucket
delimiter = "/"

# First show that the raw API response contains prefixes
logger.debug("\nChecking raw API response:")
page = bucket.list_blobs(prefix=prefix, delimiter=delimiter)._get_next_page_response()
logger.debug(f"Raw API response: {page}")  # Shows prefixes exist

# Now demonstrate that iterator.prefixes is empty
logger.debug("\nTrying to iterate prefixes:")
iterator = bucket.list_blobs(prefix=prefix, delimiter=delimiter)
prefix_count = 0
for prefix in iterator.prefixes:
    prefix_count += 1
    logger.debug(f"Found prefix: {prefix}")
logger.debug(f"Total prefixes found: {prefix_count}")  # Shows 0

# Also demonstrate list() returns empty
prefixes = list(iterator.prefixes)
logger.debug(f"List of prefixes: {prefixes}")  # Shows []

Expected Behavior

The iterator's .prefixes property should provide access to the prefixes returned in the API response
Both iterating through iterator.prefixes and calling list(iterator.prefixes) should return the prefixes

Actual Behavior

The raw API response shows prefixes exist: {'kind': 'storage#objects', 'prefixes': [...]}
However, iterator.prefixes appears empty when iterated
list(iterator.prefixes) returns an empty list

Workaround

Currently using direct access to the API response as a workaround:
pythonCopypage = bucket.list_blobs(prefix=prefix, delimiter=delimiter)._get_next_page_response()
prefixes = page.get('prefixes', [])

Environment

google-cloud-storage version: 2.18.2
Python version: Python 3.11.6
OS: Darwin 23.6.0

Related Issues

#436
#483

This issue was co-authored by Claude.

Activity

  1. blunderbuss-gcf
    assigned
    cojenco
    on Nov 27, 2024
  2. product-auto-label
    added
    api: storageIssues related to the googleapis/python-storage API.
    on Nov 27, 2024
  3. cojenco
    added
    type: questionRequest for information or clarification. Not an issue.
    priority: p3Desirable enhancement or fix. May not be included in next release.
    on Nov 28, 2024
  4. cojenco commented on Nov 28, 2024

    @cojenco
    cojenco
    on Nov 28, 2024
    Contributor
    More actions

    Hi andtrott@ thanks for the question. The iterator has to be called first in order to access the list of prefixes. Try adding a list(iterator)to consume the iterator first

    iterator = bucket.list_blobs(prefix=prefix, delimiter=delimiter)
    list(iterator)
    ...
    prefix_count = 0
    for prefix in iterator.prefixes:
    

    See the list_files_with_prefix example with detailed explanation

    See related issues
    #1208 (comment)
    #863 (comment)

    Reacted by Andrew Trott
  5. andtrott commented on Dec 6, 2024

    @andtrott
    andtrott
    on Dec 6, 2024
    Author
    More actions

    Thanks for the response! Stand by the expected behavior, but that's a good workaround - thanks!

    Reacted by cojenco
  6. cojenco
    closed this as completedon Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @cojenco
    cojenco

Labels

api: storageIssues related to the googleapis/python-storage API.priority: p3Desirable enhancement or fix. May not be included in next release.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.