Exemple #1
0
    def test_create_blob_client_with_sub_directory_path_in_blob_name(self):
        blob_url = "https://testaccount.blob.core.windows.net/containername/dir1/sub000/2010_Unit150_Ivan097_img0003.jpg"
        blob_client = BlobClient.from_blob_url(blob_url)
        self.assertEqual(blob_client.container_name, "containername")
        self.assertEqual(blob_client.blob_name, "dir1/sub000/2010_Unit150_Ivan097_img0003.jpg")

        blob_emulator_url = 'http://127.0.0.1:1000/devstoreaccount1/containername/dir1/sub000/2010_Unit150_Ivan097_img0003.jpg'
        blob_client = BlobClient.from_blob_url(blob_emulator_url)
        self.assertEqual(blob_client.container_name, "containername")
        self.assertEqual(blob_client.blob_name, "dir1/sub000/2010_Unit150_Ivan097_img0003.jpg")
Exemple #2
0
    async def _test_auth_blob_url_async(self):
        # [START create_blob_client]
        from azure.storage.blob.aio import BlobClient
        blob_client = BlobClient.from_blob_url(blob_url="https://account.blob.core.windows.net/container/blob-name")
        # [END create_blob_client]

        # [START create_blob_client_sas_url]
        from azure.storage.blob.aio import BlobClient

        sas_url = "https://account.blob.core.windows.net/container/blob-name?sv=2015-04-05&st=2015-04-29T22%3A18%3A26Z&se=2015-04-30T02%3A23%3A26Z&sr=b&sp=rw&sip=168.1.5.60-168.1.5.70&spr=https&sig=Z%2FRHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk%3D"
        blob_client = BlobClient.from_blob_url(sas_url)
    async def _setup(self, bsc):
        self.container_name = self.get_resource_name('utcontainer')
        if self.is_live:
            try:
                # create source blob to be copied from
                self.source_blob_name = self.get_resource_name('srcblob')
                self.source_blob_data = self.get_random_bytes(4 * 1024)
                source_blob = bsc.get_blob_client(self.container_name,
                                                  self.source_blob_name)

                await bsc.create_container(self.container_name)
                await source_blob.upload_blob(self.source_blob_data)

                # generate a SAS so that it is accessible with a URL
                sas_token = generate_blob_sas(
                    source_blob.account_name,
                    source_blob.container_name,
                    source_blob.blob_name,
                    snapshot=source_blob.snapshot,
                    account_key=source_blob.credential.account_key,
                    permission=BlobSasPermissions(read=True),
                    expiry=datetime.utcnow() + timedelta(hours=1),
                )
                sas_source = BlobClient.from_blob_url(source_blob.url,
                                                      credential=sas_token)
                self.source_blob_url = sas_source.url
            except:
                pass
Exemple #4
0
    def setUp(self):
        super(StorageBlockBlobTestAsync, self).setUp()
        url = self._get_account_url()
        credential = self._get_shared_key_credential()

        # test chunking functionality by reducing the size of each chunk,
        # otherwise the tests would take too long to execute
        self.bsc = BlobServiceClient(
            url,
            credential=credential,
            connection_data_block_size=4 * 1024,
            max_single_put_size=32 * 1024,
            max_block_size=4 * 1024,
            transport=AiohttpTestTransport())
        self.config = self.bsc._config
        self.container_name = self.get_resource_name('utcontainer')

        # create source blob to be copied from
        self.source_blob_name = self.get_resource_name('srcblob')
        self.source_blob_data = self.get_random_bytes(SOURCE_BLOB_SIZE)

        blob = self.bsc.get_blob_client(self.container_name, self.source_blob_name)

        # generate a SAS so that it is accessible with a URL
        sas_token = blob.generate_shared_access_signature(
            permission=BlobSasPermissions(read=True),
            expiry=datetime.utcnow() + timedelta(hours=1),
        )
        self.source_blob_url = BlobClient.from_blob_url(blob.url, credential=sas_token).url
Exemple #5
0
async def set_blob_metadata(blob_url: str, metadata: Dict[str, str]):
    """Sets the provided dictionary as the metadata on the Azure blob"""
    blob_client = BlobClient.from_blob_url(blob_url)
    logger.info(f"Setting blob properties '{blob_client.blob_name}'" +
                f"from container '{blob_client.container_name}' on account:" +
                f"'{blob_client.account_name}'")
    return await blob_client.set_blob_metadata(metadata=metadata)
