Created
February 12, 2019 21:28
-
-
Save bchess/0165e08818ddd34153b4712da590a43e to your computer and use it in GitHub Desktop.
List GS objects that have been deleted
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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