class BlobBasicSamples():
    def __init__(self):
        self.random_data = RandomData()

    # Runs all samples for Azure Storage Blob service.
    # Input Arguments:
    # account - CloudStorageAccount to use for running the samples
    def run_all_samples(self, account):
        print('\n\nAzure Storage Blob sample - Starting.')

        try:
            # Block blob basics
            print('\n\n* Basic block blob operations *\n')
            self.basic_blockblob_operations(account)

            # Page blob basics
            print('\n\n* Basic page blob operations *\n')
            self.basic_pageblob_operations(account)

            # Snapshot
            print('\n\n* Snapshot sample *\n')
            self.basic_snapshot(account)

            if (config.IS_EMULATED == False):
                # Append blob basics
                # Append blob is not yet supported in the Emulator
                print('\n\n* Basic append blob operations *\n')
                self.basic_appendblob_operations(account)

        except Exception as e:
            if (config.IS_EMULATED):
                print(
                    'Error occurred in the sample. If you are using the emulator, please make sure the emulator is running.',
                    e)
            else:
                print(
                    'Error occurred in the sample. Please make sure the account name and key are correct.',
                    e)

        finally:
            print('\nAzure Storage Blob sample - Completed.\n')

    # Runs basic block blob samples for Azure Storage Blob service.
    # Input Arguments:
    # account - CloudStorageAccount to use for running the samples
    def basic_blockblob_operations(self, account):
        blob_name1 = "blob1"
        blob_name2 = "blob2"
        file_to_upload = "HelloWorld.png"

        # Create a Block Blob Service object
        blockblob_service = account.create_block_blob_service()
        #blockblob_service = BlockBlobService(account_name=config.STORAGE_ACCOUNT_NAME, account_key=config.STORAGE_ACCOUNT_KEY)
        container_name = 'blockblobbasicscontainer' + self.random_data.get_random_name(
            6)

        try:
            # Create a new container
            print('1. Create a container with name - ' + container_name)
            blockblob_service.create_container(container_name)

            # Create blobs
            print('2. Create blobs')
            # Get full path on drive to file_to_upload by joining the fully qualified directory name and file name on the local drive
            full_path_to_file = os.path.join(os.path.dirname(__file__),
                                             file_to_upload)
            blockblob_service.create_blob_from_path(container_name, blob_name1,
                                                    full_path_to_file)
            # Create blob from text
            blockblob_service.create_blob_from_text(
                container_name, blob_name2,
                self.random_data.get_random_name(256))

            # List all the blobs in the container
            print('3. List Blobs in Container')
            generator = blockblob_service.list_blobs(container_name)
            for blob in generator:
                print('\tBlob Name: ' + blob.name)

            # Download the blob
            print('4. Download the blob')
            blockblob_service.get_blob_to_path(
                container_name, blob_name1,
                os.path.join(os.path.dirname(__file__),
                             file_to_upload + '.copy.png'))
            blockblob_service.get_blob_to_path(
                container_name, blob_name2,
                os.path.join(os.path.dirname(__file__), 'blob2.copy.txt'))

            # Delete the blobs, this can be ommited because the container is deleted
            print('5. Delete blobs')
            blockblob_service.delete_blob(container_name, blob_name1)
            blockblob_service.delete_blob(container_name, blob_name2)
        finally:
            # Delete the container
            print("6. Delete Container")
            if blockblob_service.exists(container_name):
                blockblob_service.delete_container(container_name)

    # Runs basic page blob samples for Azure Storage Blob service.
    # Input Arguments:
    # account - CloudStorageAccount to use for running the samples
    def basic_pageblob_operations(self, account):
        file_to_upload = "HelloPageBlobWorld.txt"

        # Create a block blob service object
        pageblob_service = account.create_page_blob_service()
        container_name = 'pageblobbasicscontainer' + self.random_data.get_random_name(
            6)

        try:
            # Create a new container
            print('1. Create a container with name - ' + container_name)
            pageblob_service.create_container(container_name)

            # Create a page blob
            print('2. Creating Page Blob')
            pageblob_service.create_blob_from_bytes(
                container_name, file_to_upload,
                self.random_data.get_random_bytes(512))

            # List all the blobs in the container
            print('3. List Blobs in Container')
            blob_list = pageblob_service.list_blobs(container_name)
            for blob in blob_list:
                print('\tBlob Name: ' + blob.name)

            # Read a page blob
            print('4. Reading a Page Blob')
            readblob = pageblob_service.get_blob_to_bytes(
                container_name,  # name of the container
                file_to_upload,  # name of blob to read
                start_range=3,  # page to start reading from
                end_range=10)  # page to stop reading at

            # Delete the blob, this can be ommited because the container is deleted
            print('5. Delete Page Blob')
            pageblob_service.delete_blob(container_name, file_to_upload)
        finally:
            # Delete the container
            print("6. Delete Container")
            if pageblob_service.exists(container_name):
                pageblob_service.delete_container(container_name)

    # Runs basic append blob samples for Azure Storage Blob service.
    # Input Arguments:
    # account - CloudStorageAccount to use for running the samples
    def basic_appendblob_operations(self, account):
        file_to_upload = "HelloAppendBlobWorld.txt"

        # Create an append blob service object
        appendblob_service = account.create_append_blob_service()
        container_name = 'appendblobbasicscontainer' + self.random_data.get_random_name(
            6)

        try:
            # Create a new container
            print('1. Create a container with name - ' + container_name)
            appendblob_service.create_container(container_name)

            # Create an append blob
            print('2. Create Append Blob')
            appendblob_service.create_blob(container_name, file_to_upload)

            # Write to an append blob
            print('3. Write to Append Blob')
            appendblob_service.append_blob_from_text(
                container_name, file_to_upload, '\tHello Append Blob world!\n')
            appendblob_service.append_blob_from_text(
                container_name, file_to_upload,
                '\tHello Again Append Blob world!')

            # List all the blobs in the container
            print('4. List Blobs in Container')
            generator = appendblob_service.list_blobs(container_name)
            for blob in generator:
                print('\tBlob Name: ' + blob.name)

            # Read the blob
            print('5. Read Append blob')
            append_blob = appendblob_service.get_blob_to_text(
                container_name, file_to_upload)
            print(append_blob.content)

            # Delete the blob, this can be ommited because the container is deleted
            print('6. Delete Append Blob')
            appendblob_service.delete_blob(container_name, file_to_upload)
        finally:
            # Delete the container
            print("7. Delete Container")
            if appendblob_service.exists(container_name):
                appendblob_service.delete_container(container_name)

    # Runs a snapthot sample for Azure Storage Blob service.
    # Input Arguments:
    # account - CloudStorageAccount to use for running the samples
    def basic_snapshot(self, account):
        blob_name = "blob1"
        file_to_upload = "HelloWorld.png"

        # Create a Block Blob Service object
        blockblob_service = account.create_block_blob_service()
        container_name = 'blockblobbasicscontainer' + self.random_data.get_random_name(
            6)

        try:
            # Create a new container
            print('1. Create a container with name - ' + container_name)
            blockblob_service.create_container(container_name)

            # Create blobs
            print('2. Create a blob')
            # Get full path on drive to file_to_upload by joining the fully qualified directory name and file name on the local drive
            full_path_to_file = os.path.join(os.path.dirname(__file__),
                                             file_to_upload)
            blockblob_service.create_blob_from_path(container_name, blob_name,
                                                    full_path_to_file)

            # Create a read-only snapshot of the blob
            print('3. Create a snapshot')
            snapshot = blockblob_service.snapshot_blob(container_name,
                                                       blob_name)

        finally:
            # Delete the container
            print("4. Delete Container")
            if blockblob_service.exists(container_name):
                blockblob_service.delete_container(container_name)