Exemple #6
0
    def test_create_service_with_custom_account_endpoint_path(self):
        account_name = "blobstorage"
        account_key = "blobkey"
        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={};BlobEndpoint={};'.format(
                account_name, account_key, custom_account_url)

            # Act
            service = service_type[0].from_connection_string(
                conn_string, container_name="foo", blob_name="bar")

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

        service = BlobServiceClient(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 = ContainerClient(account_url=custom_account_url, container_name="foo")
        self.assertEqual(service.account_name, None)
        self.assertEqual(service.container_name, "foo")
        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 = ContainerClient.from_container_url("http://local-machine:11002/custom/account/path/foo?query=value")
        self.assertEqual(service.account_name, None)
        self.assertEqual(service.container_name, "foo")
        self.assertEqual(service.credential, None)
        self.assertEqual(service.primary_hostname, 'local-machine:11002/custom/account/path')
        self.assertEqual(service.url, 'http://local-machine:11002/custom/account/path/foo')

        service = BlobClient(account_url=custom_account_url, container_name="foo", blob_name="bar", snapshot="baz")
        self.assertEqual(service.account_name, None)
        self.assertEqual(service.container_name, "foo")
        self.assertEqual(service.blob_name, "bar")
        self.assertEqual(service.snapshot, "baz")
        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?snapshot=baz&'))

        service = BlobClient.from_blob_url("http://local-machine:11002/custom/account/path/foo/bar?snapshot=baz&query=value")
        self.assertEqual(service.account_name, None)
        self.assertEqual(service.container_name, "foo")
        self.assertEqual(service.blob_name, "bar")
        self.assertEqual(service.snapshot, "baz")
        self.assertEqual(service.credential, None)
        self.assertEqual(service.primary_hostname, 'local-machine:11002/custom/account/path')
        self.assertEqual(service.url, 'http://local-machine:11002/custom/account/path/foo/bar?snapshot=baz')
Exemple #7
0
async def download_blob_properties(blob_url: str) -> Dict[str, str]:
    """Downloads the blob properties from Azure for the given blob URI"""
    blob_client = BlobClient.from_blob_url(blob_url)
    logger.info(f"Downloading blob properties '{blob_client.blob_name}'" +
                f"from container '{blob_client.container_name}'" +
                f"on account: '{blob_client.account_name}'")

    response = await blob_client.get_blob_properties()
    logger.debug(response)

    return response
Exemple #8
0
async def download_blob(blob_url: str) -> Any:
    """
    Downloads the given blob from the container.
    """
    blob_client = BlobClient.from_blob_url(blob_url)
    logger.info(f"Downloading blob '{blob_client.blob_name}'" +
                f"from container '{blob_client.container_name}'" +
                f"on account: '{blob_client.account_name}'")

    response = await (await blob_client.download_blob()).readall()
    logger.debug(response)
    await blob_client.close()

    return response
Exemple #9
0
    async def _setup(self):
        blob = self.bsc.get_blob_client(self.container_name, self.source_blob_name)
        if not self.is_playback():
            try:
                await self.bsc.create_container(self.container_name)
            except:
                pass
            await blob.upload_blob(self.source_blob_data, overwrite=True)

        # generate a SAS so that it is accessible with a URL
        sas_token = blob.generate_shared_access_signature(
            permission=BlobSasPermissions(read=True),
            expiry=datetime.utcnow() + timedelta(hours=1),
        )
        self.source_blob_url = BlobClient.from_blob_url(blob.url, credential=sas_token).url
Exemple #10
0
    async def _setup(self):
        if not self.is_playback():
            try:
                # create source blob to be copied from
                self.source_blob_name = self.get_resource_name('srcblob')
                self.source_blob_data = self.get_random_bytes(4 * 1024)
                source_blob = self.bsc.get_blob_client(self.container_name,
                                                       self.source_blob_name)

                await self.bsc.create_container(self.container_name)
                await source_blob.upload_blob(self.source_blob_data)

                # generate a SAS so that it is accessible with a URL
                sas_token = source_blob.generate_shared_access_signature(
                    permission=BlobSasPermissions(read=True),
                    expiry=datetime.utcnow() + timedelta(hours=1),
                )
                sas_source = BlobClient.from_blob_url(source_blob.url,
                                                      credential=sas_token)
                self.source_blob_url = sas_source.url
            except:
                pass
Exemple #11
0
    async def _setup(self, storage_account, key):
        # test chunking functionality by reducing the size of each chunk,
        # otherwise the tests would take too long to execute
        self.bsc = BlobServiceClient(self.account_url(storage_account, "blob"),
                                     credential=key,
                                     connection_data_block_size=4 * 1024,
                                     max_single_put_size=32 * 1024,
                                     max_block_size=4 * 1024,
                                     transport=AiohttpTestTransport())
        self.config = self.bsc._config
        self.container_name = self.get_resource_name('utcontainer')

        # create source blob to be copied from
        self.source_blob_name = self.get_resource_name('srcblob')
        self.source_blob_data = self.get_random_bytes(SOURCE_BLOB_SIZE)

        blob = self.bsc.get_blob_client(self.container_name,
                                        self.source_blob_name)

        if self.is_live:
            try:
                await self.bsc.create_container(self.container_name)
            except:
                pass
            await blob.upload_blob(self.source_blob_data, overwrite=True)

        # generate a SAS so that it is accessible with a URL
        sas_token = generate_blob_sas(
            blob.account_name,
            blob.container_name,
            blob.blob_name,
            snapshot=blob.snapshot,
            account_key=blob.credential.account_key,
            permission=BlobSasPermissions(read=True),
            expiry=datetime.utcnow() + timedelta(hours=1),
        )
        self.source_blob_url = BlobClient.from_blob_url(
            blob.url, credential=sas_token).url
        self.source_blob_url_without_sas = blob.url
Exemple #12
0
    async def sample_translation_with_azure_blob(self):

        translation_client = DocumentTranslationClient(
            self.endpoint, AzureKeyCredential(self.key))

        blob_service_client = BlobServiceClient(self.storage_endpoint,
                                                credential=self.storage_key)

        source_container = await self.create_container(
            blob_service_client,
            container_name=self.storage_source_container_name
            or "translation-source-container",
        )
        target_container = await self.create_container(
            blob_service_client,
            container_name=self.storage_target_container_name
            or "translation-target-container")

        if self.document_name:
            with open(self.document_name, "rb") as doc:
                await source_container.upload_blob(self.document_name, doc)
        else:
            self.document_name = "example_document.txt"
            await source_container.upload_blob(
                name=self.document_name,
                data=
                b"This is an example translation with the document translation client library"
            )
        print("Uploaded document {} to storage container {}".format(
            self.document_name, source_container.container_name))

        source_container_sas_url = self.generate_sas_url(source_container,
                                                         permissions="rl")
        target_container_sas_url = self.generate_sas_url(target_container,
                                                         permissions="wl")

        translation_inputs = [
            DocumentTranslationInput(
                source_url=source_container_sas_url,
                targets=[
                    TranslationTarget(target_url=target_container_sas_url,
                                      language_code="fr")
                ])
        ]

        job = await translation_client.create_translation_job(
            translation_inputs)
        print("Created translation job with ID: {}".format(job.id))
        print("Waiting until job completes...")

        job_result = await translation_client.wait_until_done(job.id)
        print("Job status: {}".format(job_result.status))

        doc_results = translation_client.list_all_document_statuses(
            job_result.id)

        print("\nDocument results:")
        async for document in doc_results:
            print("Document ID: {}".format(document.id))
            print("Document status: {}".format(document.status))
            if document.status == "Succeeded":
                print("Document location: {}".format(
                    document.translated_document_url))
                print("Translated to language: {}\n".format(
                    document.translate_to))

                blob_client = BlobClient.from_blob_url(
                    document.translated_document_url,
                    credential=self.storage_key)
                async with blob_client:
                    with open("translated_" + self.document_name,
                              "wb") as my_blob:
                        download_stream = await blob_client.download_blob()
                        my_blob.write(await download_stream.readall())

                print("Downloaded {} locally".format("translated_" +
                                                     self.document_name))
            else:
                print("\nThere was a problem translating your document.")
                print("Document Error Code: {}, Message: {}\n".format(
                    document.error.code, document.error.message))

        await translation_client.close()
        await blob_service_client.close()
Exemple #13
0
    async def sample_translation_with_azure_blob(self):

        translation_client = DocumentTranslationClient(
            self.endpoint, AzureKeyCredential(self.key)
        )

        blob_service_client = BlobServiceClient(
            self.storage_endpoint,
            credential=self.storage_key
        )

        source_container = await self.create_container(
            blob_service_client,
            container_name=self.storage_source_container_name or "translation-source-container-async",
        )
        target_container = await self.create_container(
            blob_service_client,
            container_name=self.storage_target_container_name or "translation-target-container-async"
        )

        if self.document_name:
            with open(self.document_name, "rb") as doc:
                await source_container.upload_blob(self.document_name, doc)
        else:
            self.document_name = "example_document.txt"
            await source_container.upload_blob(
                name=self.document_name,
                data=b"This is an example translation with the document translation client library"
            )
        print(f"Uploaded document {self.document_name} to storage container {source_container.container_name}")

        source_container_sas_url = self.generate_sas_url(source_container, permissions="rl")
        target_container_sas_url = self.generate_sas_url(target_container, permissions="wl")

        poller = await translation_client.begin_translation(source_container_sas_url, target_container_sas_url, "fr")
        print(f"Created translation operation with ID: {poller.id}")
        print("Waiting until translation completes...")

        result = await poller.result()
        print(f"Status: {poller.status()}")

        print("\nDocument results:")
        async for document in result:
            print(f"Document ID: {document.id}")
            print(f"Document status: {document.status}")
            if document.status == "Succeeded":
                print(f"Source document location: {document.source_document_url}")
                print(f"Translated document location: {document.translated_document_url}")
                print(f"Translated to language: {document.translated_to}\n")

                blob_client = BlobClient.from_blob_url(document.translated_document_url, credential=self.storage_key)
                async with blob_client:
                    with open("translated_"+self.document_name, "wb") as my_blob:
                        download_stream = await blob_client.download_blob()
                        my_blob.write(await download_stream.readall())

                print("Downloaded {} locally".format("translated_"+self.document_name))
            else:
                print("\nThere was a problem translating your document.")
                print(f"Document Error Code: {document.error.code}, Message: {document.error.message}\n")

        await translation_client.close()
        await blob_service_client.close()