Created
July 9, 2020 16:30
-
-
Save bigsnarfdude/fb090ab746e9f911618e2191fa4bd1f5 to your computer and use it in GitHub Desktop.
azure_load.py
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
import os | |
from os import listdir | |
from os.path import isfile, join | |
import asyncio | |
from azure.storage.blob import BlobServiceClient | |
from azure.storage.blob.aio import BlobClient | |
from azure.storage.blob.aio import ContainerClient | |
from datetime import datetime, timedelta | |
async def upload_single_file_async(connect_str, target_container, folder_path, file_name): | |
blob = BlobClient.from_connection_string(conn_str=connect_str, container_name=target_container, blob_name=file_name) | |
with open(join(folder_path, file_name), "rb") as data: | |
await blob.upload_blob(data) | |
async def upload_files(connect_str, target_container, folder_path): | |
list_of_files = [] | |
for f in listdir(folder_path): | |
if isfile(join(folder_path, f)): | |
list_of_files.append(f) | |
for file_name in list_of_files: | |
result = await upload_single_file_async(connect_str, target_container, folder_path, file_name) | |
return result | |
async def get_list_files(connect_str, target_container): | |
container = ContainerClient.from_connection_string(conn_str=connect_str, container_name=target_container) | |
blob_list = [] | |
async for blob in container.list_blobs(): | |
blob_list.append(blob) | |
return blob_list | |
async def main(): | |
await upload_files(connect_str, target_container, folder_path) | |
await get_list_files(connect_str, target_container) | |
connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING') | |
yesterday = datetime.now() - timedelta(days=1) | |
date_time = yesterday.strftime('-%Y-%m-%d') | |
target_container = 'serverdumps' + date_time | |
folder_path = 'D:\\data\\all_server_backups' | |
blob_service_client = BlobServiceClient.from_connection_string(connect_str) | |
container_client = blob_service_client.create_container(target_container) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment