def test_copy_file_async_private_file_with_sas(self): # Arrange data = b'12345678' * 1024 * 1024 self._create_remote_share() source_file = self._create_remote_file(file_data=data) sas_token = source_file.generate_shared_access_signature( permission=FilePermissions.READ, expiry=datetime.utcnow() + timedelta(hours=1), ) source_url = source_file.url + '?' + sas_token # Act target_file_name = 'targetfile' file_client = FileClient( self.get_file_url(), share=self.share_name, file_path=target_file_name, credential=self.settings.STORAGE_ACCOUNT_KEY) copy_resp = file_client.start_copy_from_url(source_url) # Assert self.assertTrue(copy_resp['copy_status'] in ['success', 'pending']) self._wait_for_async_copy(self.share_name, target_file_name) actual_data = file_client.download_file().content_as_bytes() self.assertEqual(actual_data, data)
def test_get_file_metadata_with_snapshot(self): # Arrange file_client = self._create_file() metadata = {"test1": "foo", "test2": "bar"} file_client.set_file_metadata(metadata) share_client = self.fsc.get_share_client(self.share_name) snapshot = share_client.create_snapshot() snapshot_client = FileClient( self.get_file_url(), share=self.share_name, file_path=file_client.file_name, snapshot=snapshot, credential=self.settings.STORAGE_ACCOUNT_KEY) metadata2 = {"test100": "foo100", "test200": "bar200"} file_client.set_file_metadata(metadata2) # Act file_metadata = file_client.get_file_properties().metadata file_snapshot_metadata = snapshot_client.get_file_properties().metadata # Assert self.assertDictEqual(metadata2, file_metadata) self.assertDictEqual(metadata, file_snapshot_metadata)
def test_abort_copy_file(self): # Arrange data = b'12345678' * 1024 * 1024 self._create_remote_share() source_file = self._create_remote_file(file_data=data) sas_token = source_file.generate_shared_access_signature( permission=FilePermissions.READ, expiry=datetime.utcnow() + timedelta(hours=1), ) source_url = source_file.url + '?' + sas_token # Act target_file_name = 'targetfile' file_client = FileClient(self.get_file_url(), share=self.share_name, file_path=target_file_name, credential=self.settings.STORAGE_ACCOUNT_KEY) copy_resp = file_client.copy_file_from_url(source_url) self.assertEqual(copy_resp.status(), 'pending') copy_resp.abort() # Assert target_file = file_client.download_file() self.assertEqual(target_file.content_as_bytes(), b'') self.assertEqual(target_file.properties.copy.status, 'aborted')
def test_create_file_from_path_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_name = self._get_file_reference() data = self.get_random_bytes(LARGE_FILE_SIZE) with open(INPUT_FILE_PATH, 'wb') as stream: stream.write(data) file_client = FileClient( self.get_file_url(), share=self.share_name, file_path=file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_range_size=4 * 1024) # Act progress = [] def callback(response): current = response.context['upload_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) with open(INPUT_FILE_PATH, 'rb') as stream: file_client.upload_file(stream, max_connections=2, raw_response_hook=callback) # Assert self.assertFileEqual(file_client, data) self.assert_upload_progress( len(data), self.fsc._config.max_range_size, progress, unknown_size=False)
def test_create_file_from_bytes_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_name = self._get_file_reference() data = self.get_random_bytes(LARGE_FILE_SIZE) file_client = FileClient( self.get_file_url(), share=self.share_name, file_path=file_name, credential=self.settings.STORAGE_ACCOUNT_KEY) # Act progress = [] def callback(response): current = response.context['upload_stream_current'] total = response.context['data_stream_total'] if current is not None: progress.append((current, total)) file_client.upload_file(data, max_connections=2, raw_response_hook=callback) # Assert self.assertFileEqual(file_client, data)
def test_create_file_from_stream_non_seekable(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange file_name = self._get_file_reference() data = self.get_random_bytes(LARGE_FILE_SIZE) with open(INPUT_FILE_PATH, 'wb') as stream: stream.write(data) file_client = FileClient(self.get_file_url(), share=self.share_name, file_path=file_name, credential=self.settings.STORAGE_ACCOUNT_KEY) # Act file_size = len(data) with open(INPUT_FILE_PATH, 'rb') as stream: non_seekable_file = StorageFileTest.NonSeekableFile(stream) file_client.upload_file(non_seekable_file, length=file_size, max_connections=1) # Assert self.assertFileEqual(file_client, data[:file_size])
def test_list_ranges_none_from_snapshot(self): # Arrange file_name = self._get_file_reference() file_client = FileClient( self.get_file_url(), share=self.share_name, file_path=file_name, credential=self.settings.STORAGE_ACCOUNT_KEY) file_client.create_file(1024) share_client = self.fsc.get_share_client(self.share_name) snapshot = share_client.create_snapshot() snapshot_client = FileClient( self.get_file_url(), share=self.share_name, file_path=file_client.file_name, snapshot=snapshot, credential=self.settings.STORAGE_ACCOUNT_KEY) file_client.delete_file() # Act ranges = snapshot_client.get_ranges() # Assert self.assertIsNotNone(ranges) self.assertEqual(len(ranges), 0)
def test_delete_file_with_non_existing_file(self): # Arrange file_name = self._get_file_reference() file_client = FileClient(self.get_file_url(), share=self.share_name, file_path=file_name, credential=self.settings.STORAGE_ACCOUNT_KEY) # Act with self.assertRaises(ResourceNotFoundError): file_client.delete_file()
def test_file_not_exists(self): # Arrange file_name = self._get_file_reference() file_client = FileClient(self.get_file_url(), share=self.share_name, file_path="missingdir/" + file_name, credential=self.settings.STORAGE_ACCOUNT_KEY) # Act with self.assertRaises(ResourceNotFoundError): file_client.get_file_properties()
def test_create_file_with_md5_small(self): # Arrange file_name = self._get_file_reference() data = self.get_random_bytes(512) file_client = FileClient(self.get_file_url(), share=self.share_name, file_path=file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_range_size=4 * 1024) # Act file_client.upload_file(data, validate_content=True)
def test_unicode_get_file_unicode_name(self): # Arrange file_name = '啊齄丂狛狜' file_client = FileClient(self.get_file_url(), share=self.share_name, file_path=file_name, credential=self.settings.STORAGE_ACCOUNT_KEY) file_client.upload_file(b'hello world') # Act content = file_client.download_file().content_as_bytes() # Assert self.assertEqual(content, b'hello world')
def test_file_unicode_data(self): # Arrange file_name = self._get_file_reference() file_client = FileClient(self.get_file_url(), share=self.share_name, file_path=file_name, credential=self.settings.STORAGE_ACCOUNT_KEY) # Act data = u'hello world啊齄丂狛狜'.encode('utf-8') file_client.upload_file(data) # Assert content = file_client.download_file().content_as_bytes() self.assertEqual(content, data)
def test_create_file(self): # Arrange file_name = self._get_file_reference() file_client = FileClient(self.get_file_url(), share=self.share_name, file_path=file_name, credential=self.settings.STORAGE_ACCOUNT_KEY) # Act resp = file_client.create_file(1024) # Assert props = file_client.get_file_properties() self.assertIsNotNone(props) self.assertEqual(props.etag, resp['etag']) self.assertEqual(props.last_modified, resp['last_modified'])
def test_create_file_with_md5_large(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange file_name = self._get_file_reference() data = self.get_random_bytes(LARGE_FILE_SIZE) file_client = FileClient(self.get_file_url(), share=self.share_name, file_path=file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_range_size=4 * 1024) # Act file_client.upload_file(data, validate_content=True, max_connections=2)
def test_upload_file_to_share(self): # Instantiate the ShareClient from a connection string from azure.storage.file import ShareClient share = ShareClient.from_connection_string(self.connection_string, share_name="share") # Create the share share.create_share() try: # Instantiate the FileClient from a connection string # [START create_file_client] from azure.storage.file import FileClient file = FileClient.from_connection_string(self.connection_string, share_name="share", file_path="myfile") # [END create_file_client] # Upload a file with open(SOURCE_FILE, "rb") as source_file: file.upload_file(source_file) finally: # Delete the share share.delete_share()
def test_create_file_from_text_with_encoding(self): # Arrange file_name = self._get_file_reference() text = u'hello 啊齄丂狛狜 world' data = text.encode('utf-16') file_client = FileClient(self.get_file_url(), share=self.share_name, file_path=file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_range_size=4 * 1024) # Act file_client.upload_file(text, encoding='UTF-16') # Assert self.assertFileEqual(file_client, data)
def test_shared_write_access_file(self): # SAS URL is calculated from storage key, so this test runs live only if TestMode.need_recording_file(self.test_mode): return # Arrange updated_data = b'updated file data' file_client_admin = self._create_file() token = file_client_admin.generate_shared_access_signature( permission=FilePermissions.WRITE, expiry=datetime.utcnow() + timedelta(hours=1), ) file_client = FileClient( self.get_file_url(), share=self.share_name, file_path=file_client_admin.file_name, credential=token) # Act headers = {'x-ms-range': 'bytes=0-16', 'x-ms-write': 'update'} response = requests.put(file_client.url + '&comp=range', headers=headers, data=updated_data) # Assert self.assertTrue(response.ok) file_content = file_client_admin.download_file().content_as_bytes() self.assertEqual(updated_data, file_content[:len(updated_data)])
def test_shared_read_access_file_with_content_query_params(self): # SAS URL is calculated from storage key, so this test runs live only if TestMode.need_recording_file(self.test_mode): return # Arrange file_client = self._create_file() token = file_client.generate_shared_access_signature( permission=FilePermissions.READ, expiry=datetime.utcnow() + timedelta(hours=1), cache_control='no-cache', content_disposition='inline', content_encoding='utf-8', content_language='fr', content_type='text', ) # Act file_client = FileClient( self.get_file_url(), share=self.share_name, file_path=file_client.file_name, credential=token) response = requests.get(file_client.url) # Assert self.assertEqual(self.short_byte_data, response.content) self.assertEqual(response.headers['cache-control'], 'no-cache') self.assertEqual(response.headers['content-disposition'], 'inline') self.assertEqual(response.headers['content-encoding'], 'utf-8') self.assertEqual(response.headers['content-language'], 'fr') self.assertEqual(response.headers['content-type'], 'text')
def test_account_sas(self): # SAS URL is calculated from storage key, so this test runs live only if TestMode.need_recording_file(self.test_mode): return # Arrange file_client = self._create_file() token = self.fsc.generate_shared_access_signature( ResourceTypes.OBJECT, AccountPermissions.READ, datetime.utcnow() + timedelta(hours=1), ) # Act file_client = FileClient( self.get_file_url(), share=self.share_name, file_path=file_client.file_name, credential=token) response = requests.get(file_client.url) # Assert self.assertTrue(response.ok) self.assertEqual(self.short_byte_data, response.content)
def test_sas_signed_identifier(self): # SAS URL is calculated from storage key, so this test runs live only if TestMode.need_recording_file(self.test_mode): return # Arrange file_client = self._create_file() share_client = self.fsc.get_share_client(self.share_name) access_policy = AccessPolicy() access_policy.start = datetime.utcnow() - timedelta(hours=1) access_policy.expiry = datetime.utcnow() + timedelta(hours=1) access_policy.permission = FilePermissions.READ identifiers = {'testid': access_policy} share_client.set_share_access_policy(identifiers) token = file_client.generate_shared_access_signature(policy_id='testid') # Act sas_file = FileClient( file_client.url, credential=token) content = file_client.download_file().content_as_bytes() # Assert self.assertEqual(self.short_byte_data, content)
def test_copy_file_async_private_file(self): # Arrange self._create_remote_share() source_file = self._create_remote_file() # Act target_file_name = 'targetfile' file_client = FileClient( self.get_file_url(), share=self.share_name, file_path=target_file_name, credential=self.settings.STORAGE_ACCOUNT_KEY) with self.assertRaises(HttpResponseError) as e: file_client.start_copy_from_url(source_file.url) # Assert self.assertEqual(e.exception.error_code, StorageErrorCode.cannot_verify_copy_source)
def test_abort_copy_file_with_synchronous_copy_fails(self): # Arrange source_file = self._create_file() # Act target_file_name = 'targetfile' file_client = FileClient(self.get_file_url(), share=self.share_name, file_path=target_file_name, credential=self.settings.STORAGE_ACCOUNT_KEY) copy_resp = file_client.copy_file_from_url(source_file.url) with self.assertRaises(HttpResponseError): copy_resp.abort() # Assert self.assertEqual(copy_resp.status(), 'success')
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 = FileClient(self.get_file_url(), share=self.share_name, file_path=file_name, credential=self.settings.STORAGE_ACCOUNT_KEY) file_client.upload_file(binary_data) # Act content = file_client.download_file().content_as_bytes() # Assert self.assertEqual(content, binary_data)
def test_create_file_with_metadata(self): # Arrange metadata = {'hello': 'world', 'number': '42'} file_name = self._get_file_reference() file_client = FileClient(self.get_file_url(), share=self.share_name, file_path=file_name, credential=self.settings.STORAGE_ACCOUNT_KEY) # Act resp = file_client.create_file(1024, metadata=metadata) # Assert props = file_client.get_file_properties() self.assertIsNotNone(props) self.assertEqual(props.etag, resp['etag']) self.assertEqual(props.last_modified, resp['last_modified']) self.assertDictEqual(props.metadata, metadata)
def test_copy_file_with_existing_file(self): # Arrange source_client = self._create_file() file_client = FileClient(self.get_file_url(), share=self.share_name, file_path='file1copy', credential=self.settings.STORAGE_ACCOUNT_KEY) # Act copy = file_client.copy_file_from_url(source_client.url) # Assert self.assertIsNotNone(copy) self.assertEqual(copy.status(), 'success') self.assertIsNotNone(copy.copy_id()) copy_file = file_client.download_file().content_as_bytes() self.assertEqual(copy_file, self.short_byte_data)
def test_file_not_exists_with_snapshot(self): # Arrange share_client = self.fsc.get_share_client(self.share_name) snapshot = share_client.create_snapshot() file_client = self._create_file() # Act snapshot_client = FileClient( self.get_file_url(), share=self.share_name, file_path=file_client.file_name, snapshot=snapshot, credential=self.settings.STORAGE_ACCOUNT_KEY) # Assert with self.assertRaises(ResourceNotFoundError): snapshot_client.get_file_properties()
def test_file_exists_with_snapshot(self): # Arrange file_client = self._create_file() share_client = self.fsc.get_share_client(self.share_name) snapshot = share_client.create_snapshot() file_client.delete_file() # Act snapshot_client = FileClient( self.get_file_url(), share=self.share_name, file_path=file_client.file_name, snapshot=snapshot, credential=self.settings.STORAGE_ACCOUNT_KEY) props = snapshot_client.get_file_properties() # Assert self.assertTrue(props)
def test_create_file_from_bytes_with_index(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange file_name = self._get_file_reference() data = self.get_random_bytes(LARGE_FILE_SIZE) index = 1024 file_client = FileClient(self.get_file_url(), share=self.share_name, file_path=file_name, credential=self.settings.STORAGE_ACCOUNT_KEY) # Act file_client.upload_file(data[index:], max_connections=2) # Assert self.assertFileEqual(file_client, data[1024:])
def test_create_file_from_text_chunked_upload(self): # parallel tests introduce random order of requests, can only run live if TestMode.need_recording_file(self.test_mode): return # Arrange file_name = self._get_file_reference() data = self.get_random_text_data(LARGE_FILE_SIZE) encoded_data = data.encode('utf-8') file_client = FileClient(self.get_file_url(), share=self.share_name, file_path=file_name, credential=self.settings.STORAGE_ACCOUNT_KEY, max_range_size=4 * 1024) # Act file_client.upload_file(data) # Assert self.assertFileEqual(file_client, encoded_data)
def test_sas_access_file(self): # SAS URL is calculated from storage key, so this test runs live only if TestMode.need_recording_file(self.test_mode): return # Arrange file_client = self._create_file() token = file_client.generate_shared_access_signature( permission=FilePermissions.READ, expiry=datetime.utcnow() + timedelta(hours=1), ) # Act file_client = FileClient(self.get_file_url(), share=self.share_name, file_path=file_client.file_name, credential=token) content = file_client.download_file().content_as_bytes() # Assert self.assertEqual(self.short_byte_data, content)