def test_create_snapshot_with_metadata(self): # Arrange share = self._get_share_reference() metadata = {"test1": "foo", "test2": "bar"} metadata2 = {"test100": "foo100", "test200": "bar200"} # Act created = share.create_share(metadata=metadata) snapshot = share.create_snapshot(metadata=metadata2) share_props = share.get_share_properties() snapshot_client = ShareClient( self.get_file_url(), share_name=share.share_name, snapshot=snapshot, credential=self.settings.STORAGE_ACCOUNT_KEY) snapshot_props = snapshot_client.get_share_properties() # Assert self.assertTrue(created) self.assertIsNotNone(snapshot['snapshot']) self.assertIsNotNone(snapshot['etag']) self.assertIsNotNone(snapshot['last_modified']) self.assertEqual(share_props.metadata, metadata) self.assertEqual(snapshot_props.metadata, metadata2) self._delete_shares(share.share_name)
def test_upload_file_to_share(self): # Instantiate the ShareClient from a connection string from azure.storage.file import ShareClient share = ShareClient.from_connection_string(self.connection_string, share_name="share") # Create the share share.create_share() try: # Instantiate the FileClient from a connection string # [START create_file_client] from azure.storage.file import FileClient file = FileClient.from_connection_string(self.connection_string, share_name="share", file_path="myfile") # [END create_file_client] # Upload a file with open(SOURCE_FILE, "rb") as source_file: file.upload_file(source_file) finally: # Delete the share share.delete_share()
def test_set_share_quota_and_metadata(self): # [START create_share_client_from_conn_string] from azure.storage.file import ShareClient share = ShareClient.from_connection_string(self.connection_string, "fileshare") # [END create_share_client_from_conn_string] # Create the share share.create_share() try: # [START set_share_quota] # Set the quota for the share to 1GB share.set_share_quota(quota=1) # [END set_share_quota] # [START set_share_metadata] data = {'category': 'test'} share.set_share_metadata(metadata=data) # [END set_share_metadata] # Get the metadata for the share props = share.get_share_properties().metadata assert props == data finally: # Delete the share share.delete_share()
def test_copy_from_url(self): # Instantiate the ShareClient from a connection string from azure.storage.file import ShareClient share = ShareClient.from_connection_string(self.connection_string, "filesfromurl") # Create the share share.create_share() try: # Get a file client and upload a file source_file = share.get_file_client("sourcefile") with open(SOURCE_FILE, "rb") as source: source_file.upload_file(source) # Create another file client which will copy the file from url destination_file = share.get_file_client("destfile") # Build the url from which to copy the file source_url = "{}://{}.file.core.windows.net/{}/{}".format( settings.PROTOCOL, settings.STORAGE_ACCOUNT_NAME, "filesfromurl", "sourcefile" ) # Copy the sample source file from the url to the destination file # [START copy_file_from_url] destination_file.start_copy_from_url(source_url=source_url) # [END copy_file_from_url] finally: # Delete the share share.delete_share()
def test_create_directory(self): # Instantiate the ShareClient from a connection string from azure.storage.file import ShareClient share = ShareClient.from_connection_string(self.connection_string, "dirshare") # Create the share share.create_share() try: # Get the directory client dir = share.get_directory_client(directory_path="mydirectory") # [START create_directory] dir.create_directory() # [END create_directory] # [START upload_file_to_directory] # Upload a file to the directory with open(SOURCE_FILE, "rb") as source: dir.upload_file(file_name="sample", data=source) # [END upload_file_to_directory] # [START delete_file_in_directory] # Delete the file in the directory dir.delete_file(file_name="sample") # [END delete_file_in_directory] # [START delete_directory] dir.delete_directory() # [END delete_directory] finally: # Delete the share share.delete_share()
def test_get_directory_or_file_client(self): # Instantiate the ShareClient from a connection string from azure.storage.file import ShareClient share = ShareClient.from_connection_string(self.connection_string, "testfiles") # Get the directory client to interact with a specific directory my_dir = share.get_directory_client("dir1") # Get the file client to interact with a specific file my_file = share.get_file_client("dir1/myfile")
def test_delete_snapshot(self): # Arrange share = self._get_share_reference() share.create_share() snapshot = share.create_snapshot() # Act with self.assertRaises(HttpResponseError): share.delete_share() snapshot_client = ShareClient( self.get_file_url(), share_name=share.share_name, snapshot=snapshot, credential=self.settings.STORAGE_ACCOUNT_KEY) deleted = snapshot_client.delete_share() self.assertIsNone(deleted) self._delete_shares()
def test_create_share_snapshot(self): # Instantiate the ShareClient from a connection string from azure.storage.file import ShareClient share = ShareClient.from_connection_string(self.connection_string, "sharesnapshot") # [START create_share] share.create_share() # [END create_share] try: # [START create_share_snapshot] share.create_snapshot() # [END create_share_snapshot] finally: # [START delete_share] share.delete_share(delete_snapshots=True)
def test_create_file_share(self): # Instantiate the ShareClient from a connection string from azure.storage.file import ShareClient share = ShareClient.from_connection_string(self.connection_string, share_name="myshare") # Create the share share.create_share() try: # [START get_share_properties] properties = share.get_share_properties() # [END get_share_properties] assert properties is not None finally: # Delete the share share.delete_share()
def test_create_subdirectories(self): # Instantiate the ShareClient from a connection string from azure.storage.file import ShareClient share = ShareClient.from_connection_string(self.connection_string, "subdirshare") # Create the share share.create_share() try: # Get the directory client parent_dir = share.get_directory_client(directory_path="parentdir") # [START create_subdirectory] # Create the directory parent_dir.create_directory() # Create a subdirectory subdir = parent_dir.create_subdirectory("subdir") # [END create_subdirectory] # Upload a file to the parent directory with open(SOURCE_FILE, "rb") as source: parent_dir.upload_file(file_name="sample", data=source) # Upload a file to the subdirectory with open(SOURCE_FILE, "rb") as source: subdir.upload_file(file_name="sample", data=source) # [START lists_directory] # List the directories and files under the parent directory my_list = list(parent_dir.list_directories_and_files()) print(my_list) # [END lists_directory] # You must delete the file in the subdirectory before deleting the subdirectory subdir.delete_file("sample") # [START delete_subdirectory] parent_dir.delete_subdirectory("subdir") # [END delete_subdirectory] finally: # Delete the share share.delete_share()
def test_get_subdirectory_client(self): # Instantiate the ShareClient from a connection string from azure.storage.file import ShareClient share = ShareClient.from_connection_string(self.connection_string, "dirtest") # Create the share share.create_share() try: # [START get_subdirectory_client] # Get a directory client and create the directory parent = share.get_directory_client("dir1") parent.create_directory() # Get a subdirectory client and create the subdirectory "dir1/dir2" subdirectory = parent.get_subdirectory_client("dir2") subdirectory.create_directory() # [END get_subdirectory_client] finally: # Delete the share share.delete_share()
def test_file_operations(self): # Instantiate the ShareClient from a connection string from azure.storage.file import ShareClient share = ShareClient.from_connection_string(self.connection_string, "filesshare") # Create the share share.create_share() try: # Get a file client file = share.get_file_client("myfile") file2 = share.get_file_client("myfile2") # [START create_file] # Create and allocate bytes for the file (no content added yet) file.create_file(size=100) # [END create_file] # Or upload a file directly # [START upload_file] with open(SOURCE_FILE, "rb") as source: file2.upload_file(source) # [END upload_file] # Download the file # [START download_file] with open(DEST_FILE, "wb") as data: stream = file2.download_file() data.write(stream.readall()) # [END download_file] # Delete the files file.delete_file() # [START delete_file] file2.delete_file() # [END delete_file] finally: # Delete the share share.delete_share()
def test_list_directories_and_files(self): # Instantiate the ShareClient from a connection string from azure.storage.file import ShareClient share = ShareClient.from_connection_string(self.connection_string, "listshare") # Create the share share.create_share() try: # [START share_list_files_in_dir] # Create a directory in the share dir_client = share.create_directory("mydir") # Upload a file to the directory with open(SOURCE_FILE, "rb") as source_file: dir_client.upload_file(file_name="sample", data=source_file) # List files in the directory my_files = list(share.list_directories_and_files(directory_name="mydir")) print(my_files) # [END share_list_files_in_dir] finally: # Delete the share share.delete_share()