async def set_share_properties(self):
        from azure.storage.fileshare.aio import ShareClient
        share1 = ShareClient.from_connection_string(self.connection_string,
                                                    "sharesamples3a")
        share2 = ShareClient.from_connection_string(self.connection_string,
                                                    "sharesamples3b")

        # Create the share
        async with share1 and share2:
            await share1.create_share()
            await share2.create_share()

            try:
                # [START set_share_properties]
                # Set the tier for the first share to Hot
                await share1.set_share_properties(access_tier="Hot")
                # Set the quota for the first share to 3
                await share1.set_share_properties(quota=3)
                # Set the tier for the second share to Cool and quota to 2
                await share2.set_share_properties(
                    access_tier=ShareAccessTier("Cool"), quota=2)

                # Get the shares' properties
                props1 = await share1.get_share_properties()
                props2 = await share2.get_share_properties()
                print(props1.access_tier)
                print(props1.quota)
                print(props2.access_tier)
                print(props2.quota)
                # [END set_share_properties]

            finally:
                # Delete the shares
                await share1.delete_share()
                await share2.delete_share()
Пример #2
0
async def download_files_async(directory_path: str, dest_path: str):
    share_client = ShareClientAsync.from_connection_string(
        connection_string, share_name)
    async with share_client:
        tasks = []
        try:
            for path in list_directories_and_files(directory_path):
                # Get Azure file share client.
                file_client = share_client.get_file_client(file_path=path)
                # Get file name
                file_name = path.split("/")[-1]
                # Get file path
                file_path_tmp = (dest_path + "/" +
                                 "/".join([x for x in path.split("/")[:-1]]))
                # Create sub folder
                os.makedirs(file_path_tmp, exist_ok=True)
                # Execute dowload file
                task = asyncio.ensure_future(
                    __dowload_file_async(file_client,
                                         file_path_tmp + "/" + file_name))
                tasks.append(task)
            await asyncio.gather(*tasks)
        finally:
            # Close share client.
            await share_client.close()
Пример #3
0
    async def test_create_snapshot_with_metadata_async(self, resource_group,
                                                       location,
                                                       storage_account,
                                                       storage_account_key):
        self._setup(storage_account, storage_account_key)
        share = self._get_share_reference()
        metadata = {"test1": "foo", "test2": "bar"}
        metadata2 = {"test100": "foo100", "test200": "bar200"}

        # Act
        created = await share.create_share(metadata=metadata)
        snapshot = await share.create_snapshot(metadata=metadata2)

        share_props = await share.get_share_properties()
        snapshot_client = ShareClient(self.account_url(storage_account,
                                                       "file"),
                                      share_name=share.share_name,
                                      snapshot=snapshot,
                                      credential=storage_account_key)
        snapshot_props = await 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)
        await self._delete_shares(share.share_name)
