diff --git a/sdk/storage/azure-storage-file/README.md b/sdk/storage/azure-storage-file/README.md index 05ab4d03aebf..067ba86f79fc 100644 --- a/sdk/storage/azure-storage-file/README.md +++ b/sdk/storage/azure-storage-file/README.md @@ -126,6 +126,87 @@ with open("./SampleSource.txt", "rb") as source_file: file_client.upload_file(source_file) ``` +### Download a file +Download a file to the share + +```python +from azure.storage.file import FileClient + +file_client = FileClient.from_connection_string("my_connection_string", share="share", file_path="myfile") + +with open("DEST_FILE", "wb") as data: + file_client.download_file(data) +``` + +### List contents of a directory. +Lists all the directories and files under the directory. + +```python +from azure.storage.file import ShareClient +share = ShareClient.from_connection_string(self.connection_string, "subdirshare") +parent_dir = share.get_directory_client(directory_path="parentdir") + +my_list = list(parent_dir.list_directories_and_files()) +print(my_list) +``` + +### Client creation with a connection string +Create the FileServiceClient using the connection string to your Azure Storage account. + +```python +from azure.storage.file.aio import FileServiceClient + +service = FileServiceClient.from_connection_string("my_connection_string") +``` + +### Create a file share asynchronously +Create a file share to store your files. + +```python +from azure.storage.file.aio import ShareClient + +share = ShareClient.from_connection_string("my_connection_string", share="myshare") +await share.create_share() +``` + +### Upload a file asynchronously +Upload a file to the share + +```python +from azure.storage.file.aio import FileClient + +file_client = FileClient.from_connection_string("my_connection_string", share="share", file_path="myfile") + +with open("./SampleSource.txt", "rb") as source_file: + await file_client.upload_file(source_file) +``` + +### Download a file asynchronously +Download a file to the share + +```python +from azure.storage.file.aio import FileClient + +file_client = FileClient.from_connection_string("my_connection_string", share="share", file_path="myfile") + +with open("DEST_FILE", "wb") as data: + await file_client.download_file(data) +``` + +### List contents of a directory asynchronously +Lists all the directories and files under the directory. + +```python +from azure.storage.file import ShareClient +share = ShareClient.from_connection_string(self.connection_string, "subdirshare") +parent_dir = share.get_directory_client(directory_path="parentdir") + +my_files = [] +async for item in parent_dir.list_directories_and_files(): + my_list.append(item) +print(my_list) +``` + ## Troubleshooting Storage File clients raise exceptions defined in [Azure Core](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core/docs/exceptions.md). @@ -139,38 +220,44 @@ Get started with our [File samples](https://github.com/Azure/azure-sdk-for-pytho Several Storage File Python SDK samples are available to you in the SDK's GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with Storage File: * [`test_file_samples_hello_world.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_hello_world.py) - Examples found in this article: +* [`test_file_samples_hello_world.py`]([async version]https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_hello_world_async.py) - Examples found in this article: * Client creation * Create a file share * Upload a file * [`test_file_samples_authentication.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_authentication.py) - Examples for authenticating and creating the client: +* [`test_file_samples_authentication.py`]([async version]https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_authentication_async.py) - Examples for authenticating and creating the client: * From a connection string * From a shared access key * From a shared access signature token * [`test_file_samples_service.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_service.py) - Examples for interacting with the file service: +* [`test_file_samples_service.py`]([async version]https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_service_async.py) - Examples for interacting with the file service: * Get and set service properties * Create, list, and delete shares * Get a share client * [`test_file_samples_share.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_share.py) - Examples for interacting with file shares: +* [`test_file_samples_share.py`]([async version]https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_share_async.py) - Examples for interacting with file shares: * Create a share snapshot * Set share quota and metadata * List directories and files * Get the directory or file client to interact with a specific entity * [`test_file_samples_directory.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_directory.py) - Examples for interacting with directories: +* [`test_file_samples_directory.py`]([async version]https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_directory_async.py) - Examples for interacting with directories: * Create a directory and add files * Create and delete subdirectories * Get the subdirectory client * [`test_file_samples_file.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_file.py) - Examples for interacting with files: +* [`test_file_samples_file.py`]([async version]https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_file_async.py) - Examples for interacting with files: * Create, upload, download, and delete files * Copy a file from a URL ### Additional documentation -For more extensive documentation on the Azure Storage File, see the [Azure Storage File documentation](https://docs.microsoft.com/azure/storage/) on docs.microsoft.com. +For more extensive documentation on the Azure Storage File, see the [Azure Storage File documentation](https://azure.github.io/azure-sdk-for-python/ref/azure.storage.file) on docs.microsoft.com. ## Contributing diff --git a/sdk/storage/azure-storage-file/azure/storage/file/aio/directory_client_async.py b/sdk/storage/azure-storage-file/azure/storage/file/aio/directory_client_async.py index 1618293e17a3..deab7f3507a3 100644 --- a/sdk/storage/azure-storage-file/azure/storage/file/aio/directory_client_async.py +++ b/sdk/storage/azure-storage-file/azure/storage/file/aio/directory_client_async.py @@ -120,10 +120,10 @@ def get_subdirectory_client(self, directory_name, **kwargs): :param str directory_name: The name of the subdirectory. :returns: A Directory Client. - :rtype: ~azure.storage.file.directory_client.DirectoryClient + :rtype: ~azure.storage.file.aio.directory_client_async.DirectoryClient Example: - .. literalinclude:: ../tests/test_file_samples_directory.py + .. literalinclude:: ../tests/test_file_samples_directory_async.py :start-after: [START get_subdirectory_client] :end-before: [END get_subdirectory_client] :language: python @@ -154,7 +154,7 @@ async def create_directory( # type: ignore :rtype: dict(str, Any) Example: - .. literalinclude:: ../tests/test_file_samples_directory.py + .. literalinclude:: ../tests/test_file_samples_directory_async.py :start-after: [START create_directory] :end-before: [END create_directory] :language: python @@ -183,7 +183,7 @@ async def delete_directory(self, timeout=None, **kwargs): :rtype: None Example: - .. literalinclude:: ../tests/test_file_samples_directory.py + .. literalinclude:: ../tests/test_file_samples_directory_async.py :start-after: [START delete_directory] :end-before: [END delete_directory] :language: python @@ -209,7 +209,7 @@ def list_directories_and_files(self, name_starts_with=None, timeout=None, **kwar :rtype: ~azure.core.async_paging.AsyncItemPaged[~azure.storage.file.models.DirectoryProperties] Example: - .. literalinclude:: ../tests/test_file_samples_directory.py + .. literalinclude:: ../tests/test_file_samples_directory_async.py :start-after: [START lists_directory] :end-before: [END lists_directory] :language: python @@ -366,10 +366,10 @@ async def create_subdirectory( :param int timeout: The timeout parameter is expressed in seconds. :returns: DirectoryClient - :rtype: ~azure.storage.file.directory_client.DirectoryClient + :rtype: ~azure.storage.file.aio.directory_client_async.DirectoryClient Example: - .. literalinclude:: ../tests/test_file_samples_directory.py + .. literalinclude:: ../tests/test_file_samples_directory_async.py :start-after: [START create_subdirectory] :end-before: [END create_subdirectory] :language: python @@ -396,7 +396,7 @@ async def delete_subdirectory( :rtype: None Example: - .. literalinclude:: ../tests/test_file_samples_directory.py + .. literalinclude:: ../tests/test_file_samples_directory_async.py :start-after: [START delete_subdirectory] :end-before: [END delete_subdirectory] :language: python @@ -448,10 +448,10 @@ async def upload_file( :param str encoding: Defaults to UTF-8. :returns: FileClient - :rtype: ~azure.storage.file.file_client.FileClient + :rtype: ~azure.storage.file.aio.file_client_async.FileClient Example: - .. literalinclude:: ../tests/test_file_samples_directory.py + .. literalinclude:: ../tests/test_file_samples_directory_async.py :start-after: [START upload_file_to_directory] :end-before: [END upload_file_to_directory] :language: python @@ -488,7 +488,7 @@ async def delete_file( :rtype: None Example: - .. literalinclude:: ../tests/test_file_samples_directory.py + .. literalinclude:: ../tests/test_file_samples_directory_async.py :start-after: [START delete_file_in_directory] :end-before: [END delete_file_in_directory] :language: python diff --git a/sdk/storage/azure-storage-file/azure/storage/file/aio/file_client_async.py b/sdk/storage/azure-storage-file/azure/storage/file/aio/file_client_async.py index dd5f2101e612..0795ad55fe6b 100644 --- a/sdk/storage/azure-storage-file/azure/storage/file/aio/file_client_async.py +++ b/sdk/storage/azure-storage-file/azure/storage/file/aio/file_client_async.py @@ -153,7 +153,7 @@ async def create_file( # type: ignore :rtype: dict(str, Any) Example: - .. literalinclude:: ../tests/test_file_samples_file.py + .. literalinclude:: ../tests/test_file_samples_file_async.py :start-after: [START create_file] :end-before: [END create_file] :language: python @@ -230,7 +230,7 @@ async def upload_file( :rtype: dict(str, Any) Example: - .. literalinclude:: ../tests/test_file_samples_file.py + .. literalinclude:: ../tests/test_file_samples_file_async.py :start-after: [START upload_file] :end-before: [END upload_file] :language: python @@ -293,7 +293,7 @@ async def start_copy_from_url( :rtype: dict(str, Any) Example: - .. literalinclude:: ../tests/test_file_samples_file.py + .. literalinclude:: ../tests/test_file_samples_file_async.py :start-after: [START copy_file_from_url] :end-before: [END copy_file_from_url] :language: python @@ -368,7 +368,7 @@ async def download_file( :returns: A iterable data generator (stream) Example: - .. literalinclude:: ../tests/test_file_samples_file.py + .. literalinclude:: ../tests/test_file_samples_file_async.py :start-after: [START download_file] :end-before: [END download_file] :language: python @@ -407,7 +407,7 @@ async def delete_file(self, timeout=None, **kwargs): :rtype: None Example: - .. literalinclude:: ../tests/test_file_samples_file.py + .. literalinclude:: ../tests/test_file_samples_file_async.py :start-after: [START delete_file] :end-before: [END delete_file] :language: python diff --git a/sdk/storage/azure-storage-file/azure/storage/file/aio/file_service_client_async.py b/sdk/storage/azure-storage-file/azure/storage/file/aio/file_service_client_async.py index c10e9c2cc557..9d94ce9f6156 100644 --- a/sdk/storage/azure-storage-file/azure/storage/file/aio/file_service_client_async.py +++ b/sdk/storage/azure-storage-file/azure/storage/file/aio/file_service_client_async.py @@ -66,7 +66,7 @@ class FileServiceClient(AsyncStorageAccountHostsMixin, FileServiceClientBase): shared access key. Example: - .. literalinclude:: ../tests/test_file_samples_authentication.py + .. literalinclude:: ../tests/test_file_samples_authentication_async.py :start-after: [START create_file_service_client] :end-before: [END create_file_service_client] :language: python @@ -100,7 +100,7 @@ async def get_service_properties(self, timeout=None, **kwargs): :rtype: ~azure.storage.file._generated.models.StorageServiceProperties Example: - .. literalinclude:: ../tests/test_file_samples_service.py + .. literalinclude:: ../tests/test_file_samples_service_async.py :start-after: [START get_service_properties] :end-before: [END get_service_properties] :language: python @@ -143,7 +143,7 @@ async def set_service_properties( :rtype: None Example: - .. literalinclude:: ../tests/test_file_samples_service.py + .. literalinclude:: ../tests/test_file_samples_service_async.py :start-after: [START set_service_properties] :end-before: [END set_service_properties] :language: python @@ -185,7 +185,7 @@ def list_shares( :rtype: ~azure.core.async_paging.AsyncItemPaged[~azure.storage.file.models.ShareProperties] Example: - .. literalinclude:: ../tests/test_file_samples_service.py + .. literalinclude:: ../tests/test_file_samples_service_async.py :start-after: [START fsc_list_shares] :end-before: [END fsc_list_shares] :language: python @@ -229,10 +229,10 @@ async def create_share( Quota in bytes. :param int timeout: The timeout parameter is expressed in seconds. - :rtype: ~azure.storage.file.share_client.ShareClient + :rtype: ~azure.storage.file.aio.share_client_async.ShareClient Example: - .. literalinclude:: ../tests/test_file_samples_service.py + .. literalinclude:: ../tests/test_file_samples_service_async.py :start-after: [START fsc_create_shares] :end-before: [END fsc_create_shares] :language: python @@ -265,7 +265,7 @@ async def delete_share( :rtype: None Example: - .. literalinclude:: ../tests/test_file_samples_service.py + .. literalinclude:: ../tests/test_file_samples_service_async.py :start-after: [START fsc_delete_shares] :end-before: [END fsc_delete_shares] :language: python @@ -288,10 +288,10 @@ def get_share_client(self, share, snapshot=None): :param str snapshot: An optional share snapshot on which to operate. :returns: A ShareClient. - :rtype: ~azure.storage.file.share_client.ShareClient + :rtype: ~azure.storage.file.aio.share_client_async.ShareClient Example: - .. literalinclude:: ../tests/test_file_samples_service.py + .. literalinclude:: ../tests/test_file_samples_service_async.py :start-after: [START get_share_client] :end-before: [END get_share_client] :language: python diff --git a/sdk/storage/azure-storage-file/azure/storage/file/aio/share_client_async.py b/sdk/storage/azure-storage-file/azure/storage/file/aio/share_client_async.py index 1fe40bb5b0cf..29728d584bf6 100644 --- a/sdk/storage/azure-storage-file/azure/storage/file/aio/share_client_async.py +++ b/sdk/storage/azure-storage-file/azure/storage/file/aio/share_client_async.py @@ -95,7 +95,7 @@ def get_directory_client(self, directory_path=None): :param str directory_path: Path to the specified directory. :returns: A Directory Client. - :rtype: ~azure.storage.file.directory_client.DirectoryClient + :rtype: ~azure.storage.file.aio.directory_client_async.DirectoryClient """ return DirectoryClient( self.url, directory_path=directory_path or "", snapshot=self.snapshot, credential=self.credential, @@ -109,7 +109,7 @@ def get_file_client(self, file_path): :param str file_path: Path to the specified file. :returns: A File Client. - :rtype: ~azure.storage.file.file_client.FileClient + :rtype: ~azure.storage.file.aio.file_client_async.FileClient """ return FileClient( self.url, file_path=file_path, snapshot=self.snapshot, credential=self.credential, _hosts=self._hosts, @@ -137,7 +137,7 @@ async def create_share( # type: ignore :rtype: dict(str, Any) Example: - .. literalinclude:: ../tests/test_file_samples_share.py + .. literalinclude:: ../tests/test_file_samples_share_async.py :start-after: [START create_share] :end-before: [END create_share] :language: python @@ -184,7 +184,7 @@ async def create_snapshot( # type: ignore :rtype: dict[str, Any] Example: - .. literalinclude:: ../tests/test_file_samples_share.py + .. literalinclude:: ../tests/test_file_samples_share_async.py :start-after: [START create_share_snapshot] :end-before: [END create_share_snapshot] :language: python @@ -219,7 +219,7 @@ async def delete_share( :rtype: None Example: - .. literalinclude:: ../tests/test_file_samples_share.py + .. literalinclude:: ../tests/test_file_samples_share_async.py :start-after: [START delete_share] :end-before: [END delete_share] :language: python @@ -251,7 +251,7 @@ async def get_share_properties(self, timeout=None, **kwargs): :rtype: ~azure.storage.file.models.ShareProperties Example: - .. literalinclude:: ../tests/test_file_samples_hello_world.py + .. literalinclude:: ../tests/test_file_samples_hello_world_async.py :start-after: [START get_share_properties] :end-before: [END get_share_properties] :language: python @@ -284,7 +284,7 @@ async def set_share_quota(self, quota, timeout=None, **kwargs): # type: ignore :rtype: dict(str, Any) Example: - .. literalinclude:: ../tests/test_file_samples_share.py + .. literalinclude:: ../tests/test_file_samples_share_async.py :start-after: [START set_share_quota] :end-before: [END set_share_quota] :language: python @@ -318,7 +318,7 @@ async def set_share_metadata(self, metadata, timeout=None, **kwargs): # type: ig :rtype: dict(str, Any) Example: - .. literalinclude:: ../tests/test_file_samples_share.py + .. literalinclude:: ../tests/test_file_samples_share_async.py :start-after: [START set_share_metadata] :end-before: [END set_share_metadata] :language: python @@ -444,7 +444,7 @@ def list_directories_and_files( # type: ignore :returns: An auto-paging iterable of dict-like DirectoryProperties and FileProperties Example: - .. literalinclude:: ../tests/test_file_samples_share.py + .. literalinclude:: ../tests/test_file_samples_share_async.py :start-after: [START share_list_files_in_dir] :end-before: [END share_list_files_in_dir] :language: python @@ -469,7 +469,7 @@ async def create_directory(self, directory_name, metadata=None, timeout=None, ** :param int timeout: The timeout parameter is expressed in seconds. :returns: DirectoryClient - :rtype: ~azure.storage.file.directory_client.DirectoryClient + :rtype: ~azure.storage.file.aio.directory_client_async.DirectoryClient """ directory = self.get_directory_client(directory_name) await directory.create_directory(metadata, timeout, **kwargs) diff --git a/sdk/storage/azure-storage-file/tests/test_file_samples_authentication_async.py b/sdk/storage/azure-storage-file/tests/test_file_samples_authentication_async.py new file mode 100644 index 000000000000..ab78f5fad071 --- /dev/null +++ b/sdk/storage/azure-storage-file/tests/test_file_samples_authentication_async.py @@ -0,0 +1,90 @@ +# coding: utf-8 + +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +import asyncio +from datetime import datetime, timedelta + +try: + import settings_real as settings +except ImportError: + import file_settings_fake as settings + +from filetestcase import ( + FileTestCase, + TestMode, + record +) + + +class TestFileAuthSamples(FileTestCase): + url = "{}://{}.file.core.windows.net".format( + settings.PROTOCOL, + settings.STORAGE_ACCOUNT_NAME + ) + + connection_string = settings.CONNECTION_STRING + shared_access_key = settings.STORAGE_ACCOUNT_KEY + + async def _test_auth_connection_string(self): + # Instantiate the FileServiceClient from a connection string + # [START create_file_service_client_from_conn_string] + from azure.storage.file.aio import FileServiceClient + file_service = FileServiceClient.from_connection_string(self.connection_string) + # [END create_file_service_client_from_conn_string] + + # Get queue service properties + properties = await file_service.get_service_properties() + assert properties is not None + + def test_auth_connection_string(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_auth_connection_string()) + + async def _test_auth_shared_key(self): + # Instantiate a FileServiceClient using a shared access key + # [START create_file_service_client] + from azure.storage.file.aio import FileServiceClient + file_service_client = FileServiceClient(account_url=self.url, credential=self.shared_access_key) + # [END create_file_service_client] + + # Get account information for the File Service + account_info = await file_service_client.get_service_properties() + assert account_info is not None + + def test_auth_shared_key(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_auth_shared_key()) + + async def _test_auth_shared_access_signature(self): + # SAS URL is calculated from storage key, so this test runs live only + if TestMode.need_recording_file(self.test_mode): + return + + # Instantiate a FileServiceClient using a connection string + from azure.storage.file.aio import FileServiceClient + file_service_client = FileServiceClient.from_connection_string(self.connection_string) + + # Create a SAS token to use to authenticate a new client + # [START generate_sas_token] + sas_token = file_service_client.generate_shared_access_signature( + resource_types="object", + permission="read", + expiry=datetime.utcnow() + timedelta(hours=1) + ) + # [END generate_sas_token] + assert sas_token is not None + + def test_auth_shared_access_signature(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_auth_shared_access_signature()) diff --git a/sdk/storage/azure-storage-file/tests/test_file_samples_directory_async.py b/sdk/storage/azure-storage-file/tests/test_file_samples_directory_async.py new file mode 100644 index 000000000000..f759ef672970 --- /dev/null +++ b/sdk/storage/azure-storage-file/tests/test_file_samples_directory_async.py @@ -0,0 +1,166 @@ +# coding: utf-8 + +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +import os +import asyncio +try: + import settings_real as settings +except ImportError: + import file_settings_fake as settings + +from filetestcase import ( + FileTestCase, + TestMode, + record +) + +SOURCE_FILE = 'SampleSource.txt' + + +class TestDirectorySamples(FileTestCase): + + connection_string = settings.CONNECTION_STRING + + def setUp(self): + data = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit" + with open(SOURCE_FILE, 'wb') as stream: + stream.write(data) + + super(TestDirectorySamples, self).setUp() + + def tearDown(self): + if os.path.isfile(SOURCE_FILE): + try: + os.remove(SOURCE_FILE) + except: + pass + + return super(TestDirectorySamples, self).tearDown() + + #--Begin File Samples----------------------------------------------------------------- + + async def _test_create_directory(self): + # Instantiate the ShareClient from a connection string + from azure.storage.file.aio import ShareClient + share = ShareClient.from_connection_string(self.connection_string, "dirshare") + + # Create the share + await share.create_share() + + try: + # Get the directory client + directory = share.get_directory_client(directory_path="mydirectory") + + # [START create_directory] + await directory.create_directory() + # [END create_directory] + + # [START upload_file_to_directory] + # Upload a file to the directory + with open(SOURCE_FILE, "rb") as source: + await directory.upload_file(file_name="sample", data=source) + # [END upload_file_to_directory] + + # [START delete_file_in_directory] + # Delete the file in the directory + await directory.delete_file(file_name="sample") + # [END delete_file_in_directory] + + # [START delete_directory] + await directory.delete_directory() + # [END delete_directory] + + finally: + # Delete the share + await share.delete_share() + + def test_create_directory(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_create_directory()) + + async def _test_create_subdirectories(self): + # Instantiate the ShareClient from a connection string + from azure.storage.file.aio import ShareClient + share = ShareClient.from_connection_string(self.connection_string, "subdirshare") + + # Create the share + await share.create_share() + + try: + # Get the directory client + parent_dir = share.get_directory_client(directory_path="parentdir") + + # [START create_subdirectory] + # Create the directory + await 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: + await parent_dir.upload_file(file_name="sample", data=source) + + # Upload a file to the subdirectory + with open(SOURCE_FILE, "rb") as source: + await subdir.upload_file(file_name="sample", data=source) + + # [START lists_directory] + # List the directories and files under the parent directory + my_list = [] + async for item in parent_dir.list_directories_and_files(): + my_list.append(item) + print(my_list) + # [END lists_directory] + + # You must delete the file in the subdirectory before deleting the subdirectory + await subdir.delete_file("sample") + # [START delete_subdirectory] + await parent_dir.delete_subdirectory("subdir") + # [END delete_subdirectory] + + finally: + # Delete the share + await share.delete_share() + + def test_create_subdirectories(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_create_subdirectories()) + + async def _test_get_subdirectory_client(self): + # Instantiate the ShareClient from a connection string + from azure.storage.file.aio import ShareClient + share = ShareClient.from_connection_string(self.connection_string, "dirtest") + + # Create the share + await share.create_share() + + try: + # [START get_subdirectory_client] + # Get a directory client and create the directory + parent = share.get_directory_client("dir1") + await parent.create_directory() + + # Get a subdirectory client and create the subdirectory "dir1/dir2" + subdirectory = parent.get_subdirectory_client("dir2") + await subdirectory.create_directory() + # [END get_subdirectory_client] + finally: + # Delete the share + await share.delete_share() + + def test_get_subdirectory_client(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_get_subdirectory_client()) diff --git a/sdk/storage/azure-storage-file/tests/test_file_samples_file_async.py b/sdk/storage/azure-storage-file/tests/test_file_samples_file_async.py new file mode 100644 index 000000000000..2e0326167031 --- /dev/null +++ b/sdk/storage/azure-storage-file/tests/test_file_samples_file_async.py @@ -0,0 +1,136 @@ +# coding: utf-8 + +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +import os +import asyncio +try: + import settings_real as settings +except ImportError: + import file_settings_fake as settings + +from filetestcase import ( + FileTestCase, + TestMode, + record +) + +SOURCE_FILE = 'SampleSource.txt' +DEST_FILE = 'SampleDestination.txt' + + +class TestFileSamples(FileTestCase): + + connection_string = settings.CONNECTION_STRING + + def setUp(self): + data = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit" + with open(SOURCE_FILE, 'wb') as stream: + stream.write(data) + + super(TestFileSamples, self).setUp() + + def tearDown(self): + if os.path.isfile(SOURCE_FILE): + try: + os.remove(SOURCE_FILE) + except: + pass + if os.path.isfile(DEST_FILE): + try: + os.remove(DEST_FILE) + except: + pass + + return super(TestFileSamples, self).tearDown() + + #--Begin File Samples----------------------------------------------------------------- + + async def _test_file_operations(self): + # Instantiate the ShareClient from a connection string + from azure.storage.file.aio import ShareClient + share = ShareClient.from_connection_string(self.connection_string, "filesshare") + + # Create the share + await share.create_share() + + try: + # Get a file client + file1 = 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) + await file1.create_file(size=100) + # [END create_file] + + # Or upload a file directly + # [START upload_file] + with open(SOURCE_FILE, "rb") as source: + await file2.upload_file(source) + # [END upload_file] + + # Download the file + # [START download_file] + with open(DEST_FILE, "wb") as data: + data.writelines(file2.download_file()) + # [END download_file] + + # Delete the files + await file1.delete_file() + # [START delete_file] + await file2.delete_file() + # [END delete_file] + + finally: + # Delete the share + await share.delete_share() + + def test_file_operations(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_file_operations()) + + async def _test_copy_from_url(self): + # Instantiate the ShareClient from a connection string + from azure.storage.file.aio import ShareClient + share = ShareClient.from_connection_string(self.connection_string, "filesfromurl") + + # Create the share + await 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: + await 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] + await destination_file.start_copy_from_url(source_url=source_url) + # [END copy_file_from_url] + finally: + # Delete the share + await share.delete_share() + + def test_copy_from_url(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_copy_from_url()) diff --git a/sdk/storage/azure-storage-file/tests/test_file_samples_hello_world_async.py b/sdk/storage/azure-storage-file/tests/test_file_samples_hello_world_async.py new file mode 100644 index 000000000000..fcce24938459 --- /dev/null +++ b/sdk/storage/azure-storage-file/tests/test_file_samples_hello_world_async.py @@ -0,0 +1,113 @@ +# coding: utf-8 + +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +import os +import asyncio +try: + import settings_real as settings +except ImportError: + import file_settings_fake as settings + +from filetestcase import ( + FileTestCase, + TestMode, + record +) + +SOURCE_FILE = 'SampleSource.txt' + + +class TestHelloWorldSamples(FileTestCase): + + connection_string = settings.CONNECTION_STRING + + def setUp(self): + data = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit" + with open(SOURCE_FILE, 'wb') as stream: + stream.write(data) + + super(TestHelloWorldSamples, self).setUp() + + def tearDown(self): + if os.path.isfile(SOURCE_FILE): + try: + os.remove(SOURCE_FILE) + except: + pass + + return super(TestHelloWorldSamples, self).tearDown() + + #--Begin File Samples----------------------------------------------------------------- + + async def _test_create_client_with_connection_string(self): + # Instantiate the FileServiceClient from a connection string + from azure.storage.file.aio import FileServiceClient + file_service = FileServiceClient.from_connection_string(self.connection_string) + + # Get queue service properties + properties = await file_service.get_service_properties() + assert properties is not None + + def test_create_client_with_connection_string(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_create_client_with_connection_string()) + + async def _test_create_file_share(self): + # Instantiate the ShareClient from a connection string + from azure.storage.file.aio import ShareClient + share = ShareClient.from_connection_string(self.connection_string, share="myshare") + + # Create the share + await share.create_share() + + try: + # [START get_share_properties] + properties = await share.get_share_properties() + # [END get_share_properties] + assert properties is not None + + finally: + # Delete the share + await share.delete_share() + + def test_create_file_share(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_create_file_share()) + + async def _test_upload_file_to_share(self): + # Instantiate the ShareClient from a connection string + from azure.storage.file.aio import ShareClient + share = ShareClient.from_connection_string(self.connection_string, share="share") + + # Create the share + await share.create_share() + + try: + # Instantiate the FileClient from a connection string + # [START create_file_client] + from azure.storage.file.aio import FileClient + file = FileClient.from_connection_string(self.connection_string, share="share", file_path="myfile") + # [END create_file_client] + + # Upload a file + with open(SOURCE_FILE, "rb") as source_file: + await file.upload_file(source_file) + + finally: + # Delete the share + await share.delete_share() + + def test_upload_file_to_share(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_upload_file_to_share()) diff --git a/sdk/storage/azure-storage-file/tests/test_file_samples_service_async.py b/sdk/storage/azure-storage-file/tests/test_file_samples_service_async.py new file mode 100644 index 000000000000..070c36fcdc32 --- /dev/null +++ b/sdk/storage/azure-storage-file/tests/test_file_samples_service_async.py @@ -0,0 +1,114 @@ +# coding: utf-8 + +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +import asyncio +try: + import settings_real as settings +except ImportError: + import file_settings_fake as settings + +from filetestcase import ( + FileTestCase, + TestMode, + record +) + + +class TestFileServiceSamples(FileTestCase): + + connection_string = settings.CONNECTION_STRING + + async def _test_file_service_properties(self): + # Instantiate the FileServiceClient from a connection string + from azure.storage.file.aio import FileServiceClient + file_service = FileServiceClient.from_connection_string(self.connection_string) + + # [START set_service_properties] + # Create service properties + from azure.storage.file.aio import Metrics, CorsRule, RetentionPolicy + + # Create metrics for requests statistics + hour_metrics = Metrics(enabled=True, include_apis=True, retention_policy=RetentionPolicy(enabled=True, days=5)) + minute_metrics = Metrics(enabled=True, include_apis=True, + retention_policy=RetentionPolicy(enabled=True, days=5)) + + # Create CORS rules + cors_rule1 = CorsRule(['www.xyz.com'], ['GET']) + allowed_origins = ['www.xyz.com', "www.ab.com", "www.bc.com"] + allowed_methods = ['GET', 'PUT'] + max_age_in_seconds = 500 + exposed_headers = ["x-ms-meta-data*", "x-ms-meta-source*", "x-ms-meta-abc", "x-ms-meta-bcd"] + allowed_headers = ["x-ms-meta-data*", "x-ms-meta-target*", "x-ms-meta-xyz", "x-ms-meta-foo"] + cors_rule2 = CorsRule( + allowed_origins, + allowed_methods, + max_age_in_seconds=max_age_in_seconds, + exposed_headers=exposed_headers, + allowed_headers=allowed_headers) + + cors = [cors_rule1, cors_rule2] + + # Set the service properties + await file_service.set_service_properties(hour_metrics, minute_metrics, cors) + # [END set_service_properties] + + # [START get_service_properties] + properties = await file_service.get_service_properties() + # [END get_service_properties] + + def test_file_service_properties(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_file_service_properties()) + + async def _test_share_operations(self): + # Instantiate the FileServiceClient from a connection string + from azure.storage.file.aio import FileServiceClient + file_service = FileServiceClient.from_connection_string(self.connection_string) + + # [START fsc_create_shares] + await file_service.create_share(share_name="testshare") + # [END fsc_create_shares] + try: + # [START fsc_list_shares] + # List the shares in the file service + my_shares = [] + async for s in file_service.list_shares(): + my_shares.append(s) + + # Print the shares + for share in my_shares: + print(share) + # [END fsc_list_shares] + + finally: + # [START fsc_delete_shares] + await file_service.delete_share(share_name="testshare") + # [END fsc_delete_shares] + + def test_share_operations(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_share_operations()) + + async def _test_get_share_client(self): + # [START get_share_client] + from azure.storage.file.aio import FileServiceClient + file_service = FileServiceClient.from_connection_string(self.connection_string) + + # Get a share client to interact with a specific share + share = await file_service.get_share_client("fileshare") + # [END get_share_client] + + def test_get_share_client(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_get_share_client()) diff --git a/sdk/storage/azure-storage-file/tests/test_file_samples_share_async.py b/sdk/storage/azure-storage-file/tests/test_file_samples_share_async.py new file mode 100644 index 000000000000..ee5c2ca138d2 --- /dev/null +++ b/sdk/storage/azure-storage-file/tests/test_file_samples_share_async.py @@ -0,0 +1,155 @@ +# coding: utf-8 + +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +import os +import asyncio +try: + import settings_real as settings +except ImportError: + import file_settings_fake as settings + +from filetestcase import ( + FileTestCase, + TestMode, + record +) + +SOURCE_FILE = 'SampleSource.txt' + + +class TestShareSamples(FileTestCase): + url = "{}://{}.file.core.windows.net".format( + settings.PROTOCOL, + settings.STORAGE_ACCOUNT_NAME + ) + connection_string = settings.CONNECTION_STRING + + def setUp(self): + data = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit" + with open(SOURCE_FILE, 'wb') as stream: + stream.write(data) + + super(TestShareSamples, self).setUp() + + def tearDown(self): + if os.path.isfile(SOURCE_FILE): + try: + os.remove(SOURCE_FILE) + except: + pass + + return super(TestShareSamples, self).tearDown() + + #--Begin File Samples----------------------------------------------------------------- + + async def _test_create_share_snapshot(self): + # Instantiate the ShareClient from a connection string + from azure.storage.file.aio import ShareClient + share = ShareClient.from_connection_string(self.connection_string, "sharesnapshot") + + # [START create_share] + await share.create_share() + # [END create_share] + try: + # [START create_share_snapshot] + await share.create_snapshot() + # [END create_share_snapshot] + finally: + # [START delete_share] + await share.delete_share(delete_snapshots=True) + # [END delete_share] + + def test_create_share_snapshot(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_create_share_snapshot()) + + async def _test_set_share_quota_and_metadata(self): + # [START create_share_client_from_conn_string] + from azure.storage.file.aio import ShareClient + share = ShareClient.from_connection_string(self.connection_string, "fileshare") + # [END create_share_client_from_conn_string] + + # Create the share + await share.create_share() + + try: + # [START set_share_quota] + # Set the quota for the share to 1GB + await share.set_share_quota(quota=1) + # [END set_share_quota] + + # [START set_share_metadata] + data = {'category': 'test'} + await share.set_share_metadata(metadata=data) + # [END set_share_metadata] + + # Get the metadata for the share + props = await share.get_share_properties().metadata + assert props == data + + finally: + # Delete the share + await share.delete_share() + + def test_set_share_quota_and_metadata(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_set_share_quota_and_metadata()) + + async def _test_list_directories_and_files(self): + # Instantiate the ShareClient from a connection string + from azure.storage.file.aio import ShareClient + share = ShareClient.from_connection_string(self.connection_string, "listshare") + + # Create the share + await share.create_share() + + try: + # [START share_list_files_in_dir] + # Create a directory in the share + dir_client = await share.create_directory("mydir") + + # Upload a file to the directory + with open(SOURCE_FILE, "rb") as source_file: + await dir_client.upload_file(file_name="sample", data=source_file) + + # List files in the directory + my_files = [] + async for item in share.list_directories_and_files(directory_name="mydir"): + my_files.append(item) + print(my_files) + # [END share_list_files_in_dir] + finally: + # Delete the share + await share.delete_share() + + def test_list_directories_and_files(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_list_directories_and_files()) + + async def _test_get_directory_or_file_client(self): + # Instantiate the ShareClient from a connection string + from azure.storage.file.aio 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_get_directory_or_file_client(self): + if TestMode.need_recording_file(self.test_mode): + return + loop = asyncio.get_event_loop() + loop.run_until_complete(self._test_get_directory_or_file_client())