locked
[Python] Using the Azure Storage Python SDK and the File Service, how can I make a copy of a file within the Fileshare? RRS feed

  • Question

  • I use Azure Storage, the File Service, and the Python SDK.

    So I have "File A", and I simply wish to copy it to another location within the Fileshare.  

    How can I do this?  

    I already know most of the basics, functions, etc. - but I just can't seem to find a (documented) way to copy a file.

    Thanks.

    Sri





    Saturday, September 10, 2016 11:11 PM

Answers

  • Hi,

    Thank you for posting here.

    For copying file between locations within the file share, you can use the function copy_file of the class FileService of the module azure.storage.file.fileservice which wrapped the REST API Copy File.

    As reference, here is my sample code.

    from azure.storage import CloudStorageAccount
    from azure.storage.file import FileService
    
    account_name = '<your-storage-account>'
    account_key = '<your-storage-account-key>'
    
    source_share_name = '<source-share>'
    source_dir_name = '<source-dir>'
    source_file_name = '<source-file>'
    
    target_share_name = '<target-share>'
    target_dir_name = '<target-dir>'
    target_file_name = '<target-file>'
    
    account = CloudStorageAccount(account_name, account_key)
    
    file_service = account.create_file_service()
    
    # If target share & directory not exist
    file_service.create_share(target_share_name) 
    file_service.create_directory(target_share_name, target_dir_name)
    
    # Get the url of the source file
    source_url = file_service.make_file_url(source_share_name, source_dir_name, source_file_name)
    
    # Copy File from source to target
    file_service.copy_file(target_share_name, target_dir_name, target_file_name, source_url)

    Hope it helps.

    Best Regards,

    Peter Pan

    Monday, September 12, 2016 4:27 AM

All replies

  • Hi Srikanth,

    Thank you for posting here! We are happy to assist you.

    Refer to the below mentioned link:

    https://azure.microsoft.com/en-in/documentation/articles/storage-python-how-to-use-file-storage/

    For more information on Microsoft Azure Storage SDK for Python. Click here

    Regards,
    Sumanth BM


    Sunday, September 11, 2016 3:23 PM
  • Hi,

    Thank you for posting here.

    For copying file between locations within the file share, you can use the function copy_file of the class FileService of the module azure.storage.file.fileservice which wrapped the REST API Copy File.

    As reference, here is my sample code.

    from azure.storage import CloudStorageAccount
    from azure.storage.file import FileService
    
    account_name = '<your-storage-account>'
    account_key = '<your-storage-account-key>'
    
    source_share_name = '<source-share>'
    source_dir_name = '<source-dir>'
    source_file_name = '<source-file>'
    
    target_share_name = '<target-share>'
    target_dir_name = '<target-dir>'
    target_file_name = '<target-file>'
    
    account = CloudStorageAccount(account_name, account_key)
    
    file_service = account.create_file_service()
    
    # If target share & directory not exist
    file_service.create_share(target_share_name) 
    file_service.create_directory(target_share_name, target_dir_name)
    
    # Get the url of the source file
    source_url = file_service.make_file_url(source_share_name, source_dir_name, source_file_name)
    
    # Copy File from source to target
    file_service.copy_file(target_share_name, target_dir_name, target_file_name, source_url)

    Hope it helps.

    Best Regards,

    Peter Pan

    Monday, September 12, 2016 4:27 AM
  • Thank you.
    Monday, September 12, 2016 11:25 PM
  • Thank you.
    Monday, September 12, 2016 11:26 PM
  • Actually, I had one more question regarding this answer:

    Where you have

    "

    file_service.copy_file(target_share_name, target_dir_name, target_file_name, source_url)

    How can I determine the source_url of a given file using Fileshares?

    Wednesday, September 14, 2016 7:14 PM
  • Actually, I got the answer:

    https://<storage account>.file.core.windows.net/<share>/<directory/directories>/<file>

    Thanks.

    Wednesday, September 14, 2016 7:16 PM