Пример #4
0
    async def file_copy_from_url_async(self):
        # Instantiate the ShareClient from a connection string
        from azure.storage.fileshare.aio import ShareClient
        share = ShareClient.from_connection_string(self.connection_string,
                                                   "filesamples2")

        # Create the share
        async with 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("destinationfile")

                # Build the url from which to copy the file
                source_url = "{}://{}.file.core.windows.net/{}/{}".format(
                    self.protocol, self.storage_account_name, 'filesamples2',
                    '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()
Пример #5
0
def get_az_share_client(storage_account_name: str,
                        storage_key: str,
                        share_name: str,
                        is_async: bool = False,
                        **kwargs) -> Union[ShareClient, ShareClientAsync]:
    """Get Azure Share Client.

    :param storage_account_name: Storage account name.
    :param storage_key: Storage key.
    :param share_name: Share name
    :param is_async: If True create a Azure share client with async mode.
    :return: Azure share client.
    """
    # Create connection String.
    conn_str = "DefaultEndpointsProtocol={0};AccountName={1};AccountKey={2};EndpointSuffix={3}".format(
        config_storage.protocol,
        storage_account_name,
        storage_key,
        config_storage.suffix,
    )

    # Create Share client.
    if is_async:
        share_client = ShareClientAsync.from_connection_string(
            conn_str=conn_str, share_name=share_name)
    else:
        share_client = ShareClient.from_connection_string(
            conn_str=conn_str, share_name=share_name)
    return share_client
Пример #6
0
    async def create_directory_and_file_async(self):
        # Instantiate the ShareClient from a connection string
        from azure.storage.fileshare.aio import ShareClient
        share = ShareClient.from_connection_string(self.connection_string, "directorysamples1")

        # Create the share
        async with 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()
Пример #7
0
    async def upload_a_file_to_share_async(self):
        # Instantiate the ShareClient from a connection string
        from azure.storage.fileshare.aio import ShareClient
        share = ShareClient.from_connection_string(self.connection_string,
                                                   share_name='helloworld2')

        # Create the share
        async with share:
            await share.create_share()

            try:
                # Instantiate the ShareFileClient from a connection string
                # [START create_file_client]
                from azure.storage.fileshare.aio import ShareFileClient
                file = ShareFileClient.from_connection_string(
                    self.connection_string,
                    share_name='helloworld2',
                    file_path="myfile")
                # [END create_file_client]

                # Upload a file
                async with file:
                    with open(SOURCE_FILE, "rb") as source_file:
                        await file.upload_file(source_file)

            finally:
                # Delete the share
                await share.delete_share()
    async def set_share_quota_and_metadata_async(self):
        # [START create_share_client_from_conn_string]
        from azure.storage.fileshare.aio import ShareClient
        share = ShareClient.from_connection_string(self.connection_string,
                                                   "sharesamples2")
        # [END create_share_client_from_conn_string]

        # Create the share
        async with 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

            finally:
                # Delete the share
                await share.delete_share()
    async def list_directories_and_files_async(self):
        # Instantiate the ShareClient from a connection string
        from azure.storage.fileshare.aio import ShareClient
        share = ShareClient.from_connection_string(self.connection_string,
                                                   "sharesamples4")

        # Create the share
        async with 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()
Пример #10
0
    def test_invalid_api_version(self):
        with pytest.raises(ValueError) as error:
            ShareServiceClient("https://foo.file.core.windows.net/account",
                               credential="fake_key",
                               api_version="foo")
        self.assertTrue(
            str(error.value).startswith("Unsupported API version 'foo'."))

        with pytest.raises(ValueError) as error:
            ShareClient("https://foo.file.core.windows.net/account",
                        "share_name",
                        credential="fake_key",
                        api_version="foo")
        self.assertTrue(
            str(error.value).startswith("Unsupported API version 'foo'."))

        with pytest.raises(ValueError) as error:
            ShareDirectoryClient("https://foo.file.core.windows.net/account",
                                 "share_name",
                                 "dir_path",
                                 credential="fake_key",
                                 api_version="foo")
        self.assertTrue(
            str(error.value).startswith("Unsupported API version 'foo'."))

        with pytest.raises(ValueError) as error:
            ShareFileClient("https://foo.file.core.windows.net/account",
                            "share",
                            self._get_file_reference(),
                            credential="fake_key",
                            api_version="foo")
        self.assertTrue(
            str(error.value).startswith("Unsupported API version 'foo'."))
    async def get_directory_or_file_client_async(self):
        # Instantiate the ShareClient from a connection string
        from azure.storage.fileshare.aio import ShareClient
        share = ShareClient.from_connection_string(self.connection_string, "sharesamples4")

        # 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")
Пример #12
0
    def test_share_client_api_version_property(self):
        share_client = ShareClient("https://foo.file.core.windows.net/account",
                                   "share_name",
                                   credential="fake_key")
        self.assertEqual(share_client.api_version, self.api_version_2)
        self.assertEqual(share_client._client._config.version,
                         self.api_version_2)

        share_client = ShareClient("https://foo.file.core.windows.net/account",
                                   "share_name",
                                   credential="fake_key",
                                   api_version=self.api_version_1)
        self.assertEqual(share_client.api_version, self.api_version_1)
        self.assertEqual(share_client._client._config.version,
                         self.api_version_1)

        dir_client = share_client.get_directory_client("foo")
        self.assertEqual(dir_client.api_version, self.api_version_1)
        self.assertEqual(dir_client._client._config.version,
                         self.api_version_1)

        file_client = share_client.get_file_client("foo")
        self.assertEqual(file_client.api_version, self.api_version_1)
        self.assertEqual(file_client._client._config.version,
                         self.api_version_1)
Пример #13
0
    async def create_subdirectory_and_file_async(self):
        # Instantiate the ShareClient from a connection string
        from azure.storage.fileshare.aio import ShareClient
        share = ShareClient.from_connection_string(self.connection_string,
                                                   "directorysamples2")

        # Create the share
        async with 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 = await 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()
    async def create_share_snapshot_async(self):
        # Instantiate the ShareClient from a connection string
        from azure.storage.fileshare.aio import ShareClient
        share = ShareClient.from_connection_string(self.connection_string, "sharesamples1")

        async with share:
            # [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)
Пример #15
0
    async def _test_delete_snapshot_async(self):
        # Arrange
        share = self._get_share_reference()
        await share.create_share()
        snapshot = await share.create_snapshot()

        # Act
        with self.assertRaises(HttpResponseError):
            await share.delete_share()

        snapshot_client = ShareClient(
            self.get_file_url(),
            share_name=share.share_name,
            snapshot=snapshot,
            credential=self.settings.STORAGE_ACCOUNT_KEY)

        deleted = await snapshot_client.delete_share()
        self.assertIsNone(deleted)
Пример #16
0
    async def create_file_share_async(self):
        # Instantiate the ShareClient from a connection string
        from azure.storage.fileshare.aio import ShareClient
        share = ShareClient.from_connection_string(self.connection_string,
                                                   share_name="helloworld1")

        # Create the share
        async with share:
            await share.create_share()

            try:
                # [START get_share_properties]
                properties = await share.get_share_properties()
                # [END get_share_properties]

            finally:
                # Delete the share
                await share.delete_share()
Пример #17
0
    async def simple_file_operations_async(self):
        # Instantiate the ShareClient from a connection string
        from azure.storage.fileshare.aio import ShareClient
        share = ShareClient.from_connection_string(self.connection_string,
                                                   "filesamples1")

        # Create the share
        async with share:
            await share.create_share()

            try:
                # Get a file client
                my_allocated_file = share.get_file_client("my_allocated_file")
                my_file = share.get_file_client("my_file")

                # [START create_file]
                # Create and allocate bytes for the file (no content added yet)
                await my_allocated_file.create_file(size=100)
                # [END create_file]

                # Or upload a file directly
                # [START upload_file]
                with open(SOURCE_FILE, "rb") as source:
                    await my_file.upload_file(source)
                # [END upload_file]

                # Download the file
                # [START download_file]
                with open(DEST_FILE, "wb") as data:
                    stream = await my_file.download_file()
                    data.write(await stream.readall())
                # [END download_file]

                # Delete the files
                # [START delete_file]
                await my_file.delete_file()
                # [END delete_file]
                await my_allocated_file.delete_file()

            finally:
                # Delete the share
                await share.delete_share()
Пример #18
0
    async def test_delete_snapshot_async(self, resource_group, location,
                                         storage_account, storage_account_key):
        self._setup(storage_account, storage_account_key)
        share = self._get_share_reference()
        await share.create_share()
        snapshot = await share.create_snapshot()

        # Act
        with self.assertRaises(HttpResponseError):
            await share.delete_share()

        snapshot_client = ShareClient(self.account_url(storage_account,
                                                       "file"),
                                      share_name=share.share_name,
                                      snapshot=snapshot,
                                      credential=storage_account_key)

        deleted = await snapshot_client.delete_share()
        self.assertIsNone(deleted)
        await self._delete_shares(share.share_name)
Пример #19
0
    async def get_subdirectory_client_async(self):
        # Instantiate the ShareClient from a connection string
        from azure.storage.fileshare.aio import ShareClient
        share = ShareClient.from_connection_string(self.connection_string, "directorysamples3")

        # Create the share
        async with 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()
Пример #20
0
    async def _test_create_snapshot_with_metadata_async(self):
        # Arrange
        share = self._get_share_reference()
        metadata = {"test1": "foo", "test2": "bar"}
        metadata2 = {"test100": "foo100", "test200": "bar200"}

        # Act
        created = await share.create_share(metadata=metadata)
        snapshot = await share.create_snapshot(metadata=metadata2)

        share_props = await 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 = await 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)
    def test_create_service_with_custom_account_endpoint_path(self):
        custom_account_url = "http://local-machine:11002/custom/account/path/" + self.sas_token
        for service_type in SERVICES.items():
            conn_string = 'DefaultEndpointsProtocol=http;AccountName={};AccountKey={};FileEndpoint={};'.format(
                self.account_name, self.account_key, custom_account_url)

            # Act
            service = service_type[0].from_connection_string(
                conn_string,
                share_name="foo",
                directory_path="bar",
                file_path="baz")

            # Assert
            self.assertEqual(service.account_name, self.account_name)
            self.assertEqual(service.credential.account_name,
                             self.account_name)
            self.assertEqual(service.credential.account_key, self.account_key)
            self.assertEqual(service.primary_hostname,
                             'local-machine:11002/custom/account/path')

        service = ShareServiceClient(account_url=custom_account_url)
        self.assertEqual(service.account_name, None)
        self.assertEqual(service.credential, None)
        self.assertEqual(service.primary_hostname,
                         'local-machine:11002/custom/account/path')
        self.assertTrue(
            service.url.startswith(
                'http://local-machine:11002/custom/account/path/?'))

        service = ShareClient(account_url=custom_account_url,
                              share_name="foo",
                              snapshot="snap")
        self.assertEqual(service.account_name, None)
        self.assertEqual(service.share_name, "foo")
        self.assertEqual(service.snapshot, "snap")
        self.assertEqual(service.credential, None)
        self.assertEqual(service.primary_hostname,
                         'local-machine:11002/custom/account/path')
        self.assertTrue(
            service.url.startswith(
                'http://local-machine:11002/custom/account/path/foo?sharesnapshot=snap&'
            ))

        service = ShareDirectoryClient(account_url=custom_account_url,
                                       share_name='foo',
                                       directory_path="bar/baz",
                                       snapshot="snap")
        self.assertEqual(service.account_name, None)
        self.assertEqual(service.share_name, "foo")
        self.assertEqual(service.directory_path, "bar/baz")
        self.assertEqual(service.snapshot, "snap")
        self.assertEqual(service.credential, None)
        self.assertEqual(service.primary_hostname,
                         'local-machine:11002/custom/account/path')
        self.assertTrue(
            service.url.startswith(
                'http://local-machine:11002/custom/account/path/foo/bar%2Fbaz?sharesnapshot=snap&'
            ))

        service = ShareDirectoryClient(account_url=custom_account_url,
                                       share_name='foo',
                                       directory_path="")
        self.assertEqual(service.account_name, None)
        self.assertEqual(service.share_name, "foo")
        self.assertEqual(service.directory_path, "")
        self.assertEqual(service.snapshot, None)
        self.assertEqual(service.credential, None)
        self.assertEqual(service.primary_hostname,
                         'local-machine:11002/custom/account/path')
        self.assertTrue(
            service.url.startswith(
                'http://local-machine:11002/custom/account/path/foo?'))

        service = ShareFileClient(account_url=custom_account_url,
                                  share_name="foo",
                                  file_path="bar/baz/file",
                                  snapshot="snap")
        self.assertEqual(service.account_name, None)
        self.assertEqual(service.share_name, "foo")
        self.assertEqual(service.directory_path, "bar/baz")
        self.assertEqual(service.file_path, ["bar", "baz", "file"])
        self.assertEqual(service.file_name, "file")
        self.assertEqual(service.snapshot, "snap")
        self.assertEqual(service.credential, None)
        self.assertEqual(service.primary_hostname,
                         'local-machine:11002/custom/account/path')
        self.assertTrue(
            service.url.startswith(
                'http://local-machine:11002/custom/account/path/foo/bar/baz/file?sharesnapshot=snap&'
            ))

        service = ShareFileClient(account_url=custom_account_url,
                                  share_name="foo",
                                  file_path="file")
        self.assertEqual(service.account_name, None)
        self.assertEqual(service.share_name, "foo")
        self.assertEqual(service.directory_path, "")
        self.assertEqual(service.file_path, ["file"])
        self.assertEqual(service.file_name, "file")
        self.assertEqual(service.snapshot, None)
        self.assertEqual(service.credential, None)
        self.assertEqual(service.primary_hostname,
                         'local-machine:11002/custom/account/path')
        self.assertTrue(
            service.url.startswith(
                'http://local-machine:11002/custom/account/path/foo/file?'))
Пример #22
0
 def _get_share_cli(self):
     return ShareClient.from_connection_string(conn_str=self.connection_string, share_name=self.share_name)