123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import os
- import uuid
- import sys
- from azure.storage.blob import BlockBlobService, PublicAccess
- def run_sample():
- try:
-
- blob_service_client = BlockBlobService(
- account_name='accountname', account_key='accountkey')
-
- container_name = 'quickstartblobs'
- blob_service_client.create_container(container_name)
-
- blob_service_client.set_container_acl(
- container_name, public_access=PublicAccess.Container)
-
- local_path = os.path.expanduser("~/Sample")
- if not os.path.exists(local_path):
- os.makedirs(os.path.expanduser("~/Sample"))
- local_file_name = "QuickStart_" + str(uuid.uuid4()) + ".txt"
- full_path_to_file = os.path.join(local_path, local_file_name)
-
- file = open(full_path_to_file, 'w')
- file.write("Hello, World!")
- file.close()
- print("Temp file = " + full_path_to_file)
- print("\nUploading to Blob storage as blob" + local_file_name)
-
- blob_service_client.create_blob_from_path(
- container_name, local_file_name, full_path_to_file)
-
- print("\nList blobs in the container")
- generator = blob_service_client.list_blobs(container_name)
- for blob in generator:
- print("\t Blob name: " + blob.name)
-
-
- full_path_to_file2 = os.path.join(local_path, str.replace(
- local_file_name ,'.txt', '_DOWNLOADED.txt'))
- print("\nDownloading blob to " + full_path_to_file2)
- blob_service_client.get_blob_to_path(
- container_name, local_file_name, full_path_to_file2)
- sys.stdout.write("Sample finished running. When you hit <any key>, the sample will be deleted and the sample "
- "application will exit.")
- sys.stdout.flush()
- input()
-
- blob_service_client.delete_container(container_name)
- os.remove(full_path_to_file)
- os.remove(full_path_to_file2)
- except Exception as e:
- print(e)
- if __name__ == '__main__':
- run_sample()
|