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 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()
Пример #3
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()
Пример #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()
    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()
    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")
Пример #10
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)
Пример #12
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()
Пример #13
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()
Пример #14
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()
Пример #15
0
 def _get_share_cli(self):
     return ShareClient.from_connection_string(conn_str=self.connection_string, share_name=self.share_name)