Azure-sdk-for-python: Azure Datalake Gen2 Rename File Issue

Created on 2 Feb 2020  Â·  10Comments  Â·  Source: Azure/azure-sdk-for-python

  • azure.storage.filedatalake:
  • *12.0.0.b *:
  • Windows:
  • 3.73:

Describe the bug
When trying to get azure.storage.filedatalake rename_file method working for an existing file_client I receive the following error:
file_client.rename_file(file_client.file_system_name+'/'+target_path) azure.core.exceptions.ResourceNotFoundError: Operation returned an invalid status 'The parent directory of the destination path does not exist.' ErrorCode:RenameDestinationParentPathNotFound error:{'code': 'RenameDestinationParentPathNotFound', 'message': 'The parent directory of the destination path does not exist.\nRequestId:889d285a-601f-001d-3afc-d9ef4b000000\nTime:2020-02-02T19:11:34.7429024Z'}

Provided is my code snippet:
-Creating a file_client for an existing file
-Creating target path name by taking the file_system name and appending it to the full path of the new file

logger.info("Now loading file: {0} into it's corresponding raw directory: {1}".format(filename, curated_path)) file_client = self.datalake_conn.file_system.get_file_client(filename) target_path = self.datalake_conn.file_system_name+'/'+curated_path+filename.split('/')[1] file_client.rename_file(target_path)

Client Data Lake Service Attention Storage customer-reported question

Most helpful comment

Hi @markpearl ,
Here are the steps on how to use rename_file, your question appears in step3.:
Step 1, create a DataLakeServiceClient Object.
Step 2, get file client.
step 3, create target path.
step 4, rename file.

The detailed code is shown below for how to use rename_file (here is the official sample ):
```
from azure.storage.filedatalake import DataLakeServiceClient
account_name = 'AccountName'
account_key = 'AccountKey'

Step 1, create a DataLakeServiceClient Object.

dsc = DataLakeServiceClient(account_url="{}://{}.dfs.core.windows.net".format(
"https",
account_name
), credential=account_key)

step 2, get file client.

file_client=dsc.get_file_client(file_system='FileSystem', file_path='FolderName/YourFileName')

step 3, create target path.

target_path=file_client.file_system_name + '/' + file_client.path_name.split('/')[0]+"/"+"NewFileName"

step 4, rename file.

new_file_client = file_client.rename_file(target_path)
```
@jongio

All 10 comments

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @sumantmehtams

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @xgithubtriage

Hi @markpearl
Thanks for reaching out!
I think the problem is you didn't create a curated_path, rename will not help you create the path

Feel free to let me know if it's not working!

@xiafu-msft are you able to provide me with the a small sample on how to use rename_file()? There's nothing for it in the samples folder. I saw the unit tests you created for it, but wasn't able to replicate their behavior. Would be extremely useful if you could show the steps!
i.e. 1) create_file_client for target path 2) ..

Hi @markpearl ,
Here are the steps on how to use rename_file, your question appears in step3.:
Step 1, create a DataLakeServiceClient Object.
Step 2, get file client.
step 3, create target path.
step 4, rename file.

The detailed code is shown below for how to use rename_file (here is the official sample ):
```
from azure.storage.filedatalake import DataLakeServiceClient
account_name = 'AccountName'
account_key = 'AccountKey'

Step 1, create a DataLakeServiceClient Object.

dsc = DataLakeServiceClient(account_url="{}://{}.dfs.core.windows.net".format(
"https",
account_name
), credential=account_key)

step 2, get file client.

file_client=dsc.get_file_client(file_system='FileSystem', file_path='FolderName/YourFileName')

step 3, create target path.

target_path=file_client.file_system_name + '/' + file_client.path_name.split('/')[0]+"/"+"NewFileName"

step 4, rename file.

new_file_client = file_client.rename_file(target_path)
```
@jongio

Hi @markpearl ,
How is this issue going? Feel free to let us know if further help is needed.

Hi @markpearl
Sorry about the late response. Based on your code, I think you just didn't create the original file that you want to rename. Would you like to try the code below(which is based on your existing code) and see if it's working.

file_client = self.datalake_conn.file_system.get_file_client(filename) 
target_path = self.datalake_conn.file_system_name+'/'+curated_path+filename.split('/')[1]
file_client.create_file()
file_client.rename_file(target_path)

Hi @xiafu-msft

I'm doing some development with this SDK and Azure Functions. I've noticed that the file_client.rename_file method fails if there are spaces in the source file name.

If there are spaces in the source file name (the one for which you get the file_client and execute the rename_file method) you get the: Operation returned an invalid status 'The source URI is invalid.' error.

If you take the spaces out, it works fine. I notice that having spaces in the destination file name (the one you are renaming to) works fine.

Hope this makes sense - it may be a separate bug and I may have put this in the wrong place...if so, sorry, I'm new to raising bugs here :)

Hi @JADawson
Sorry that I just noticed you comments under this open issue! Please feel free to create a new one next time!
And you are right this is a bug in SDK, while it's fixed in the most recent release! We are appreciated that you reported this!

Thanks again

close this issue for now, feel free to reopen it if you have any question

Was this page helpful?
0 / 5 - 0 ratings