class QueueBasicSamples(): def __init__(self): self.random_data = RandomData() # Runs all samples for Azure Storage Queue service. # Input Arguments: # account - CloudStorageAccount to use for running the samples def run_all_samples(self, account): try: print('Azure Storage Basic Queue samples - Starting.') # declare variables queuename = "queuesample" + self.random_data.get_random_name(6) queuename2 = "queuesample" + self.random_data.get_random_name(6) # create a new queue service that can be passed to all methods queue_service = account.create_queue_service() # Basic queue operations such as creating a queue and listing all queues in your account print('\n\n* Basic queue operations *\n') self.basic_queue_operations(queue_service, queuename, queuename2) # Add a message to a queue in your account print('\n\n* Basic message operations *\n') self.basic_queue_message_operations(queue_service, queuename) except Exception as e: if (config.IS_EMULATED): print( 'Error occurred in the sample. Please make sure the Storage emulator is running.', e) else: print( 'Error occurred in the sample. Please make sure the account name and key are correct.', e) finally: # Delete the queues from your account self.delete_queue(queue_service, queuename) self.delete_queue(queue_service, queuename2) print('\nAzure Storage Basic Queue samples - Completed.\n') # Basic queue operations including creating and listing def basic_queue_operations(self, queue_service, queuename, queuename2): # Create a queue or leverage one if already exists print('Attempting create of queue: ', queuename) queue_service.create_queue(queuename) print('Successfully created queue: ', queuename) # Create a second queue or leverage one if already exists print('Attempting create of queue: ', queuename2) queue_service.create_queue(queuename2) print('Successfully created queue: ', queuename2) #List all queues with prefix "queuesample" print('Listing all queues with prefix "queuesample"') queues = queue_service.list_queues("queuesample") for queue in queues: print('\t', queue.name) # Basic queue operations on messages def basic_queue_message_operations(self, queue_service, queuename): # Add a number of messages to the queue. # if you do not specify time_to_live, the message will expire after 7 days # if you do not specify visibility_timeout, the message will be immediately visible messagename = "test message" for i in range(1, 10): queue_service.put_message(queuename, messagename + str(i)) print('Successfully added message: ', messagename + str(i)) # Get length of queue # Retrieve queue metadata which contains the approximate message count ie.. length. # Note that this may not be accurate given dequeueing operations that could be happening in parallel metadata = queue_service.get_queue_metadata(queuename) length = metadata.approximate_message_count print('Approximate length of the queue: ', length) # Look at the first messages only without dequeueing it messages = queue_service.peek_messages(queuename) for message in messages: print('Peeked message content is: ', message.content) # Look at the first 5 messages only without any timeout without dequeueing it messages = queue_service.peek_messages(queuename, num_messages=5) for message in messages: print('Peeked message content is: ', message.content) # Update the visibility timeout of a message # You can also use this operation to update the contents of a message. print('Update the visibility timeout of a message') messages = queue_service.get_messages(queuename) message = messages[0] queue_service.update_message(queuename, message.id, message.pop_receipt, 300) # Dequeuing a message # First get the message, to read and process it. # Specify num_messages to process a number of messages. If not specified, num_messages defaults to 1 # Specify visibility_timeout optionally to set how long the message is visible messages = queue_service.get_messages(queuename) for message in messages: print('Message for dequeueing is: ', message.content) # Then delete it. # When queue is deleted all messages are deleted, here is done for demo purposes # Deleting requires the message id and pop receipt (returned by get_messages) queue_service.delete_message(queuename, message.id, message.pop_receipt) print('Successfully dequeued message') # Clear out all messages from the queue queue_service.clear_messages(queuename) print('Successfully cleared out all queue messages') # Delete the queue def delete_queue(self, queue_service, queuename): # Delete the queue. # Warning: This will delete all the messages that are contained in it. print('Attempting delete of queue: ', queuename) if queue_service.exists(queuename): queue_service.delete_queue(queuename) print('Successfully deleted queue: ', queuename)
class QueueAdvancedSamples(): def __init__(self): self.random_data = RandomData() # Runs all samples for Azure Storage Queue service. # Input Arguments: # account - CloudStorageAccount to use for running the samples def run_all_samples(self, account): try: print('Azure Storage Advanced Queue samples - Starting.') # create a new queue service that can be passed to all methods queue_service = account.create_queue_service() print('\n\n* List queues *\n') self.list_queues(queue_service) print('\n\n* Set cors Rules *\n') self.set_cors_rules(queue_service) print('\n\n* ACL operations *\n') self.queue_acl_operations(queue_service) print('\n\n* Set service logging and metrics properties *\n') self.set_service_properties(queue_service) print('\n\n* Set queue metadata *\n') self.metadata_operations(queue_service) except Exception as e: if (config.IS_EMULATED): print( 'Error occurred in the sample. Please make sure the Storage emulator is running.', e) else: print( 'Error occurred in the sample. Please make sure the account name and key are correct.', e) finally: print('\nAzure Storage Advanced Queue samples - Completed\n') # Manage queues including, creating, listing and deleting def list_queues(self, queue_service): queue_prefix = "queuesample" + self.random_data.get_random_name(6) try: print('1. Create multiple queues with prefix: ', queue_prefix) for i in range(5): queue_service.create_queue(queue_prefix + str(i)) print('2. List queues with prefix: ', queue_prefix) queues = queue_service.list_queues(queue_prefix) for queue in queues: print(' Queue name:' + queue.name) finally: print('3. Delete queues with prefix:' + queue_prefix) for i in range(5): if queue_service.exists(queue_prefix + str(i)): queue_service.delete_queue(queue_prefix + str(i)) print("List queues sample completed") # Manage CORS rules def set_cors_rules(self, queue_service): cors_rule = CorsRule(allowed_origins=['*'], allowed_methods=['POST', 'GET'], allowed_headers=['*'], exposed_headers=['*'], max_age_in_seconds=3600) try: print('1. Get Cors Rules') original_cors_rules = queue_service.get_queue_service_properties( ).cors print('2. Overwrite Cors Rules') queue_service.set_queue_service_properties(cors=[cors_rule]) finally: print('3. Revert Cors Rules back the original ones') #reverting cors rules back to the original ones queue_service.set_queue_service_properties( cors=original_cors_rules) print("CORS sample completed") # Manage properties of the Queue service, including logging and metrics settings, and the default service version. def set_service_properties(self, queue_service): try: print('1. Get Queue service properties') props = queue_service.get_queue_service_properties() retention = RetentionPolicy(enabled=True, days=5) logging = Logging(delete=True, read=False, write=True, retention_policy=retention) hour_metrics = Metrics(enabled=True, include_apis=True, retention_policy=retention) minute_metrics = Metrics(enabled=False) print('2. Ovewrite Queue service properties') queue_service.set_queue_service_properties( logging=logging, hour_metrics=hour_metrics, minute_metrics=minute_metrics) finally: print( '3. Revert Queue service properties back to the original ones') queue_service.set_queue_service_properties( logging=props.logging, hour_metrics=props.hour_metrics, minute_metrics=props.minute_metrics) print('4. Set Queue service properties completed') # Manage metadata of a queue def metadata_operations(self, queue_service): queue_name = 'queue' + self.random_data.get_random_name(6) try: # Create a new queue print('1. Create a queue with custom metadata - ' + queue_name) queue_service.create_queue(queue_name, { 'category': 'azure-storage', 'type': 'queue-sample' }) # Get all the queue metadata print('2. Get queue metadata') metadata = queue_service.get_queue_metadata(queue_name) print(' Metadata:') for key in metadata: print(' ' + key + ':' + metadata[key]) finally: # Delete the queue print("3. Delete Queue") if queue_service.exists(queue_name): queue_service.delete_queue(queue_name) # Manage access policy of a queue def queue_acl_operations(self, queue_service): queue_name = 'aclqueue' + self.random_data.get_random_name(6) try: print('1. Create a queue with name - ' + queue_name) queue_service.create_queue(queue_name) print('2. Set access policy for queue') access_policy = AccessPolicy(permission=QueuePermissions.READ, expiry=datetime.datetime.utcnow() + datetime.timedelta(hours=1)) identifiers = {'id': access_policy} queue_service.set_queue_acl(queue_name, identifiers) print('3. Get access policy from queue') acl = queue_service.get_queue_acl(queue_name) print('4. Clear access policy in queue') # Clear queue_service.set_queue_acl(queue_name) finally: print('5. Delete queue') if queue_service.exists(queue_name): queue_service.delete_queue(queue_name) print("Queue ACL operations sample completed")
class FileBasicSamples(): def __init__(self): self.random_data = RandomData() # Runs all samples for Azure Storage File service. # Input Arguments: # account - CloudStorageAccount to use for running the samples def run_all_samples(self, account): print('Azure Storage File Basis samples - Starting.') #declare variables filename = 'filesample' + self.random_data.get_random_name(6) sharename = 'sharesample' + self.random_data.get_random_name(6) # Create a new file service that can be passed to all methods file_service = account.create_file_service() try: print('\n\n* Basic file operations *\n') self.basic_file_operations(file_service, sharename, filename) except Exception as e: print( 'Error occurred in the sample. Please make sure the account name and key are correct.', e) finally: # Delete all Azure Files created in this sample self.file_delete_samples(file_service, sharename, filename) print('\nAzure Storage File Basic samples - Completed.\n') def basic_file_operations(self, file_service, sharename, filename): # Creating an SMB file share in your Azure Files account. print( '\nAttempting to create a sample file from text for upload demonstration.' ) # All directories and share must be created in a parent share. # Max capacity: 5TB per share print('Creating sample share.') file_service.create_share(sharename) print('Sample share "' + sharename + '" created.') # Creating an optional file directory in your Azure Files account. print('Creating a sample directory.') file_service.create_directory(sharename, 'mydirectory') print('Sample directory "mydirectory" created.') # Uploading text to sharename/mydirectory/my_text_file.txt in Azure Files account. # Max capacity: 1TB per file print('Uploading a sample file from text.') file_service.create_file_from_text( sharename, # share 'mydirectory', # directory path - root path if none filename, # destination file name 'Hello World! - from text sample') # file text print('Sample file "' + filename + '" created and uploaded to: ' + sharename + '/mydirectory') # Demonstrate how to copy a file print('\nCopying file ' + filename) sourcefile = file_service.make_file_url(sharename, 'mydirectory', filename) copy = file_service.copy_file(sharename, None, 'file1copy', sourcefile) if (copy.status == 'pending'): # Demonstrate how to abort a copy operation (just for demo, probably will never get here) print('Abort copy operation') file_service.abort_copy_file(sharename, None, 'file1copy', copy.id) else: print('Copy was a ' + copy.status) # Demonstrate how to create a share and upload a file from a local temporary file path print( '\nAttempting to upload a sample file from path for upload demonstration.' ) # Creating a temporary file to upload to Azure Files print('Creating a temporary file from text.') with tempfile.NamedTemporaryFile(delete=False) as my_temp_file: # my_temp_file.file.write(b"Hello world!") print('Sample temporary file created.') # Uploading my_temp_file to sharename/mydirectory folder in Azure Files # Max capacity: 1TB per file print('Uploading a sample file from local path.') file_service.create_file_from_path( sharename, # share name None, # directory path - root path if none filename, # destination file name my_temp_file.name) # full source path with file name print('Sample file "' + filename + '" uploaded from path to share: ' + sharename) # Close the temp file my_temp_file.close() # Get the list of valid ranges and write to the specified range print('\nGet list of valid ranges of the file.') ranges = file_service.list_ranges(sharename, None, filename) data = b'abcdefghijkl' print('Put a range of data to the file.') file_service.update_range(sharename, None, filename, data, ranges[0].start, ranges[0].end) # Demonstrate how to download a file from Azure Files # The following example download the file that was previously uploaded to Azure Files print( '\nAttempting to download a sample file from Azure files for demonstration.' ) destination_file = tempfile.tempdir + '\mypathfile.txt' file_service.get_file_to_path( sharename, # share name 'mydirectory', # directory path filename, # source file name destination_file) # destinatation path with name print('Sample file downloaded to: ' + destination_file) # Demonstrate how to list files and directories contains under Azure File share print( '\nAttempting to list files and directories directory under share "' + sharename + '":') # Create a generator to list directories and files under share # This is not a recursive listing operation generator = file_service.list_directories_and_files(sharename) # Prints the directories and files under the share for file_or_dir in generator: print(file_or_dir.name) # remove temp file os.remove(my_temp_file.name) print('Files and directories under share "' + sharename + '" listed.') print('\nCompleted successfully - Azure basic Files operations.') # Demonstrate how to delete azure files created for this demonstration # Warning: Deleting a share or directory will also delete all files and directories that are contained in it. def file_delete_samples(self, file_service, sharename, filename): print('\nDeleting all samples created for this demonstration.') try: # Deleting file: 'sharename/mydirectory/filename' # This is for demo purposes only, it's unnecessary, as we're deleting the share later print('Deleting a sample file.') file_service.delete_file( sharename, # share name 'mydirectory', # directory path filename) # file name to delete print('Sample file "' + filename + '" deleted from: ' + sharename + '/mydirectory') # Deleting directory: 'sharename/mydirectory' print( 'Deleting sample directory and all files and directories under it.' ) file_service.delete_directory( sharename, # share name 'mydirectory') # directory path print('Sample directory "/mydirectory" deleted from: ' + sharename) # Deleting share: 'sharename' print('Deleting sample share ' + sharename + ' and all files and directories under it.') if (file_service.exists(sharename)): file_service.delete_share(sharename) # share name print('Sample share "' + sharename + '" deleted.') print('\nCompleted successfully - Azure Files samples deleted.') except Exception as e: print('********ErrorDelete***********') print(e)
class BlobBasicSamples(): def __init__(self): self.random_data = RandomData() # Runs all samples for Azure Storage Blob service. # Input Arguments: # account - CloudStorageAccount to use for running the samples def run_all_samples(self, account): print('\n\nAzure Storage Blob sample - Starting.') try: # Block blob basics print('\n\n* Basic block blob operations *\n') self.basic_blockblob_operations(account) # Page blob basics print('\n\n* Basic page blob operations *\n') self.basic_pageblob_operations(account) # Snapshot print('\n\n* Snapshot sample *\n') self.basic_snapshot(account) if (config.IS_EMULATED == False): # Append blob basics # Append blob is not yet supported in the Emulator print('\n\n* Basic append blob operations *\n') self.basic_appendblob_operations(account) except Exception as e: if (config.IS_EMULATED): print( 'Error occurred in the sample. If you are using the emulator, please make sure the emulator is running.', e) else: print( 'Error occurred in the sample. Please make sure the account name and key are correct.', e) finally: print('\nAzure Storage Blob sample - Completed.\n') # Runs basic block blob samples for Azure Storage Blob service. # Input Arguments: # account - CloudStorageAccount to use for running the samples def basic_blockblob_operations(self, account): blob_name1 = "blob1" blob_name2 = "blob2" file_to_upload = "HelloWorld.png" # Create a Block Blob Service object blockblob_service = account.create_block_blob_service() #blockblob_service = BlockBlobService(account_name=config.STORAGE_ACCOUNT_NAME, account_key=config.STORAGE_ACCOUNT_KEY) container_name = 'blockblobbasicscontainer' + self.random_data.get_random_name( 6) try: # Create a new container print('1. Create a container with name - ' + container_name) blockblob_service.create_container(container_name) # Create blobs print('2. Create blobs') # Get full path on drive to file_to_upload by joining the fully qualified directory name and file name on the local drive full_path_to_file = os.path.join(os.path.dirname(__file__), file_to_upload) blockblob_service.create_blob_from_path(container_name, blob_name1, full_path_to_file) # Create blob from text blockblob_service.create_blob_from_text( container_name, blob_name2, self.random_data.get_random_name(256)) # List all the blobs in the container print('3. List Blobs in Container') generator = blockblob_service.list_blobs(container_name) for blob in generator: print('\tBlob Name: ' + blob.name) # Download the blob print('4. Download the blob') blockblob_service.get_blob_to_path( container_name, blob_name1, os.path.join(os.path.dirname(__file__), file_to_upload + '.copy.png')) blockblob_service.get_blob_to_path( container_name, blob_name2, os.path.join(os.path.dirname(__file__), 'blob2.copy.txt')) # Delete the blobs, this can be ommited because the container is deleted print('5. Delete blobs') blockblob_service.delete_blob(container_name, blob_name1) blockblob_service.delete_blob(container_name, blob_name2) finally: # Delete the container print("6. Delete Container") if blockblob_service.exists(container_name): blockblob_service.delete_container(container_name) # Runs basic page blob samples for Azure Storage Blob service. # Input Arguments: # account - CloudStorageAccount to use for running the samples def basic_pageblob_operations(self, account): file_to_upload = "HelloPageBlobWorld.txt" # Create a block blob service object pageblob_service = account.create_page_blob_service() container_name = 'pageblobbasicscontainer' + self.random_data.get_random_name( 6) try: # Create a new container print('1. Create a container with name - ' + container_name) pageblob_service.create_container(container_name) # Create a page blob print('2. Creating Page Blob') pageblob_service.create_blob_from_bytes( container_name, file_to_upload, self.random_data.get_random_bytes(512)) # List all the blobs in the container print('3. List Blobs in Container') blob_list = pageblob_service.list_blobs(container_name) for blob in blob_list: print('\tBlob Name: ' + blob.name) # Read a page blob print('4. Reading a Page Blob') readblob = pageblob_service.get_blob_to_bytes( container_name, # name of the container file_to_upload, # name of blob to read start_range=3, # page to start reading from end_range=10) # page to stop reading at # Delete the blob, this can be ommited because the container is deleted print('5. Delete Page Blob') pageblob_service.delete_blob(container_name, file_to_upload) finally: # Delete the container print("6. Delete Container") if pageblob_service.exists(container_name): pageblob_service.delete_container(container_name) # Runs basic append blob samples for Azure Storage Blob service. # Input Arguments: # account - CloudStorageAccount to use for running the samples def basic_appendblob_operations(self, account): file_to_upload = "HelloAppendBlobWorld.txt" # Create an append blob service object appendblob_service = account.create_append_blob_service() container_name = 'appendblobbasicscontainer' + self.random_data.get_random_name( 6) try: # Create a new container print('1. Create a container with name - ' + container_name) appendblob_service.create_container(container_name) # Create an append blob print('2. Create Append Blob') appendblob_service.create_blob(container_name, file_to_upload) # Write to an append blob print('3. Write to Append Blob') appendblob_service.append_blob_from_text( container_name, file_to_upload, '\tHello Append Blob world!\n') appendblob_service.append_blob_from_text( container_name, file_to_upload, '\tHello Again Append Blob world!') # List all the blobs in the container print('4. List Blobs in Container') generator = appendblob_service.list_blobs(container_name) for blob in generator: print('\tBlob Name: ' + blob.name) # Read the blob print('5. Read Append blob') append_blob = appendblob_service.get_blob_to_text( container_name, file_to_upload) print(append_blob.content) # Delete the blob, this can be ommited because the container is deleted print('6. Delete Append Blob') appendblob_service.delete_blob(container_name, file_to_upload) finally: # Delete the container print("7. Delete Container") if appendblob_service.exists(container_name): appendblob_service.delete_container(container_name) # Runs a snapthot sample for Azure Storage Blob service. # Input Arguments: # account - CloudStorageAccount to use for running the samples def basic_snapshot(self, account): blob_name = "blob1" file_to_upload = "HelloWorld.png" # Create a Block Blob Service object blockblob_service = account.create_block_blob_service() container_name = 'blockblobbasicscontainer' + self.random_data.get_random_name( 6) try: # Create a new container print('1. Create a container with name - ' + container_name) blockblob_service.create_container(container_name) # Create blobs print('2. Create a blob') # Get full path on drive to file_to_upload by joining the fully qualified directory name and file name on the local drive full_path_to_file = os.path.join(os.path.dirname(__file__), file_to_upload) blockblob_service.create_blob_from_path(container_name, blob_name, full_path_to_file) # Create a read-only snapshot of the blob print('3. Create a snapshot') snapshot = blockblob_service.snapshot_blob(container_name, blob_name) finally: # Delete the container print("4. Delete Container") if blockblob_service.exists(container_name): blockblob_service.delete_container(container_name)
class BlobAdvancedSamples(): def __init__(self): self.random_data = RandomData() # Runs all samples for Azure Storage Blob service. # Input Arguments: # account - CloudStorageAccount to use for running the samples def run_all_samples(self, account): print('\n\nAzure Storage Blob advanced sample - Starting.') try: print('\n\n* Container operations *\n') self.list_containers(account) print('\n\n* Set CORS *\n') self.set_cors_rules(account) print('\n\n* Container lease *\n') self.lease_container(account) print('\n\n* Copy blob *\n') self.copy_blob(account) print('\n\n* Page blob operations *\n') self.page_blob_operations(account) print('\n\n* Block blob operations *\n') self.block_blob_operations(account) print('\n\n* Properties and Metadata operations *\n') self.properties_and_metadata_operations(account) print('\n\n* Container ACL operations *\n') self.container_acl_operations(account) print('\n\n* Blob lease *\n') self.lease_blob(account) if (config.IS_EMULATED): print('\nShared Access Signature is not supported in emulator') else: print('\n\n* Container with SAS operations *\n') self.container_operations_with_sas(account) print('\n\n* SAS with access policy *\n') self.sas_with_container_access_policy(account) print( '\n\n* Set blob service logging and metrics properties *\n' ) self.set_service_properties(account) except Exception as e: if (config.IS_EMULATED): print( 'Error occurred in the sample. If you are using the emulator, please make sure the emulator is running.', e) else: print( 'Error occurred in the sample. Please make sure the account name and key are correct.', e) finally: print('\nAzure Storage Blob advanced sample - Completed.\n') # Copy a source blob to a destination blob def copy_blob(self, account): file_upload = "HelloWorld.png" container_name = 'blockblobcontainer' + self.random_data.get_random_name( 6) # Create a Block Blob Service object blockblob_service = account.create_block_blob_service() try: # Create a new container print('1. Create a container with name - ' + container_name) blockblob_service.create_container(container_name) # Upload file as a block blob print('2. Upload BlockBlob') #Get full path on drive to file_to_upload by joining the fully qualified directory name and file name on the local drive full_path_to_file = os.path.join(os.path.dirname(__file__), file_upload) blockblob_service.create_blob_from_path(container_name, file_upload, full_path_to_file) target_blob = "target.png" blob_source_url = blockblob_service.make_blob_url( container_name, file_upload) print('3. Copy blob') blockblob_service.copy_blob(container_name, target_blob, blob_source_url) print('4. Get target blob') target_blob_properties = blockblob_service.get_blob_properties( container_name, target_blob) print('5. Get copy properties') copy_properties = target_blob_properties.properties.copy print('Copy properties status: ' + copy_properties.status) if (copy_properties.status == "pending"): print('6. Abort copy') blockblob_service.abort_copy_blob(container_name, blob_name, copy_properties.id) finally: # Delete the container print("7. Delete Container") if blockblob_service.exists(container_name): blockblob_service.delete_container(container_name) def sas_with_container_access_policy(self, account): container_name = 'demosasblobcontainer' + self.random_data.get_random_name( 6) blockblob_service = account.create_block_blob_service() try: print('1. Create a container with name - ' + container_name) blockblob_service.create_container(container_name) print('2. Create blob "blo1" with text') blockblob_service.create_blob_from_text(container_name, 'blob1', b'hello world') print('3. Set access policy for container') # Set access policy on container access_policy = AccessPolicy(permission=ContainerPermissions.READ, expiry=datetime.datetime.utcnow() + datetime.timedelta(hours=1)) identifiers = {'id': access_policy} acl = blockblob_service.set_container_acl(container_name, identifiers) # Wait 30 seconds for acl to propagate print('Wait 30 seconds for acl to propagate') time.sleep(30) print('4. Get sas for access policy in container') # Indicates to use the access policy set on the container sas = blockblob_service.generate_container_shared_access_signature( container_name, id='id') print('5. Create blob service with sas') # Create a service and use the SAS shared_blockblob_service = BlockBlobService( account_name=account.account_name, sas_token=sas, ) print('6. Read blob content with sas') blob = shared_blockblob_service.get_blob_to_text( container_name, 'blob1') content = blob.content # hello world finally: print('7. Delete container') blockblob_service.delete_container(container_name) print("SAS with access policy sample completed") def container_operations_with_sas(self, account): container_name = 'demosasblobcontainer' + self.random_data.get_random_name( 6) # Create a Block Blob Service object blockblob_service = account.create_block_blob_service() # Create a Shared Access Signature for the account print('1.Get account sas') account_sas = blockblob_service.generate_account_shared_access_signature( ResourceTypes.CONTAINER + ResourceTypes.OBJECT, AccountPermissions.READ + AccountPermissions.WRITE + AccountPermissions.DELETE + AccountPermissions.LIST + AccountPermissions.CREATE, datetime.datetime.utcnow() + datetime.timedelta(hours=1)) shared_account = CloudStorageAccount(account_name=account.account_name, sas_token=account_sas) shared_account_block_service = shared_account.create_block_blob_service( ) try: print('2. Create container with account sas. Container name - ' + container_name) shared_account_block_service.create_container(container_name) # For the purposes of the demo, get a Container SAS # In a real-world application, the above Account SAS can be used print('3. Get container sas') container_sas = blockblob_service.generate_container_shared_access_signature( container_name, ContainerPermissions.READ + ContainerPermissions.WRITE + ContainerPermissions.DELETE + ContainerPermissions.LIST, datetime.datetime.utcnow() + datetime.timedelta(hours=1)) shared_container_account = CloudStorageAccount( account_name=account.account_name, sas_token=container_sas) shared_container_block_service = shared_container_account.create_block_blob_service( ) print('4. Create blob with container sas') shared_container_block_service.create_blob_from_text( container_name, 'myblob', 'blob data') print('5. List blobs with container sas') blobs = shared_container_block_service.list_blobs(container_name) for blob in blobs: print('blob ' + blob.name) print('6. Delete blob with container sas') shared_container_block_service.delete_blob(container_name, 'myblob') finally: print('7. Delete container') blockblob_service.delete_container(container_name) print("Containers Sas sample completed") def list_containers(self, account): container_prefix = 'blockblobcontainers' + self.random_data.get_random_name( 6) # Create a Block Blob Service object blockblob_service = account.create_block_blob_service() try: # Create containers for i in range(5): container_name = container_prefix + str(i) print('1. Create a container with name - ' + container_name) blockblob_service.create_container(container_name) # List all the blobs in the container print('2. List containers with prefix ' + container_prefix) containers = blockblob_service.list_containers(container_prefix) for container in containers: print('\tContainer Name: ' + container.name) finally: # Delete the containers print("3. Delete Containers") for i in range(5): container_name = container_prefix + str(i) if blockblob_service.exists(container_name): blockblob_service.delete_container(container_name) print("Containers sample completed") def container_acl_operations(self, account): container_name = 'aclblockblobcontainer' + self.random_data.get_random_name( 6) # Create a Block Blob Service object blockblob_service = account.create_block_blob_service() try: print('1. Create a container with name - ' + container_name) blockblob_service.create_container(container_name) print('2. Set access policy for container') access_policy = AccessPolicy(permission=ContainerPermissions.READ, expiry=datetime.datetime.utcnow() + datetime.timedelta(hours=1)) identifiers = {'id': access_policy} blockblob_service.set_container_acl(container_name, identifiers) print('3. Get access policy from container') acl = blockblob_service.get_container_acl(container_name) print('4. Clear access policy in container') # Clear blockblob_service.set_container_acl(container_name) finally: print('5. Delete container') blockblob_service.delete_container(container_name) print("Container ACL operations sample completed") def properties_and_metadata_operations(self, account): file_blob_name = "HelloWorld.png" text_blob_name = "Text" # Create a Block Blob Service object blockblob_service = account.create_block_blob_service() container_name = 'blockblobbasicscontainer' + self.random_data.get_random_name( 6) try: # Create a new container print('1. Create a container with name and custom metadata - ' + container_name) blockblob_service.create_container(container_name, {'sample': 'azure-storage'}) # Upload file as a block blob print( '2. Uploading BlockBlob from file with properties and custom metadata' ) #Get full path on drive to file_to_upload by joining the fully qualified directory name and file name on the local drive full_path_to_file = os.path.join(os.path.dirname(__file__), file_blob_name) blockblob_service.create_blob_from_path( container_name, file_blob_name, full_path_to_file, content_settings=ContentSettings( content_type='application/png'), metadata={'category': 'azure-samples'}) blockblob_service.create_blob_from_text( container_name, text_blob_name, 'Data', content_settings=ContentSettings(content_encoding='UTF-8', content_language='en'), metadata={ 'origin': 'usa', 'title': 'azure-samples' }) # Get all the container properties print('3. Get Container metadata') container = blockblob_service.get_container_properties( container_name) print(' Metadata:') for key in container.metadata: print(' ' + key + ':' + container.metadata[key]) # Get all the blob properties print('4. Get Blob properties') blob = blockblob_service.get_blob_properties( container_name, file_blob_name) print(' Metadata:') for key in blob.metadata: print(' ' + key + ':' + blob.metadata[key]) print(' Properties:') print(' Content-Type:' + blob.properties.content_settings.content_type) finally: # Delete the container print("5. Delete Container") if blockblob_service.exists(container_name): blockblob_service.delete_container(container_name) # Set CORS def set_cors_rules(self, account): # Create a Block Blob Service object blockblob_service = account.create_block_blob_service() cors_rule = CorsRule(allowed_origins=['*'], allowed_methods=['POST', 'GET'], allowed_headers=['*'], exposed_headers=['*'], max_age_in_seconds=3600) print('1. Get Cors Rules') original_cors_rules = blockblob_service.get_blob_service_properties( ).cors try: print('2. Overwrite Cors Rules') blockblob_service.set_blob_service_properties(cors=[cors_rule]) finally: print('3. Revert Cors Rules back the original ones') #reverting cors rules back to the original ones blockblob_service.set_blob_service_properties( cors=original_cors_rules) print("CORS sample completed") # Lease Container def lease_container(self, account): # Create a Block Blob Service object blockblob_service = account.create_block_blob_service() try: container_name = 'blockblobcontainer' + self.random_data.get_random_name( 6) print('1. Create a container with name - ' + container_name) blockblob_service.create_container(container_name) print('2. Acquire lease on container') lease_id = blockblob_service.acquire_container_lease( container_name, lease_duration=15) print("3. Deleted container without lease") try: blockblob_service.delete_container(container_name) except: print( 'Got expected exception. Cannot delete container, lease not specified' ) finally: print("4. Delete container with lease") blockblob_service.delete_container(container_name, lease_id=lease_id) print("Lease container sample completed") # Lease Blob def lease_blob(self, account): blob_name = "exclusive" # Create an block blob service object blockblob_service = account.create_block_blob_service() container_name = 'blobcontainer' + self.random_data.get_random_name(6) try: # Create a new container print('1. Create a container with name - ' + container_name) blockblob_service.create_container(container_name) # Create a block blob print('2. Create Block Blob') blob = self.random_data.get_random_bytes(255) blockblob_service.create_blob_from_bytes(container_name, blob_name, blob) print('3. Acquire lease on blob') lease_id = blockblob_service.acquire_blob_lease(container_name, blob_name, lease_duration=15) # Write to a block blob print('4. Try to write to Block Blob without lease') block_id = self.random_data.get_random_name(32) block = self.random_data.get_random_bytes(255) try: blockblob_service.put_block(container_name, blob_name, block, block_id) except: print( 'Got expected exception. Cannot write blob, lease not specified' ) print('5. Write to Block Blob with lease') blockblob_service.put_block(container_name, blob_name, block, block_id, lease_id=lease_id) print("6. Deleted blob without lease") try: blockblob_service.delete_blob(container_name, blob_name) except: print( 'Got expected exception. Cannot delete blob, lease not specified' ) print("7. Delete blob with lease") blockblob_service.delete_blob(container_name, blob_name, lease_id=lease_id) finally: print("8. Delete container") if blockblob_service.exists(container_name): blockblob_service.delete_container(container_name) print("Lease blob sample completed") #Page Blob Operations def page_blob_operations(self, account): file_to_upload = "HelloWorld.png" page_size = 1024 # Create an page blob service object pageblob_service = account.create_page_blob_service() container_name = 'pageblobcontainer' + self.random_data.get_random_name( 6) try: # Create a new container print('1. Create a container with name - ' + container_name) pageblob_service.create_container(container_name) # Create a new page blob to upload the file print('2. Create a page blob') pageblob_service.create_blob(container_name, file_to_upload, page_size * 1024) # Read the file print('3. Upload pages to page blob') index = 0 with open(file_to_upload, "rb") as file: file_bytes = file.read(page_size) while len(file_bytes) > 0: if len(file_bytes) < page_size: file_bytes = bytes(file_bytes + bytearray(page_size - len(file_bytes))) pageblob_service.update_page( container_name, file_to_upload, file_bytes, index * page_size, index * page_size + page_size - 1) file_bytes = file.read(page_size) index = index + 1 pages = pageblob_service.get_page_ranges(container_name, file_to_upload) print('4. Enumerate pages in page blob') for page in pages: print('Page ' + str(page.start) + ' - ' + str(page.end)) finally: print('5. Delete container') if pageblob_service.exists(container_name): pageblob_service.delete_container(container_name) #Block Blob Operations def block_blob_operations(self, account): file_to_upload = "HelloWorld.png" block_size = 1024 # Create an page blob service object blockblob_service = account.create_block_blob_service() container_name = 'blockblobcontainer' + self.random_data.get_random_name( 6) try: # Create a new container print('1. Create a container with name - ' + container_name) blockblob_service.create_container(container_name) blocks = [] # Read the file print('2. Upload file to block blob') with open(file_to_upload, "rb") as file: file_bytes = file.read(block_size) while len(file_bytes) > 0: block_id = self.random_data.get_random_name(32) blockblob_service.put_block(container_name, file_to_upload, file_bytes, block_id) blocks.append(BlobBlock(id=block_id)) file_bytes = file.read(block_size) blockblob_service.put_block_list(container_name, file_to_upload, blocks) print('3. Get the block list') blockslist = blockblob_service.get_block_list( container_name, file_to_upload, None, 'all') blocks = blockslist.committed_blocks print('4. Enumerate blocks in block blob') for block in blocks: print('Block ' + block.id) finally: print('5. Delete container') if blockblob_service.exists(container_name): blockblob_service.delete_container(container_name) # Manage properties of the Blob service, including logging and metrics settings, and the default service version. def set_service_properties(self, account): # Create an page blob service object blockblob_service = account.create_block_blob_service() print('1. Get Blob service properties') props = blockblob_service.get_blob_service_properties() retention = RetentionPolicy(enabled=True, days=5) logging = Logging(delete=True, read=False, write=True, retention_policy=retention) hour_metrics = Metrics(enabled=True, include_apis=True, retention_policy=retention) minute_metrics = Metrics(enabled=False) try: print('2. Ovewrite Blob service properties') blockblob_service.set_blob_service_properties( logging=logging, hour_metrics=hour_metrics, minute_metrics=minute_metrics, target_version='2015-04-05') finally: print( '3. Revert Blob service properties back to the original ones') blockblob_service.set_blob_service_properties( logging=props.logging, hour_metrics=props.hour_metrics, minute_metrics=props.minute_metrics, target_version='2015-04-05') print('4. Set Blob service properties completed')
class TableAdvancedSamples(): def __init__(self): self.random_data = RandomData() # Runs all samples for Azure Storage Table service. def run_all_samples(self, account): table_service = account.create_table_service() print('Azure Storage Advanced Table samples - Starting.') print('\n\n* List tables *\n') self.list_tables(table_service) if not account.is_azure_cosmosdb_table(): print('\n\n* Set service properties *\n') self.set_service_properties(table_service) print('\n\n* Set Cors rules *\n') self.set_cors_rules(table_service) print('\n\n* ACL operations *\n') self.table_acl_operations(table_service) if (config.IS_EMULATED): print('\n\n* Shared Access Signature is not supported in emulator *\n') else: print('\n\n* SAS operations *\n') self.table_operations_with_sas(account) print('\nAzure Storage Advanced Table samples - Completed.\n') # Manage tables including creating, listing and deleting def list_tables(self, table_service): table_prefix = 'table' + self.random_data.get_random_name(6) try: # Create tables for i in range(5): table_name = table_prefix + str(i) print('1. Create a table with name - ' + table_name) table_service.create_table(table_name) # List all the tables print('2. List tables') tables = table_service.list_tables() for table in tables: print('\Table Name: ' + table.name) finally: # Delete the tables print("3. Delete Tables") for i in range(5): table_name = table_prefix + str(i) if(table_service.exists(table_name)): table_service.delete_table(table_name) print("List tables sample completed") # Manage properties of the Table service, including logging and metrics settings, and the default service version. def set_service_properties(self, table_service): print('1. Get Table service properties') props = table_service.get_table_service_properties() retention = RetentionPolicy(enabled=True, days=5) logging = Logging(delete=True, read=False, write=True, retention_policy=retention) hour_metrics = Metrics(enabled=True, include_apis=True, retention_policy=retention) minute_metrics = Metrics(enabled=False) try: print('2. Ovewrite Table service properties') table_service.set_table_service_properties(logging=logging, hour_metrics=hour_metrics, minute_metrics=minute_metrics) finally: print('3. Revert Table service properties back to the original ones') table_service.set_table_service_properties(logging=props.logging, hour_metrics=props.hour_metrics, minute_metrics=props.minute_metrics) print('4. Set Table service properties completed') # Manage CORS rules on the table service def set_cors_rules(self, table_service): cors_rule = CorsRule( allowed_origins=['*'], allowed_methods=['POST', 'GET'], allowed_headers=['*'], exposed_headers=['*'], max_age_in_seconds=3600) print('1. Get Cors Rules') original_cors_rules = table_service.get_table_service_properties().cors try: print('2. Overwrite Cors Rules') table_service.set_table_service_properties(cors=[cors_rule]) finally: #reverting cors rules back to the original ones print('3. Revert Cors Rules back the original ones') table_service.set_table_service_properties(cors=original_cors_rules) print("CORS sample completed") # Manage table access policy def table_acl_operations(self, table_service): table_name = 'acltable' + self.random_data.get_random_name(6) try: print('1. Create a table with name - ' + table_name) table_service.create_table(table_name) print('2. Set access policy for table') access_policy = AccessPolicy(permission=TablePermissions.QUERY, expiry=datetime.datetime.utcnow() + datetime.timedelta(hours=1)) identifiers = {'id': access_policy} table_service.set_table_acl(table_name, identifiers) print('3. Wait 30 seconds for acl to propagate') time.sleep(30) print('4. Get access policy from table') acl = table_service.get_table_acl(table_name) print('5. Clear access policy in table') table_service.set_table_acl(table_name) finally: print('5. Delete table') if(table_service.exists(table_name)): table_service.delete_table(table_name) print("Table ACL operations sample completed") # Manage shared access signature on a table def table_operations_with_sas(self, account): table_name = 'sastable' + self.random_data.get_random_name(6) try: # Create a Table Service object table_service = account.create_table_service() print('1. Create table with name - ' + table_name) table_service.create_table(table_name) # Create a Shared Access Signature for the table print('2. Get sas for table') table_sas = table_service.generate_table_shared_access_signature( table_name, TablePermissions.QUERY + TablePermissions.ADD + TablePermissions.UPDATE + TablePermissions.DELETE, datetime.datetime.utcnow() + datetime.timedelta(hours=1)) shared_account = TableStorageAccount(account_name=account.account_name, sas_token=table_sas, endpoint_suffix=account.endpoint_suffix) shared_table_service = shared_account.create_table_service() # Create a sample entity to insert into the table customer = {'PartitionKey': 'Harp', 'RowKey': '1', 'email' : '*****@*****.**', 'phone' : '555-555-5555'} # Insert the entity into the table print('3. Insert new entity into table with sas - ' + table_name) shared_table_service.insert_entity(table_name, customer) # Demonstrate how to query the entity print('4. Read the inserted entity with sas.') entity = shared_table_service.get_entity(table_name, 'Harp', '1') print(entity['email']) print(entity['phone']) # Demonstrate how to update the entity by changing the phone number print('5. Update an existing entity by changing the phone number with sas') customer = {'PartitionKey': 'Harp', 'RowKey': '1', 'email' : '*****@*****.**', 'phone' : '425-123-1234'} shared_table_service.update_entity(table_name, customer) # Demonstrate how to delete an entity print('6. Delete the entity with sas') shared_table_service.delete_entity(table_name, 'Harp', '1') finally: print('7. Delete table') if(table_service.exists(table_name)): table_service.delete_table(table_name) print("Table operations with sas completed")
class FileAdvancedSamples(): def __init__(self): self.random_data = RandomData() # Runs all samples for Azure Storage File service. def run_all_samples(self, connection_string): print('Azure Storage File Advanced samples - Starting.') try: # Create an instance of ShareServiceClient service = ShareServiceClient.from_connection_string( conn_str=connection_string) # List shares print('\n\n* List shares *\n') self.list_shares(service) # Set Cors print('\n\n* Set cors rules *\n') self.set_cors_rules(service) # Set Service Properties print('\n\n* Set service properties *\n') self.set_service_properties(service) # Share, directory and file properties and metadata print('\n\n* Metadata and properties *\n') self.metadata_and_properties(service) except Exception as e: print('Error occurred in the sample.', e) finally: print('\nAzure Storage File Advanced samples - Completed.\n') # List file shares def list_shares(self, service): share_prefix = 'sharesample' + self.random_data.get_random_name(6) try: print('1. Create multiple shares with prefix: ', share_prefix) for i in range(5): service.create_share(share_name=share_prefix + str(i)) print('2. List shares') shares = service.list_shares() for share in shares: print(' Share name:' + share.name) except Exception as e: print(e) finally: print('3. Delete shares with prefix:' + share_prefix) for i in range(5): service.delete_share(share_prefix + str(i)) # Set CORS def set_cors_rules(self, service): print('1. Get Cors Rules') original_cors_rules = service.get_service_properties()['cors'] print('2. Overwrite Cors Rules') cors_rule = CorsRule(allowed_origins=['*'], allowed_methods=['POST', 'GET'], allowed_headers=['*'], exposed_headers=['*'], max_age_in_seconds=3600) try: service.set_service_properties(cors=[cors_rule]) except Exception as e: print(e) finally: #reverting cors rules back to the original ones print('3. Revert Cors Rules back the original ones') service.set_service_properties(cors=original_cors_rules) print("CORS sample completed") # Manage properties of the File service, including logging and metrics settings, and the default service version. def set_service_properties(self, service): print('1. Get File service properties') props = service.get_service_properties() retention = RetentionPolicy(enabled=True, days=5) hour_metrics = Metrics(enabled=True, include_apis=True, retention_policy=retention) minute_metrics = Metrics(enabled=False) try: print('2. Ovewrite File service properties') service.set_service_properties(hour_metrics=hour_metrics, minute_metrics=minute_metrics) finally: print( '3. Revert File service properties back to the original ones') service.set_service_properties( hour_metrics=props['hour_metrics'], minute_metrics=props['minute_metrics']) print('4. Set File service properties completed') # Manage metadata and properties of the share def metadata_and_properties(self, service): share_name = 'sharename' + self.random_data.get_random_name(6) try: # All directories and share must be created in a parent share. # Max capacity: 5TB per share print('1. Create sample share with name ' + share_name) quota = 1 # in GB metadata = {"foo": "bar", "baz": "foo"} share_client = service.create_share(share_name=share_name) print('Sample share "' + share_name + '" created.') print('2. Get share properties.') properties = share_client.get_share_properties() print('3. Get share metadata.') get_metadata = properties['metadata'] for k, v in get_metadata.items(): print("\t" + k + ": " + v) dir_name = 'dirname' + self.random_data.get_random_name(6) print('4. Create sample directory with name ' + dir_name) metadata = {"abc": "def", "jkl": "mno"} directory_client = share_client.create_directory(dir_name, metadata=metadata) print('Sample directory "' + dir_name + '" created.') print('5. Get directory properties.') properties = directory_client.get_directory_properties() print('6. Get directory metadata.') get_metadata = properties['metadata'] for k, v in get_metadata.items(): print("\t" + k + ": " + v) file_name = 'sample.txt' # Uploading text to share_name/dir_name/sample.txt in Azure Files account. # Max capacity: 1TB per file print('7. Upload sample file from text to directory.') metadata = {"prop1": "val1", "prop2": "val2"} file_client = directory_client.get_file_client(file_name) file_client.upload_file('Hello World! - from text sample', metadata=metadata) print('Sample file "' + file_name + '" created and uploaded to: ' + share_name + '/' + dir_name) print('8. Get file properties.') properties = file_client.get_file_properties() print('9. Get file metadata.') get_metadata = properties['metadata'] for k, v in get_metadata.items(): print("\t" + k + ": " + v) # This is for demo purposes, all files will be deleted when share is deleted print('10. Delete file.') file_client.delete_file() # This is for demo purposes, all directories will be deleted when share is deleted print('11. Delete directory.') directory_client.delete_directory() finally: print('12. Delete share.') share_client.delete_share(share_name) print("Metadata and properties sample completed")
class FileBasicSamples(): def __init__(self): self.random_data = RandomData() # Runs all samples for Azure Storage File service. def run_all_samples(self, connection_string): print('Azure Storage File Basis samples - Starting.') #declare variables filename = 'filesample' + self.random_data.get_random_name(6) sharename = 'sharesample' + self.random_data.get_random_name(6) try: # Create an instance of ShareServiceClient service = ShareServiceClient.from_connection_string( conn_str=connection_string) print('\n\n* Basic file operations *\n') self.basic_file_operations(sharename, filename, service) except Exception as e: print('error:' + e) finally: # Delete all Azure Files created in this sample self.file_delete_samples(sharename, filename, service) print('\nAzure Storage File Basic samples - Completed.\n') def basic_file_operations(self, sharename, filename, service): # Creating an SMB file share in your Azure Files account. print( '\nAttempting to create a sample file from text for upload demonstration.' ) # All directories and share must be created in a parent share. # Max capacity: 5TB per share print('Creating sample share.') share_client = service.create_share(share_name=sharename) print('Sample share "' + sharename + '" created.') # Creating an optional file directory in your Azure Files account. print('Creating a sample directory.') # Get the directory client directory_client = share_client.create_directory("mydirectory") print('Sample directory "mydirectory" created.') # Uploading text to sharename/mydirectory/my_text_file in Azure Files account. # Max capacity: 1TB per file print('Uploading a sample file from text.') # create_file_client file_client = directory_client.get_file_client(filename) # Upload a file file_client.upload_file('Hello World! - from text sample') print('Sample file "' + filename + '" created and uploaded to: ' + sharename + '/mydirectory') # Demonstrate how to copy a file print('\nCopying file ' + filename) # Create another file client which will copy the file from url destination_file_client = share_client.get_file_client('file1copy') # Copy the sample source file from the url to the destination file copy_resp = destination_file_client.start_copy_from_url( source_url=file_client.url) if copy_resp['copy_status'] == 'pending': # Demonstrate how to abort a copy operation (just for demo, probably will never get here) print('Abort copy operation') destination_file.abort_copy() else: print('Copy was a ' + copy_resp['copy_status']) # Demonstrate how to create a share and upload a file from a local temporary file path print( '\nAttempting to upload a sample file from path for upload demonstration.' ) # Creating a temporary file to upload to Azure Files print('Creating a temporary file from text.') with tempfile.NamedTemporaryFile(delete=False) as my_temp_file: my_temp_file.file.write(b"Hello world!") print('Sample temporary file created.') # Uploading my_temp_file to sharename folder in Azure Files # Max capacity: 1TB per file print('Uploading a sample file from local path.') # Create file_client file_client = share_client.get_file_client(filename) # Upload a file with open(my_temp_file.name, "rb") as source_file: file_client.upload_file(source_file) print('Sample file "' + filename + '" uploaded from path to share: ' + sharename) # Close the temp file my_temp_file.close() # Get the list of valid ranges and write to the specified range print('\nGet list of valid ranges of the file.') file_ranges = file_client.get_ranges() data = b'abcdefghijkl' print('Put a range of data to the file.') file_client.upload_range(data=data, offset=file_ranges[0]['start'], length=len(data)) # Demonstrate how to download a file from Azure Files # The following example download the file that was previously uploaded to Azure Files print( '\nAttempting to download a sample file from Azure files for demonstration.' ) destination_file = tempfile.tempdir + '\mypathfile.txt' with open(destination_file, "wb") as file_handle: data = file_client.download_file() data.readinto(file_handle) print('Sample file downloaded to: ' + destination_file) # Demonstrate how to list files and directories contains under Azure File share print( '\nAttempting to list files and directories directory under share "' + sharename + '":') # Create a generator to list directories and files under share # This is not a recursive listing operation generator = share_client.list_directories_and_files() # Prints the directories and files under the share for file_or_dir in generator: print(file_or_dir['name']) # remove temp file os.remove(my_temp_file.name) print('Files and directories under share "' + sharename + '" listed.') print('\nCompleted successfully - Azure basic Files operations.') # Demonstrate how to delete azure files created for this demonstration # Warning: Deleting a share or directory will also delete all files and directories that are contained in it. def file_delete_samples(self, sharename, filename, service): print('\nDeleting all samples created for this demonstration.') try: # Deleting file: 'sharename/mydirectory/filename' # This is for demo purposes only, it's unnecessary, as we're deleting the share later print('Deleting a sample file.') share_client = service.get_share_client(sharename) directory_client = share_client.get_directory_client('mydirectory') directory_client.delete_file(file_name=filename) print('Sample file "' + filename + '" deleted from: ' + sharename + '/mydirectory') # Deleting directory: 'sharename/mydirectory' print( 'Deleting sample directory and all files and directories under it.' ) share_client.delete_directory('mydirectory') print('Sample directory "/mydirectory" deleted from: ' + sharename) # Deleting share: 'sharename' print('Deleting sample share ' + sharename + ' and all files and directories under it.') share_client.delete_share(sharename) print('Sample share "' + sharename + '" deleted.') print('\nCompleted successfully - Azure Files samples deleted.') except Exception as e: print('********ErrorDelete***********') print(e)
class TableBasicSamples(): def __init__(self): self.random_data = RandomData() # Runs all samples for Azure Storage Table service. def run_all_samples(self, account): print('Azure Storage Basic Table samples - Starting.') table_name = 'tablebasics' + self.random_data.get_random_name(6) table_service = None try: table_service = account.create_table_service() # Create a new table print('Create a table with name - ' + table_name) try: table_service.create_table(table_name) except Exception as err: print('Error creating table, ' + table_name + 'check if it already exists') # Create a sample entity to insert into the table customer = {'PartitionKey': 'Harp', 'RowKey': '1', 'email' : '*****@*****.**', 'phone' : '555-555-5555'} # Insert the entity into the table print('Inserting a new entity into table - ' + table_name) table_service.insert_entity(table_name, customer) print('Successfully inserted the new entity') # Demonstrate how to query the entity print('Read the inserted entity.') entity = table_service.get_entity(table_name, 'Harp', '1') print(entity['email']) print(entity['phone']) # Demonstrate how to update the entity by changing the phone number print('Update an existing entity by changing the phone number') customer = {'PartitionKey': 'Harp', 'RowKey': '1', 'email' : '*****@*****.**', 'phone' : '425-123-1234'} table_service.update_entity(table_name, customer) # Demonstrate how to query the updated entity, filter the results with a filter query and select only the value in the phone column print('Read the updated entity with a filter query') entities = table_service.query_entities(table_name, filter="PartitionKey eq 'Harp'", select='phone') for entity in entities: print(entity['phone']) # Demonstrate how to delete an entity print('Delete the entity') table_service.delete_entity(table_name, 'Harp', '1') print('Successfully deleted the entity') except Exception as e: if (config.IS_EMULATED): print('Error occurred in the sample. If you are using the emulator, please make sure the emulator is running.', e) else: print('Error occurred in the sample. Please make sure the account name and key are correct.', e) finally: # Demonstrate deleting the table, if you don't want to have the table deleted comment the below block of code print('Deleting the table.') if(table_service.exists(table_name)): table_service.delete_table(table_name) print('Successfully deleted the table') print('\nAzure Storage Basic Table samples - Completed.\n')