Skip to content

Instantly share code, notes, and snippets.

@bchess
Created February 12, 2019 21:28
Show Gist options
  • Save bchess/0165e08818ddd34153b4712da590a43e to your computer and use it in GitHub Desktop.
Save bchess/0165e08818ddd34153b4712da590a43e to your computer and use it in GitHub Desktop.
List GS objects that have been deleted
from urllib.parse import urlparse
from google.cloud import storage
import sys
if len(sys.argv) != 2:
print('Usage: %s gs://BUCKETNAME/PREFIX' % (sys.argv[0]), file=sys.stderr)
sys.exit(1)
parse_result = urlparse(sys.argv[1])
assert parse_result.scheme == 'gs', 'Must be a gs:// URL'
c = storage.Client()
bucket = c.get_bucket(parse_result.netloc)
blobs = bucket.list_blobs(prefix=parse_result.path.lstrip('/'), versions=True)
for blob in blobs:
if not blob.time_deleted:
continue
print(f'gs://{parse_result.netloc}/{blob.name}#{blob.generation}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment