Skip to content

Instantly share code, notes, and snippets.

@bigsnarfdude
Last active May 26, 2020 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigsnarfdude/4cb0cf651ecf880b7826db0aa5b3e2aa to your computer and use it in GitHub Desktop.
Save bigsnarfdude/4cb0cf651ecf880b7826db0aa5b3e2aa to your computer and use it in GitHub Desktop.
async_upload_azure_directory.py
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
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')
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
target_container = 'retailstorage20200523'
container_client = blob_service_client.create_container(target_container)
folder_path = './data'
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
from subprocess import check_output
import subprocess
def run_win_cmd(cmd):
result = []
process = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
for line in process.stdout:
result.append(line)
errcode = process.returncode
#for line in result:
# print(line)
if errcode is not None:
raise Exception('cmd %s failed, see above for details', cmd)
return result
list_of_servers = [ 'server1',
'server2',
'server3',
'server4']
zero_byte_check = 'dir C:\\scripts\\output\\'
list_of_servers_upload = set()
list_of_empty_servers = set()
for server in list_of_servers:
for item in run_win_cmd(zero_byte_check + server):
if ' 0 bytes' in str(item):
list_of_empty_servers.add(server)
else:
list_of_servers_upload.add(server)
for good_server in list_of_servers_upload:
print("command", good_server)
print(list_of_empty_servers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment