Exemple #1
0
    def test_get_file_non_seekable_from_snapshot(self):
        # Arrange
        # Create a snapshot of the share and delete the file
        share_client = self.fsc.get_share_client(self.share_name)
        share_snapshot = share_client.create_snapshot()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY)
        file_client.delete_file()

        snapshot_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            snapshot=share_snapshot,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        with open(FILE_PATH, 'wb') as stream:
            non_seekable_stream = StorageGetFileTest.NonSeekableFile(stream)
            bytes_read = snapshot_client.download_file(
                max_concurrency=1).readinto(non_seekable_stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data, actual)
Exemple #2
0
    def test_get_file_to_stream_from_snapshot(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        # Create a snapshot of the share and delete the file
        share_client = self.fsc.get_share_client(self.share_name)
        share_snapshot = share_client.create_snapshot()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY)
        file_client.delete_file()

        snapshot_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            snapshot=share_snapshot,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = snapshot_client.download_file(
                max_concurrency=2).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data, actual)
Exemple #3
0
    def test_get_file_non_seekable_parallel_from_snapshot(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        # Create a snapshot of the share and delete the file
        share_client = self.fsc.get_share_client(self.share_name)
        share_snapshot = share_client.create_snapshot()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY)
        file_client.delete_file()

        snapshot_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            snapshot=share_snapshot,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        with open(FILE_PATH, 'wb') as stream:
            non_seekable_stream = StorageGetFileTest.NonSeekableFile(stream)

            with self.assertRaises(ValueError):
                snapshot_client.download_file(
                    max_concurrency=2).readinto(non_seekable_stream)
Exemple #4
0
    def test_get_file_to_stream_small(self):
        # Arrange
        file_data = self.get_random_bytes(1024)
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(file_data)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = file_client.download_file(
                raw_response_hook=callback, max_concurrency=1).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(file_data, actual)
        self.assert_download_progress(len(file_data), self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)