class BlobAdvancedSamples():
    def __init__(self):
        self.random_data = RandomData()

    # Runs all samples for Azure Storage Blob service.
    # Input Arguments:
    # account - CloudStorageAccount to use for running the samples
    def run_all_samples(self, account):
        print('\n\nAzure Storage Blob advanced sample - Starting.')

        try:
            print('\n\n* Container operations *\n')
            self.list_containers(account)

            print('\n\n* Set CORS *\n')
            self.set_cors_rules(account)

            print('\n\n* Container lease *\n')
            self.lease_container(account)

            print('\n\n* Copy blob *\n')
            self.copy_blob(account)

            print('\n\n* Page blob operations *\n')
            self.page_blob_operations(account)

            print('\n\n* Block blob operations *\n')
            self.block_blob_operations(account)

            print('\n\n* Properties and Metadata operations *\n')
            self.properties_and_metadata_operations(account)

            print('\n\n* Container ACL operations *\n')
            self.container_acl_operations(account)

            print('\n\n* Blob lease *\n')
            self.lease_blob(account)

            if (config.IS_EMULATED):
                print('\nShared Access Signature is not supported in emulator')
            else:
                print('\n\n* Container with SAS operations *\n')
                self.container_operations_with_sas(account)

                print('\n\n* SAS with access policy *\n')
                self.sas_with_container_access_policy(account)

                print(
                    '\n\n* Set blob service logging and metrics properties *\n'
                )
                self.set_service_properties(account)

        except Exception as e:
            if (config.IS_EMULATED):
                print(
                    'Error occurred in the sample. If you are using the emulator, please make sure the emulator is running.',
                    e)
            else:
                print(
                    'Error occurred in the sample. Please make sure the account name and key are correct.',
                    e)

        finally:
            print('\nAzure Storage Blob advanced sample - Completed.\n')

    # Copy a source blob to a destination blob
    def copy_blob(self, account):

        file_upload = "HelloWorld.png"
        container_name = 'blockblobcontainer' + self.random_data.get_random_name(
            6)

        # Create a Block Blob Service object
        blockblob_service = account.create_block_blob_service()

        try:
            # Create a new container
            print('1. Create a container with name - ' + container_name)
            blockblob_service.create_container(container_name)

            # Upload file as a block blob
            print('2. Upload BlockBlob')
            #Get full path on drive to file_to_upload by joining the fully qualified directory name and file name on the local drive
            full_path_to_file = os.path.join(os.path.dirname(__file__),
                                             file_upload)
            blockblob_service.create_blob_from_path(container_name,
                                                    file_upload,
                                                    full_path_to_file)

            target_blob = "target.png"
            blob_source_url = blockblob_service.make_blob_url(
                container_name, file_upload)

            print('3. Copy blob')
            blockblob_service.copy_blob(container_name, target_blob,
                                        blob_source_url)

            print('4. Get target blob')
            target_blob_properties = blockblob_service.get_blob_properties(
                container_name, target_blob)

            print('5. Get copy properties')
            copy_properties = target_blob_properties.properties.copy

            print('Copy properties status: ' + copy_properties.status)

            if (copy_properties.status == "pending"):
                print('6. Abort copy')
                blockblob_service.abort_copy_blob(container_name, blob_name,
                                                  copy_properties.id)
        finally:
            # Delete the container
            print("7. Delete Container")
            if blockblob_service.exists(container_name):
                blockblob_service.delete_container(container_name)

    def sas_with_container_access_policy(self, account):
        container_name = 'demosasblobcontainer' + self.random_data.get_random_name(
            6)

        blockblob_service = account.create_block_blob_service()

        try:
            print('1. Create a container with name - ' + container_name)
            blockblob_service.create_container(container_name)

            print('2. Create blob "blo1" with text')
            blockblob_service.create_blob_from_text(container_name, 'blob1',
                                                    b'hello world')

            print('3. Set access policy for container')
            # Set access policy on container
            access_policy = AccessPolicy(permission=ContainerPermissions.READ,
                                         expiry=datetime.datetime.utcnow() +
                                         datetime.timedelta(hours=1))
            identifiers = {'id': access_policy}
            acl = blockblob_service.set_container_acl(container_name,
                                                      identifiers)

            # Wait 30 seconds for acl to propagate
            print('Wait 30 seconds for acl to propagate')
            time.sleep(30)

            print('4. Get sas for access policy in container')
            # Indicates to use the access policy set on the container
            sas = blockblob_service.generate_container_shared_access_signature(
                container_name, id='id')

            print('5. Create blob service with sas')
            # Create a service and use the SAS
            shared_blockblob_service = BlockBlobService(
                account_name=account.account_name,
                sas_token=sas,
            )

            print('6. Read blob content with sas')
            blob = shared_blockblob_service.get_blob_to_text(
                container_name, 'blob1')
            content = blob.content  # hello world
        finally:
            print('7. Delete container')
            blockblob_service.delete_container(container_name)

        print("SAS with access policy sample completed")

    def container_operations_with_sas(self, account):
        container_name = 'demosasblobcontainer' + self.random_data.get_random_name(
            6)

        # Create a Block Blob Service object
        blockblob_service = account.create_block_blob_service()

        # Create a Shared Access Signature for the account
        print('1.Get account sas')

        account_sas = blockblob_service.generate_account_shared_access_signature(
            ResourceTypes.CONTAINER + ResourceTypes.OBJECT,
            AccountPermissions.READ + AccountPermissions.WRITE +
            AccountPermissions.DELETE + AccountPermissions.LIST +
            AccountPermissions.CREATE,
            datetime.datetime.utcnow() + datetime.timedelta(hours=1))

        shared_account = CloudStorageAccount(account_name=account.account_name,
                                             sas_token=account_sas)
        shared_account_block_service = shared_account.create_block_blob_service(
        )

        try:
            print('2. Create container with account sas. Container name - ' +
                  container_name)
            shared_account_block_service.create_container(container_name)

            # For the purposes of the demo, get a Container SAS
            # In a real-world application, the above Account SAS can be used
            print('3. Get container sas')
            container_sas = blockblob_service.generate_container_shared_access_signature(
                container_name,
                ContainerPermissions.READ + ContainerPermissions.WRITE +
                ContainerPermissions.DELETE + ContainerPermissions.LIST,
                datetime.datetime.utcnow() + datetime.timedelta(hours=1))

            shared_container_account = CloudStorageAccount(
                account_name=account.account_name, sas_token=container_sas)
            shared_container_block_service = shared_container_account.create_block_blob_service(
            )

            print('4. Create blob with container sas')
            shared_container_block_service.create_blob_from_text(
                container_name, 'myblob', 'blob data')

            print('5. List blobs with container sas')
            blobs = shared_container_block_service.list_blobs(container_name)
            for blob in blobs:
                print('blob ' + blob.name)

            print('6. Delete blob with container sas')
            shared_container_block_service.delete_blob(container_name,
                                                       'myblob')
        finally:
            print('7. Delete container')
            blockblob_service.delete_container(container_name)

        print("Containers Sas sample completed")

    def list_containers(self, account):

        container_prefix = 'blockblobcontainers' + self.random_data.get_random_name(
            6)

        # Create a Block Blob Service object
        blockblob_service = account.create_block_blob_service()

        try:
            # Create containers
            for i in range(5):
                container_name = container_prefix + str(i)
                print('1. Create a container with name - ' + container_name)
                blockblob_service.create_container(container_name)

            # List all the blobs in the container
            print('2. List containers with prefix ' + container_prefix)
            containers = blockblob_service.list_containers(container_prefix)
            for container in containers:
                print('\tContainer Name: ' + container.name)
        finally:
            # Delete the containers
            print("3. Delete Containers")
            for i in range(5):
                container_name = container_prefix + str(i)
                if blockblob_service.exists(container_name):
                    blockblob_service.delete_container(container_name)

        print("Containers sample completed")

    def container_acl_operations(self, account):

        container_name = 'aclblockblobcontainer' + self.random_data.get_random_name(
            6)

        # Create a Block Blob Service object
        blockblob_service = account.create_block_blob_service()

        try:
            print('1. Create a container with name - ' + container_name)
            blockblob_service.create_container(container_name)

            print('2. Set access policy for container')
            access_policy = AccessPolicy(permission=ContainerPermissions.READ,
                                         expiry=datetime.datetime.utcnow() +
                                         datetime.timedelta(hours=1))
            identifiers = {'id': access_policy}
            blockblob_service.set_container_acl(container_name, identifiers)

            print('3. Get access policy from container')
            acl = blockblob_service.get_container_acl(container_name)

            print('4. Clear access policy in container')
            # Clear
            blockblob_service.set_container_acl(container_name)

        finally:
            print('5. Delete container')
            blockblob_service.delete_container(container_name)

        print("Container ACL operations sample completed")

    def properties_and_metadata_operations(self, account):
        file_blob_name = "HelloWorld.png"
        text_blob_name = "Text"

        # Create a Block Blob Service object
        blockblob_service = account.create_block_blob_service()

        container_name = 'blockblobbasicscontainer' + self.random_data.get_random_name(
            6)

        try:
            # Create a new container
            print('1. Create a container with name and custom metadata - ' +
                  container_name)
            blockblob_service.create_container(container_name,
                                               {'sample': 'azure-storage'})

            # Upload file as a block blob
            print(
                '2. Uploading BlockBlob from file with properties and custom metadata'
            )
            #Get full path on drive to file_to_upload by joining the fully qualified directory name and file name on the local drive
            full_path_to_file = os.path.join(os.path.dirname(__file__),
                                             file_blob_name)

            blockblob_service.create_blob_from_path(
                container_name,
                file_blob_name,
                full_path_to_file,
                content_settings=ContentSettings(
                    content_type='application/png'),
                metadata={'category': 'azure-samples'})

            blockblob_service.create_blob_from_text(
                container_name,
                text_blob_name,
                'Data',
                content_settings=ContentSettings(content_encoding='UTF-8',
                                                 content_language='en'),
                metadata={
                    'origin': 'usa',
                    'title': 'azure-samples'
                })

            # Get all the container properties
            print('3. Get Container metadata')

            container = blockblob_service.get_container_properties(
                container_name)

            print('    Metadata:')

            for key in container.metadata:
                print('        ' + key + ':' + container.metadata[key])

            # Get all the blob properties
            print('4. Get Blob properties')
            blob = blockblob_service.get_blob_properties(
                container_name, file_blob_name)

            print('    Metadata:')
            for key in blob.metadata:
                print('        ' + key + ':' + blob.metadata[key])

            print('    Properties:')
            print('        Content-Type:' +
                  blob.properties.content_settings.content_type)
        finally:
            # Delete the container
            print("5. Delete Container")
            if blockblob_service.exists(container_name):
                blockblob_service.delete_container(container_name)

    # Set CORS
    def set_cors_rules(self, account):

        # Create a Block Blob Service object
        blockblob_service = account.create_block_blob_service()

        cors_rule = CorsRule(allowed_origins=['*'],
                             allowed_methods=['POST', 'GET'],
                             allowed_headers=['*'],
                             exposed_headers=['*'],
                             max_age_in_seconds=3600)

        print('1. Get Cors Rules')
        original_cors_rules = blockblob_service.get_blob_service_properties(
        ).cors

        try:
            print('2. Overwrite Cors Rules')
            blockblob_service.set_blob_service_properties(cors=[cors_rule])
        finally:
            print('3. Revert Cors Rules back the original ones')
            #reverting cors rules back to the original ones
            blockblob_service.set_blob_service_properties(
                cors=original_cors_rules)

        print("CORS sample completed")

    # Lease Container
    def lease_container(self, account):
        # Create a Block Blob Service object
        blockblob_service = account.create_block_blob_service()

        try:
            container_name = 'blockblobcontainer' + self.random_data.get_random_name(
                6)
            print('1. Create a container with name - ' + container_name)
            blockblob_service.create_container(container_name)

            print('2. Acquire lease on container')
            lease_id = blockblob_service.acquire_container_lease(
                container_name, lease_duration=15)

            print("3. Deleted container without lease")
            try:
                blockblob_service.delete_container(container_name)
            except:
                print(
                    'Got expected exception. Cannot delete container, lease not specified'
                )
        finally:
            print("4. Delete container with lease")
            blockblob_service.delete_container(container_name,
                                               lease_id=lease_id)

        print("Lease container sample completed")

    # Lease Blob
    def lease_blob(self, account):
        blob_name = "exclusive"

        # Create an block blob service object
        blockblob_service = account.create_block_blob_service()
        container_name = 'blobcontainer' + self.random_data.get_random_name(6)

        try:
            # Create a new container
            print('1. Create a container with name - ' + container_name)
            blockblob_service.create_container(container_name)

            # Create a block blob
            print('2. Create Block Blob')
            blob = self.random_data.get_random_bytes(255)
            blockblob_service.create_blob_from_bytes(container_name, blob_name,
                                                     blob)

            print('3. Acquire lease on blob')
            lease_id = blockblob_service.acquire_blob_lease(container_name,
                                                            blob_name,
                                                            lease_duration=15)

            # Write to a block blob
            print('4. Try to write to Block Blob without lease')
            block_id = self.random_data.get_random_name(32)
            block = self.random_data.get_random_bytes(255)
            try:
                blockblob_service.put_block(container_name, blob_name, block,
                                            block_id)
            except:
                print(
                    'Got expected exception. Cannot write blob, lease not specified'
                )

            print('5. Write to Block Blob with lease')
            blockblob_service.put_block(container_name,
                                        blob_name,
                                        block,
                                        block_id,
                                        lease_id=lease_id)

            print("6. Deleted blob without lease")
            try:
                blockblob_service.delete_blob(container_name, blob_name)
            except:
                print(
                    'Got expected exception. Cannot delete blob, lease not specified'
                )

            print("7. Delete blob with lease")
            blockblob_service.delete_blob(container_name,
                                          blob_name,
                                          lease_id=lease_id)
        finally:
            print("8. Delete container")
            if blockblob_service.exists(container_name):
                blockblob_service.delete_container(container_name)

        print("Lease blob sample completed")

    #Page Blob Operations
    def page_blob_operations(self, account):
        file_to_upload = "HelloWorld.png"
        page_size = 1024

        # Create an page blob service object
        pageblob_service = account.create_page_blob_service()
        container_name = 'pageblobcontainer' + self.random_data.get_random_name(
            6)

        try:
            # Create a new container
            print('1. Create a container with name - ' + container_name)
            pageblob_service.create_container(container_name)

            # Create a new page blob to upload the file
            print('2. Create a page blob')
            pageblob_service.create_blob(container_name, file_to_upload,
                                         page_size * 1024)

            # Read the file
            print('3. Upload pages to page blob')
            index = 0
            with open(file_to_upload, "rb") as file:
                file_bytes = file.read(page_size)
                while len(file_bytes) > 0:
                    if len(file_bytes) < page_size:
                        file_bytes = bytes(file_bytes +
                                           bytearray(page_size -
                                                     len(file_bytes)))

                    pageblob_service.update_page(
                        container_name, file_to_upload, file_bytes,
                        index * page_size, index * page_size + page_size - 1)

                    file_bytes = file.read(page_size)

                    index = index + 1

            pages = pageblob_service.get_page_ranges(container_name,
                                                     file_to_upload)

            print('4. Enumerate pages in page blob')
            for page in pages:
                print('Page ' + str(page.start) + ' - ' + str(page.end))
        finally:
            print('5. Delete container')
            if pageblob_service.exists(container_name):
                pageblob_service.delete_container(container_name)

    #Block Blob Operations
    def block_blob_operations(self, account):
        file_to_upload = "HelloWorld.png"
        block_size = 1024

        # Create an page blob service object
        blockblob_service = account.create_block_blob_service()
        container_name = 'blockblobcontainer' + self.random_data.get_random_name(
            6)

        try:
            # Create a new container
            print('1. Create a container with name - ' + container_name)
            blockblob_service.create_container(container_name)

            blocks = []

            # Read the file
            print('2. Upload file to block blob')
            with open(file_to_upload, "rb") as file:
                file_bytes = file.read(block_size)
                while len(file_bytes) > 0:
                    block_id = self.random_data.get_random_name(32)
                    blockblob_service.put_block(container_name, file_to_upload,
                                                file_bytes, block_id)

                    blocks.append(BlobBlock(id=block_id))

                    file_bytes = file.read(block_size)

            blockblob_service.put_block_list(container_name, file_to_upload,
                                             blocks)

            print('3. Get the block list')
            blockslist = blockblob_service.get_block_list(
                container_name, file_to_upload, None, 'all')
            blocks = blockslist.committed_blocks

            print('4. Enumerate blocks in block blob')
            for block in blocks:
                print('Block ' + block.id)
        finally:
            print('5. Delete container')
            if blockblob_service.exists(container_name):
                blockblob_service.delete_container(container_name)

    # Manage properties of the Blob service, including logging and metrics settings, and the default service version.
    def set_service_properties(self, account):

        # Create an page blob service object
        blockblob_service = account.create_block_blob_service()

        print('1. Get Blob service properties')
        props = blockblob_service.get_blob_service_properties()

        retention = RetentionPolicy(enabled=True, days=5)
        logging = Logging(delete=True,
                          read=False,
                          write=True,
                          retention_policy=retention)
        hour_metrics = Metrics(enabled=True,
                               include_apis=True,
                               retention_policy=retention)
        minute_metrics = Metrics(enabled=False)

        try:
            print('2. Ovewrite Blob service properties')
            blockblob_service.set_blob_service_properties(
                logging=logging,
                hour_metrics=hour_metrics,
                minute_metrics=minute_metrics,
                target_version='2015-04-05')
        finally:
            print(
                '3. Revert Blob service properties back to the original ones')
            blockblob_service.set_blob_service_properties(
                logging=props.logging,
                hour_metrics=props.hour_metrics,
                minute_metrics=props.minute_metrics,
                target_version='2015-04-05')

        print('4. Set Blob service properties completed')