Last active
July 4, 2023 17:30
-
-
Save matsonj/3b761bda7fd1d6f003d5a928936e6549 to your computer and use it in GitHub Desktop.
load arbitrary files to azure blob & delete them from local
This file contains 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
# exec with `python3 load2azure.py` | |
import os | |
import argparse | |
import logging | |
from azure.storage.blob import BlobServiceClient, BlobClient | |
from dotenv import load_dotenv | |
# load env variables | |
load_dotenv() | |
# Set your default values | |
DEFAULT_CONNECTION_STRING = os.getenv('AZURE_STORAGE_CONNECTION_STRING','None') | |
DEFAULT_CONTAINER_NAME = "container-bname" | |
DEFAULT_SOURCE_FOLDER = "path/to/file" | |
# Set up logging | |
logging.basicConfig(level=logging.INFO) | |
def upload_files_to_blob(connection_string, blob_container_name, source_folder): | |
blob_service_client = BlobServiceClient.from_connection_string(connection_string) | |
container_client = blob_service_client.get_container_client(blob_container_name) | |
for file in os.listdir(source_folder): | |
blob_client = container_client.get_blob_client(file) | |
with open(os.path.join(source_folder, file), "rb") as data: | |
blob_client.upload_blob(data) | |
logging.info(f"Uploaded {file} to {blob_container_name}") | |
os.remove(os.path.join(source_folder, file)) | |
logging.info(f"Deleted {file} from local directory") | |
print("All files uploaded successfully!") | |
def main(): | |
parser = argparse.ArgumentParser(description="Upload files to Azure Blob Storage") | |
parser.add_argument("--connection_string", default=DEFAULT_CONNECTION_STRING, help="The connection string for your Azure Storage account") | |
parser.add_argument("--container_name", default=DEFAULT_CONTAINER_NAME, help="The name of your blob container") | |
parser.add_argument("--source_folder", default=DEFAULT_SOURCE_FOLDER, help="The source folder path on your local machine") | |
args = parser.parse_args() | |
upload_files_to_blob(args.connection_string, args.container_name, args.source_folder) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment