added overwrite to sync.py

This commit is contained in:
ClF3 2024-04-24 11:16:46 +08:00
parent 79e3836a51
commit 4b9fbd4d36
1 changed files with 13 additions and 5 deletions

18
sync.py
View File

@ -6,7 +6,8 @@ parser = argparse.ArgumentParser()
parser.add_argument('--container', help='Name of the container', default='blog') parser.add_argument('--container', help='Name of the container', default='blog')
parser.add_argument('--remote', help='Path to the remote images', default='posts') parser.add_argument('--remote', help='Path to the remote images', default='posts')
parser.add_argument('--local', help='Path to the local images', default='webp') parser.add_argument('--local', help='Path to the local images', default='webp')
parser.add_argument('--ignore', help='Files to ignore', default='background.webp', action='append') parser.add_argument('--ignore', help='Files to ignore', default=['background.webp'], action='append')
parser.add_argument('--overwrite', help='Overwrite existing images', action='store_true')
args=parser.parse_args() args=parser.parse_args()
if args.remote[-1] != '/': if args.remote[-1] != '/':
@ -18,13 +19,20 @@ try:
blob_service_client = BlobServiceClient.from_connection_string(connect_str) blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_client = blob_service_client.get_container_client(args.container) 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)] existing_files=[blob.name.split("/")[-1] for blob in container_client.list_blobs(name_starts_with=args.remote)]
# print("Existing files in the container:")
# print(existing_files)
for root,fir,files in os.walk(args.local): for root,fir,files in os.walk(args.local):
for file in files: for file in files:
if file in existing_files or file in args.ignore: if file in args.ignore:
print("skipped: " + file) print("ignored: " + file)
continue 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: try:
print("Uploading: " + file) print("Uploading: " + file)
img_path = os.path.join(root,file) img_path = os.path.join(root,file)