123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- def basic_file_operations(self, sharename, filename, service):
- # Creating an SMB file share in your Azure Files account.
- print('\nAttempting to create a sample file from text for upload demonstration.')
- # All directories and share must be created in a parent share.
- # Max capacity: 5TB per share
- print('Creating sample share.')
- share_client = service.create_share(share_name=sharename)
- print('Sample share "'+ sharename +'" created.')
- # Creating an optional file directory in your Azure Files account.
- print('Creating a sample directory.')
- # Get the directory client
- directory_client = share_client.create_directory("mydirectory")
- print('Sample directory "mydirectory" created.')
- # Uploading text to sharename/mydirectory/my_text_file in Azure Files account.
- # Max capacity: 1TB per file
- print('Uploading a sample file from text.')
- # create_file_client
- file_client = directory_client.get_file_client(filename)
- # Upload a file
- file_client.upload_file('Hello World! - from text sample')
- print('Sample file "' + filename + '" created and uploaded to: ' + sharename + '/mydirectory')
-
- # Demonstrate how to copy a file
- print('\nCopying file ' + filename)
- # Create another file client which will copy the file from url
- destination_file_client = share_client.get_file_client('file1copy')
- # Copy the sample source file from the url to the destination file
- copy_resp = destination_file_client.start_copy_from_url(source_url=file_client.url)
- if copy_resp['copy_status'] == 'pending':
- # Demonstrate how to abort a copy operation (just for demo, probably will never get here)
- print('Abort copy operation')
- destination_file.abort_copy()
- else:
- print('Copy was a ' + copy_resp['copy_status'])
-
- # Demonstrate how to create a share and upload a file from a local temporary file path
- print('\nAttempting to upload a sample file from path for upload demonstration.')
- # Creating a temporary file to upload to Azure Files
- print('Creating a temporary file from text.')
- with tempfile.NamedTemporaryFile(delete=False) as my_temp_file:
- my_temp_file.file.write(b"Hello world!")
- print('Sample temporary file created.')
- # Uploading my_temp_file to sharename folder in Azure Files
- # Max capacity: 1TB per file
- print('Uploading a sample file from local path.')
- # Create file_client
- file_client = share_client.get_file_client(filename)
- # Upload a file
- with open(my_temp_file.name, "rb") as source_file:
- file_client.upload_file(source_file)
- print('Sample file "' + filename + '" uploaded from path to share: ' + sharename)
- # Close the temp file
- my_temp_file.close()
- # Get the list of valid ranges and write to the specified range
- print('\nGet list of valid ranges of the file.')
- file_ranges = file_client.get_ranges()
- data = b'abcdefghijkl'
- print('Put a range of data to the file.')
-
- file_client.upload_range(data=data, offset=file_ranges[0]['start'], length=len(data))
- # Demonstrate how to download a file from Azure Files
- # The following example download the file that was previously uploaded to Azure Files
- print('\nAttempting to download a sample file from Azure files for demonstration.')
- destination_file = tempfile.tempdir + '\mypathfile.txt'
- with open(destination_file, "wb") as file_handle:
- data = file_client.download_file()
- data.readinto(file_handle)
- print('Sample file downloaded to: ' + destination_file)
- # Demonstrate how to list files and directories contains under Azure File share
- print('\nAttempting to list files and directories directory under share "' + sharename + '":')
- # Create a generator to list directories and files under share
- # This is not a recursive listing operation
- generator = share_client.list_directories_and_files()
- # Prints the directories and files under the share
- for file_or_dir in generator:
- print(file_or_dir['name'])
-
- # remove temp file
- os.remove(my_temp_file.name)
- print('Files and directories under share "' + sharename + '" listed.')
- print('\nCompleted successfully - Azure basic Files operations.')
|