file_basic_samples_3.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. def basic_file_operations(self, sharename, filename, service):
  2. # Creating an SMB file share in your Azure Files account.
  3. print('\nAttempting to create a sample file from text for upload demonstration.')
  4. # All directories and share must be created in a parent share.
  5. # Max capacity: 5TB per share
  6. print('Creating sample share.')
  7. share_client = service.create_share(share_name=sharename)
  8. print('Sample share "'+ sharename +'" created.')
  9. # Creating an optional file directory in your Azure Files account.
  10. print('Creating a sample directory.')
  11. # Get the directory client
  12. directory_client = share_client.create_directory("mydirectory")
  13. print('Sample directory "mydirectory" created.')
  14. # Uploading text to sharename/mydirectory/my_text_file in Azure Files account.
  15. # Max capacity: 1TB per file
  16. print('Uploading a sample file from text.')
  17. # create_file_client
  18. file_client = directory_client.get_file_client(filename)
  19. # Upload a file
  20. file_client.upload_file('Hello World! - from text sample')
  21. print('Sample file "' + filename + '" created and uploaded to: ' + sharename + '/mydirectory')
  22. # Demonstrate how to copy a file
  23. print('\nCopying file ' + filename)
  24. # Create another file client which will copy the file from url
  25. destination_file_client = share_client.get_file_client('file1copy')
  26. # Copy the sample source file from the url to the destination file
  27. copy_resp = destination_file_client.start_copy_from_url(source_url=file_client.url)
  28. if copy_resp['copy_status'] == 'pending':
  29. # Demonstrate how to abort a copy operation (just for demo, probably will never get here)
  30. print('Abort copy operation')
  31. destination_file.abort_copy()
  32. else:
  33. print('Copy was a ' + copy_resp['copy_status'])
  34. # Demonstrate how to create a share and upload a file from a local temporary file path
  35. print('\nAttempting to upload a sample file from path for upload demonstration.')
  36. # Creating a temporary file to upload to Azure Files
  37. print('Creating a temporary file from text.')
  38. with tempfile.NamedTemporaryFile(delete=False) as my_temp_file:
  39. my_temp_file.file.write(b"Hello world!")
  40. print('Sample temporary file created.')
  41. # Uploading my_temp_file to sharename folder in Azure Files
  42. # Max capacity: 1TB per file
  43. print('Uploading a sample file from local path.')
  44. # Create file_client
  45. file_client = share_client.get_file_client(filename)
  46. # Upload a file
  47. with open(my_temp_file.name, "rb") as source_file:
  48. file_client.upload_file(source_file)
  49. print('Sample file "' + filename + '" uploaded from path to share: ' + sharename)
  50. # Close the temp file
  51. my_temp_file.close()
  52. # Get the list of valid ranges and write to the specified range
  53. print('\nGet list of valid ranges of the file.')
  54. file_ranges = file_client.get_ranges()
  55. data = b'abcdefghijkl'
  56. print('Put a range of data to the file.')
  57. file_client.upload_range(data=data, offset=file_ranges[0]['start'], length=len(data))
  58. # Demonstrate how to download a file from Azure Files
  59. # The following example download the file that was previously uploaded to Azure Files
  60. print('\nAttempting to download a sample file from Azure files for demonstration.')
  61. destination_file = tempfile.tempdir + '\mypathfile.txt'
  62. with open(destination_file, "wb") as file_handle:
  63. data = file_client.download_file()
  64. data.readinto(file_handle)
  65. print('Sample file downloaded to: ' + destination_file)
  66. # Demonstrate how to list files and directories contains under Azure File share
  67. print('\nAttempting to list files and directories directory under share "' + sharename + '":')
  68. # Create a generator to list directories and files under share
  69. # This is not a recursive listing operation
  70. generator = share_client.list_directories_and_files()
  71. # Prints the directories and files under the share
  72. for file_or_dir in generator:
  73. print(file_or_dir['name'])
  74. # remove temp file
  75. os.remove(my_temp_file.name)
  76. print('Files and directories under share "' + sharename + '" listed.')
  77. print('\nCompleted successfully - Azure basic Files operations.')