file_advanced_samples_6.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. def metadata_and_properties(self, service):
  2. share_name = 'sharename' + self.random_data.get_random_name(6)
  3. try:
  4. # All directories and share must be created in a parent share.
  5. # Max capacity: 5TB per share
  6. print('1. Create sample share with name ' + share_name)
  7. quota = 1 # in GB
  8. metadata = { "foo": "bar", "baz": "foo" }
  9. share_client = service.create_share(share_name=share_name)
  10. print('Sample share "'+ share_name +'" created.')
  11. print('2. Get share properties.')
  12. properties = share_client.get_share_properties()
  13. print('3. Get share metadata.')
  14. get_metadata = properties['metadata']
  15. for k, v in get_metadata.items():
  16. print("\t" + k + ": " + v)
  17. dir_name = 'dirname' + self.random_data.get_random_name(6)
  18. print('4. Create sample directory with name ' + dir_name)
  19. metadata = { "abc": "def", "jkl": "mno" }
  20. directory_client = share_client.create_directory(dir_name, metadata=metadata)
  21. print('Sample directory "'+ dir_name +'" created.')
  22. print('5. Get directory properties.')
  23. properties = directory_client.get_directory_properties()
  24. print('6. Get directory metadata.')
  25. get_metadata = properties['metadata']
  26. for k, v in get_metadata.items():
  27. print("\t" + k + ": " + v)
  28. file_name = 'sample.txt'
  29. # Uploading text to share_name/dir_name/sample.txt in Azure Files account.
  30. # Max capacity: 1TB per file
  31. print('7. Upload sample file from text to directory.')
  32. metadata = { "prop1": "val1", "prop2": "val2" }
  33. file_client = directory_client.get_file_client(file_name)
  34. file_client.upload_file('Hello World! - from text sample', metadata=metadata)
  35. print('Sample file "' + file_name + '" created and uploaded to: ' + share_name + '/' + dir_name)
  36. print('8. Get file properties.')
  37. properties = file_client.get_file_properties()
  38. print('9. Get file metadata.')
  39. get_metadata = properties['metadata']
  40. for k, v in get_metadata.items():
  41. print("\t" + k + ": " + v)
  42. # This is for demo purposes, all files will be deleted when share is deleted
  43. print('10. Delete file.')
  44. file_client.delete_file()
  45. # This is for demo purposes, all directories will be deleted when share is deleted
  46. print('11. Delete directory.')
  47. directory_client.delete_directory()
  48. finally:
  49. print('12. Delete share.')
  50. share_client.delete_share(share_name)
  51. print("Metadata and properties sample completed")