async def _test_using_directory_sas_to_read(self): # SAS URL is calculated from storage key, so this test runs live only if TestMode.need_recording_file(self.test_mode): return client = await self._create_directory_and_get_directory_client() directory_name = client.path_name # generate a token with directory level read permission token = generate_directory_sas( self.dsc.account_name, self.file_system_name, directory_name, self.dsc.credential.account_key, permission=DirectorySasPermissions(read=True), expiry=datetime.utcnow() + timedelta(hours=1), ) directory_client = DataLakeDirectoryClient(self.dsc.url, self.file_system_name, directory_name, credential=token) access_control = await directory_client.get_access_control() self.assertIsNotNone(access_control)
async def test_get_access_control_using_delegation_sas_async( self, datalake_storage_account_name, datalake_storage_account_key): self._setUp(datalake_storage_account_name, datalake_storage_account_key) url = self._get_account_url(datalake_storage_account_name) token_credential = self.generate_oauth_token() dsc = DataLakeServiceClient(url, token_credential, logging_enable=True) file_system_name = self._get_file_system_reference() directory_client_name = '/' (await dsc.create_file_system(file_system_name)).get_directory_client(directory_client_name) directory_client = self.dsc.get_directory_client(file_system_name, directory_client_name) random_guid = uuid.uuid4() await directory_client.set_access_control(owner=random_guid, permissions='0777') acl = await directory_client.get_access_control() delegation_key = await dsc.get_user_delegation_key(datetime.utcnow(), datetime.utcnow() + timedelta(hours=1)) token = generate_file_system_sas( dsc.account_name, file_system_name, delegation_key, permission=FileSystemSasPermissions( read=True, execute=True, manage_access_control=True, manage_ownership=True), expiry=datetime.utcnow() + timedelta(hours=1), agent_object_id=random_guid ) sas_directory_client = DataLakeDirectoryClient(self.dsc.url, file_system_name, directory_client_name, credential=token, logging_enable=True) access_control = await sas_directory_client.get_access_control() self.assertIsNotNone(access_control)
async def _test_using_oauth_token_credential_to_create_directory(self): # generate a token with directory level create permission directory_name = self._get_directory_reference() token_credential = self.generate_async_oauth_token() directory_client = DataLakeDirectoryClient(self.dsc.url, self.file_system_name, directory_name, credential=token_credential) response = await directory_client.create_directory() self.assertIsNotNone(response)
async def _test_file_sas_only_applies_to_file_level(self): # SAS URL is calculated from storage key, so this test runs live only if TestMode.need_recording_file(self.test_mode): return file_name = self._get_file_reference() directory_name = self._get_directory_reference() await self._create_file_and_return_client(directory=directory_name, file=file_name) # generate a token with file level read and write permissions token = generate_file_sas( self.dsc.account_name, self.file_system_name, directory_name, file_name, self.dsc.credential.account_key, permission=FileSasPermissions(read=True, write=True), expiry=datetime.utcnow() + timedelta(hours=1), ) # read the created file which is under root directory file_client = DataLakeFileClient(self.dsc.url, self.file_system_name, directory_name + '/' + file_name, credential=token) properties = await file_client.get_file_properties() # make sure we can read the file properties self.assertIsNotNone(properties) # try to write to the created file with the token response = await file_client.append_data(b"abcd", 0, 4, validate_content=True) self.assertIsNotNone(response) # the token is for file level, so users are not supposed to have access to file system level operations file_system_client = FileSystemClient(self.dsc.url, self.file_system_name, credential=token) with self.assertRaises(ClientAuthenticationError): await file_system_client.get_file_system_properties() # the token is for file level, so users are not supposed to have access to directory level operations directory_client = DataLakeDirectoryClient(self.dsc.url, self.file_system_name, directory_name, credential=token) with self.assertRaises(ClientAuthenticationError): await directory_client.get_directory_properties()
async def __get_file_client(directory_client: DataLakeDirectoryClient, path): file_client: DataLakeFileClient = directory_client.get_file_client(path) try: await file_client.get_file_properties() except ResourceNotFoundError: # We return None to indicate that the file doesnt exist. return None except HttpResponseError as error: message = f'({type(error).__name__}) Problems checking if file exist: {error}' raise HTTPException(status_code=error.status_code, detail=message) from error return file_client
async def _test_using_directory_sas_to_create(self): # SAS URL is calculated from storage key, so this test runs live only if TestMode.need_recording_file(self.test_mode): return # generate a token with directory level create permission directory_name = self._get_directory_reference() token = generate_directory_sas( self.dsc.account_name, self.file_system_name, directory_name, self.dsc.credential.account_key, permission=DirectorySasPermissions(create=True), expiry=datetime.utcnow() + timedelta(hours=1), ) directory_client = DataLakeDirectoryClient(self.dsc.url, self.file_system_name, directory_name, credential=token) response = await directory_client.create_directory() self.assertIsNotNone(response)
async def download_file( filename: str, directory_client: DataLakeDirectoryClient) -> StorageStreamDownloader: """ Downloads file from directory_client. Args: filename (str): Filename to download from directory_client directory_client (DataLakeDirectoryClient): Client to download from. Returns: Downloaded file content """ file_client = directory_client.get_file_client(filename) try: downloaded_file = await file_client.download_file() return downloaded_file except HttpResponseError as error: message = f'({type(error).__name__}) File could not be downloaded: {error}' raise HTTPException(status_code=error.status_code, detail=message) from error
async def _test_rename_dir_with_file_sas(self): # TODO: service bug?? pytest.skip("service bug?") token = generate_directory_sas( self.dsc.account_name, self.file_system_name, "olddir", self.settings.STORAGE_DATA_LAKE_ACCOUNT_KEY, permission=DirectorySasPermissions(read=True, create=True, write=True, delete=True), expiry=datetime.utcnow() + timedelta(hours=1), ) new_token = generate_directory_sas( self.dsc.account_name, self.file_system_name, "newdir", self.settings.STORAGE_DATA_LAKE_ACCOUNT_KEY, permission=DirectorySasPermissions(read=True, create=True, write=True, delete=True), expiry=datetime.utcnow() + timedelta(hours=1), ) # read the created file which is under root directory dir_client = DataLakeDirectoryClient(self.dsc.url, self.file_system_name, "olddir", credential=token) await dir_client.create_directory() new_client = await dir_client.rename_directory( dir_client.file_system_name + '/' + 'newdir' + '?' + new_token) properties = await new_client.get_directory_properties() self.assertEqual(properties.name, "newdir")
async def _test_rename_dir_with_file_system_sas(self): if TestMode.need_recording_file(self.test_mode): return token = generate_file_system_sas( self.dsc.account_name, self.file_system_name, self.dsc.credential.account_key, FileSystemSasPermissions(write=True, read=True, delete=True), datetime.utcnow() + timedelta(hours=1), ) # read the created file which is under root directory dir_client = DataLakeDirectoryClient(self.dsc.url, self.file_system_name, "olddir", credential=token) await dir_client.create_directory() new_client = await dir_client.rename_directory( dir_client.file_system_name + '/' + 'newdir' + '?') properties = await new_client.get_directory_properties() self.assertEqual(properties.name, "newdir")
def instantiate_directory_client_from_conn_str(): # [START instantiate_directory_client_from_conn_str] from azure.storage.filedatalake.aio import DataLakeDirectoryClient DataLakeDirectoryClient.from_connection_string(connection_string, "myfilesystem", "mydirectory")