import os, uuid from azure.identity import DefaultAzureCredential from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient import argparse import json defaults=json.load(open('defaults.json')) parser = argparse.ArgumentParser() parser.add_argument('--container', help='Name of the container', default=defaults['container']) parser.add_argument('--remote', help='Path to the remote images', default=defaults['remote']) parser.add_argument('--local', help='Path to the local images', default=defaults['local']) parser.add_argument('--ignore', help='Files to ignore', default=defaults['ignore'], action='append') parser.add_argument('--overwrite', help='Overwrite existing images', action='store_true') args=parser.parse_args() pics=[] if args.remote!='' and args.remote[-1] != '/': args.remote += '/' try: with open('secret.txt', 'r') as f: connect_str = f.read() blob_service_client = BlobServiceClient.from_connection_string(connect_str) container_client = blob_service_client.get_container_client(args.container) existing_files=[blob.name.split("/")[-1] for blob in container_client.list_blobs(name_starts_with=args.remote)] for root,fir,files in os.walk(args.local): for file in files: pics.append(file) if file in args.ignore: print("ignored: " + file) continue if file in existing_files: if args.overwrite: print("Overwriting: " + file) blob_client = blob_service_client.get_blob_client(container=args.container, blob=args.remote+file) blob_client.delete_blob() else: print("skipped: " + file) continue try: print("Uploading: " + file) img_path = os.path.join(root,file) blob_client = blob_service_client.get_blob_client(container=args.container, blob=args.remote+file) with open(file=img_path, mode="rb") as data: blob_client.upload_blob(data) except Exception as ex: print(f"Error uploading {file}.") print(ex) print("Upload finished.") except Exception as ex: print('Exception:') print(ex) finally: json.dump(pics, open('pics.json', 'w'))