Exemple #5
0
    def test_ranged_get_file_to_path_with_single_byte(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        end_range = self.MAX_SINGLE_GET_SIZE + 1024
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = file_client.download_file(offset=0,
                                                   length=1).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(1, len(actual))
            self.assertEqual(self.byte_data[0], actual[0])
Exemple #6
0
    def test_ranged_get_file_to_path_invalid_range_non_parallel(self):

        # Arrange
        file_size = 1024
        file_data = self.get_random_bytes(file_size)
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(file_data)

        # Act
        start = 3
        end_range = 2 * self.MAX_SINGLE_GET_SIZE
        with open(FILE_PATH, 'wb') as stream:
            length = end_range - start + 1
            bytes_read = file_client.download_file(
                offset=start, length=length,
                max_concurrency=1).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(file_data[start:file_size], actual)
Exemple #7
0
    def test_get_file_to_text_with_progress(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        text_file = self.get_resource_name('textfile')
        text_data = self.get_random_text_data(self.MAX_SINGLE_GET_SIZE + 1)
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + text_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(text_data)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        file_content = file_client.download_file(raw_response_hook=callback,
                                                 max_concurrency=2,
                                                 encoding='utf-8').readall()

        # Assert
        self.assertEqual(text_data, file_content)
        self.assert_download_progress(len(text_data.encode('utf-8')),
                                      self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)
Exemple #8
0
    def test_get_file_to_text_non_parallel(self):
        # Arrange
        text_file = self._get_file_reference()
        text_data = self.get_random_text_data(self.MAX_SINGLE_GET_SIZE + 1)
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + text_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(text_data)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        file_content = file_client.download_file(raw_response_hook=callback,
                                                 max_concurrency=1,
                                                 encoding='utf-8').readall()

        # Assert
        self.assertEqual(text_data, file_content)
        self.assert_download_progress(len(text_data), self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)
Exemple #9
0
    def test_get_file_range_with_md5(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        file_content = file_client.download_file(offset=0,
                                                 length=1024,
                                                 validate_content=True)

        # Assert
        self.assertIsNone(file_content.properties.content_settings.content_md5)

        # Arrange
        props = file_client.get_file_properties()
        props.content_settings.content_md5 = b'MDAwMDAwMDA='
        file_client.set_http_headers(props.content_settings)

        # Act
        file_content = file_client.download_file(offset=0,
                                                 length=1024,
                                                 validate_content=True)

        # Assert
        self.assertEqual(b'MDAwMDAwMDA=',
                         file_content.properties.content_settings.content_md5)
Exemple #10
0
    def test_get_file_to_text_with_encoding_and_progress(self):
        # Arrange
        text = u'hello 啊齄丂狛狜 world'
        data = text.encode('utf-16')
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(data)

        # Act
        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        file_content = file_client.download_file(raw_response_hook=callback,
                                                 encoding='UTF-16').readall()

        # Assert
        self.assertEqual(text, file_content)
        self.assert_download_progress(len(data), self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)
    def test_old_api_copy_file_succeeds(self, resource_group, location,
                                        storage_account, storage_account_key):
        fsc = ShareServiceClient(self.account_url(storage_account, "file"),
                                 credential=storage_account_key,
                                 max_range_size=4 * 1024,
                                 api_version=self.api_version_1)
        share = self._create_share(fsc)
        file_name = self._get_file_reference()

        source_client = share.get_file_client(file_name)
        source_client.upload_file(self.short_byte_data)
        source_prop = source_client.get_file_properties()

        file_client = ShareFileClient(self.account_url(storage_account,
                                                       "file"),
                                      share_name=share.share_name,
                                      file_path='file1copy',
                                      credential=storage_account_key,
                                      api_version=self.api_version_1)

        # Act
        copy = file_client.start_copy_from_url(source_client.url)

        # Assert
        dest_prop = file_client.get_file_properties()
        # to make sure the acl is copied from source
        self.assertEqual(source_prop['permission_key'],
                         dest_prop['permission_key'])

        self.assertIsNotNone(copy)
        self.assertEqual(copy['copy_status'], 'success')
        self.assertIsNotNone(copy['copy_id'])

        copy_file = file_client.download_file().readall()
        self.assertEqual(copy_file, self.short_byte_data)
    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'."))
Exemple #13
0
    def test_get_file_to_stream_with_progress(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = file_client.download_file(
                raw_response_hook=callback, max_concurrency=2).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data, actual)
        self.assert_download_progress(len(self.byte_data),
                                      self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)
Exemple #14
0
    def setUp(self):
        super(StorageGetFileTest, self).setUp()

        # test chunking functionality by reducing the threshold
        # for chunking and the size of each chunk, otherwise
        # the tests would take too long to execute
        self.MAX_SINGLE_GET_SIZE = 32 * 1024
        self.MAX_CHUNK_GET_SIZE = 4 * 1024

        url = self.get_file_url()
        credential = self.get_shared_key_credential()

        self.fsc = ShareServiceClient(
            url,
            credential=credential,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        self.share_name = self.get_resource_name('utshare')
        self.directory_name = self.get_resource_name('utdir')

        if not self.is_playback():
            share = self.fsc.create_share(self.share_name)
            share.create_directory(self.directory_name)

        self.byte_file = self.get_resource_name('bytefile')
        self.byte_data = self.get_random_bytes(64 * 1024 + 5)

        if not self.is_playback():
            byte_file = self.directory_name + '/' + self.byte_file
            file_client = ShareFileClient(self.get_file_url(),
                                          share_name=self.share_name,
                                          file_path=byte_file,
                                          credential=credential)
            file_client.upload_file(self.byte_data)
    def test_file_client_api_version_property(self):
        file_client = ShareFileClient(
            "https://foo.file.core.windows.net/account",
            "share",
            self._get_file_reference(),
            credential="fake_key")
        self.assertEqual(file_client.api_version, self.api_version_2)
        self.assertEqual(file_client._client._config.version, self.api_version_2)

        file_client = ShareFileClient(
            "https://foo.file.core.windows.net/account",
            "share",
            self._get_file_reference(),
            credential="fake_key",
            api_version=self.api_version_1)
        self.assertEqual(file_client.api_version, self.api_version_1)
        self.assertEqual(file_client._client._config.version, self.api_version_1)
Exemple #16
0
    def test_get_file_to_stream_non_parallel_from_snapshot(self):
        # Arrange
        # Create a snapshot of the share and delete the file
        share_client = self.fsc.get_share_client(self.share_name)
        share_snapshot = share_client.create_snapshot()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY)
        file_client.delete_file()

        snapshot_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            snapshot=share_snapshot,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = snapshot_client.download_file(
                raw_response_hook=callback, max_concurrency=1).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data, actual)
        self.assert_download_progress(len(self.byte_data),
                                      self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)
Exemple #17
0
    def test_unicode_get_file_unicode_data(self):
        # Arrange
        file_data = u'hello world啊齄丂狛狜'.encode('utf-8')
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(file_data)

        # Act
        file_content = file_client.download_file().readall()

        # Assert
        self.assertEqual(file_content, file_data)
Exemple #18
0
    def test_get_file_to_text_with_encoding(self):
        # Arrange
        text = u'hello 啊齄丂狛狜 world'
        data = text.encode('utf-16')
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(data)

        # Act
        file_content = file_client.download_file(encoding='UTF-16').readall()

        # Assert
        self.assertEqual(text, file_content)
Exemple #19
0
    def test_get_file_properties_server_encryption(self):

        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        props = file_client.get_file_properties()

        # Assert
        if self.is_file_encryption_enabled():
            self.assertTrue(props.server_encrypted)
        else:
            self.assertFalse(props.server_encrypted)
Exemple #20
0
    def test_get_file_no_content(self):
        # Arrange
        file_data = b''
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(file_data)

        # Act
        file_output = file_client.download_file()

        # Assert
        self.assertEqual(file_data, file_output.readall())
        self.assertEqual(0, file_output.properties.size)
Exemple #21
0
    def test_get_file_to_bytes(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        file_content = file_client.download_file(max_concurrency=2).readall()

        # Assert
        self.assertEqual(self.byte_data, file_content)
Exemple #22
0
    def test_unicode_get_file_binary_data(self):
        # Arrange
        base64_data = 'AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/wABAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4fICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9AQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVpbXF1eX2BhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ent8fX5/gIGCg4SFhoeIiYqLjI2Oj5CRkpOUlZaXmJmam5ydnp+goaKjpKWmp6ipqqusra6vsLGys7S1tre4ubq7vL2+v8DBwsPExcbHyMnKy8zNzs/Q0dLT1NXW19jZ2tvc3d7f4OHi4+Tl5ufo6err7O3u7/Dx8vP09fb3+Pn6+/z9/v8AAQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXp7fH1+f4CBgoOEhYaHiImKi4yNjo+QkZKTlJWWl5iZmpucnZ6foKGio6SlpqeoqaqrrK2ur7CxsrO0tba3uLm6u7y9vr/AwcLDxMXGx8jJysvMzc7P0NHS09TV1tfY2drb3N3e3+Dh4uPk5ebn6Onq6+zt7u/w8fLz9PX29/j5+vv8/f7/AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w=='
        binary_data = base64.b64decode(base64_data)

        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(binary_data)

        # Act
        file_content = file_client.download_file().readall()

        # Assert
        self.assertEqual(file_content, binary_data)
Exemple #23
0
    def test_ranged_get_file_to_path_non_parallel(self):
        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = file_client.download_file(
                offset=1, length=3, max_concurrency=1).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data[1:4], actual)
Exemple #24
0
    def test_ranged_get_file_to_bytes_with_zero_byte(self):
        # Arrange
        file_data = b''
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(file_data)

        # Act
        # the get request should fail in this case since the blob is empty and yet there is a range specified
        with self.assertRaises(HttpResponseError):
            file_client.download_file(offset=0, length=5).readall()

        with self.assertRaises(HttpResponseError):
            file_client.download_file(offset=3, length=5).readall()
Exemple #25
0
    def test_get_file_server_encryption(self):

        #Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        file_content = file_client.download_file(offset=0,
                                                 length=1024,
                                                 validate_content=True)

        # Assert
        if self.is_file_encryption_enabled():
            self.assertTrue(file_content.properties.server_encrypted)
        else:
            self.assertFalse(file_content.properties.server_encrypted)
Exemple #26
0
    def test_get_file_with_iter(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        with open(FILE_PATH, 'wb') as stream:
            for data in file_client.download_file().chunks():
                stream.write(data)
        # Assert
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data, actual)
Exemple #27
0
    def test_shared_access_share(self, storage_account_name,
                                 storage_account_key):
        # SAS URL is calculated from storage key, so this test runs live only
        if not self.is_live:
            return

        self._setup(storage_account_name, storage_account_key)
        file_name = 'file1'
        dir_name = 'dir1'
        data = b'hello world'

        share = self._create_share()
        dir1 = share.create_directory(dir_name)
        dir1.upload_file(file_name, data)

        token = generate_share_sas(
            share.account_name,
            share.share_name,
            share.credential.account_key,
            expiry=datetime.utcnow() + timedelta(hours=1),
            permission=ShareSasPermissions(read=True),
        )
        sas_client = ShareFileClient(
            self.account_url(storage_account_name, "file"),
            share_name=share.share_name,
            file_path=dir_name + '/' + file_name,
            credential=token,
        )

        # Act
        print(sas_client.url)
        response = requests.get(sas_client.url)

        # Assert
        self.assertTrue(response.ok)
        self.assertEqual(data, response.content)
        self._delete_shares()
Exemple #28
0
    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?'))