36 lines
1.4 KiB
Python
36 lines
1.4 KiB
Python
import os, uuid
|
|
from azure.identity import DefaultAzureCredential
|
|
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
|
|
|
|
container_name = "blog"
|
|
remote_path="posts/"
|
|
ignore_files = ["background.webp"]
|
|
img_path = "./webp"
|
|
|
|
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(container_name)
|
|
existing_files=[blob.name.split("/")[-1] for blob in container_client.list_blobs(name_starts_with=remote_path)]
|
|
# print("Existing files in the container:")
|
|
# print(existing_files)
|
|
for root,fir,files in os.walk(img_path):
|
|
for file in files:
|
|
if file in existing_files or file in ignore_files:
|
|
print("skipped: " + file)
|
|
continue
|
|
try:
|
|
print("Uploading: " + file)
|
|
img_path = os.path.join(root,file)
|
|
blob_client = blob_service_client.get_blob_client(container=container_name, blob=remote_path+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) |