github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/ops/update_metadata.py (about) 1 from google.cloud import storage 2 import os 3 import sys 4 import datetime 5 6 7 def update_metadata_function(BUCKET_NAME: str, FILE_NAME: str): 8 storage_client = storage.Client() 9 bucket = storage_client.bucket(BUCKET_NAME) 10 blob = bucket.get_blob(FILE_NAME) 11 12 if not blob: 13 print(f"Could not access the file at 'gs://{BUCKET_NAME}/{FILE_NAME}'.", file=sys.stderr) 14 15 metadata = {"x-goog-meta-last-updated": f"{datetime.datetime.utcnow().isoformat()}Z"} 16 blob.metadata = metadata 17 blob.patch() 18 19 20 if __name__ == "__main__": 21 if not os.environ.get("GOOGLE_APPLICATION_CREDENTIALS"): 22 print("'GOOGLE_APPLICATION_CREDENTIALS' env variable not set.", file=sys.stderr) 23 sys.exit(1) 24 25 print(sys.argv) 26 27 args = sys.argv 28 29 print(os.path.basename(__file__)) 30 31 if args[0] == os.path.basename(__file__): 32 args = args[1:] 33 34 if not len(args) == 2: 35 print("Please provide the 'bucket name' and 'metadata file'.", file=sys.stderr) 36 sys.exit(1) 37 38 BUCKET_NAME = sys.argv[1] 39 FILE_NAME = sys.argv[2] 40 update_metadata_function(BUCKET_NAME=BUCKET_NAME, FILE_NAME=FILE_NAME)