9-file_samples_hello_world.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # coding: utf-8
  2. # -------------------------------------------------------------------------
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. # Licensed under the MIT License. See License.txt in the project root for
  5. # license information.
  6. # --------------------------------------------------------------------------
  7. """
  8. FILE: file_samples_hello_world.py
  9. DESCRIPTION:
  10. These samples demonstrate common scenarios like instantiating a client,
  11. creating a file share, and uploading a file to a share.
  12. USAGE:
  13. python file_samples_hello_world.py
  14. Set the environment variables with your own values before running the sample:
  15. 1) AZURE_STORAGE_CONNECTION_STRING - the connection string to your storage account
  16. """
  17. import os
  18. SOURCE_FILE = './SampleSource.txt'
  19. DEST_FILE = './SampleDestination.txt'
  20. class HelloWorldSamples(object):
  21. connection_string = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
  22. def create_client_with_connection_string(self):
  23. # Instantiate the ShareServiceClient from a connection string
  24. from azure.storage.fileshare import ShareServiceClient
  25. file_service = ShareServiceClient.from_connection_string(self.connection_string)
  26. def create_file_share(self):
  27. # Instantiate the ShareClient from a connection string
  28. import azure
  29. from azure.storage.fileshare import ShareClient
  30. share = ShareClient.from_connection_string(self.connection_string, share_name="helloworld1")
  31. # Create the share
  32. share.create_share()
  33. try:
  34. # [START get_share_properties]
  35. properties = share.get_share_properties()
  36. # [END get_share_properties]
  37. finally:
  38. # Delete the share
  39. share.delete_share()
  40. def upload_a_file_to_share(self):
  41. # Instantiate the ShareClient from a connection string
  42. from azure.storage.fileshare import ShareClient
  43. share = ShareClient.from_connection_string(self.connection_string, share_name="helloworld2")
  44. # Create the share
  45. share.create_share()
  46. try:
  47. # Instantiate the ShareFileClient from a connection string
  48. # [START create_file_client]
  49. from azure.storage.fileshare import ShareFileClient
  50. file = ShareFileClient.from_connection_string(
  51. self.connection_string,
  52. share_name="helloworld2",
  53. file_path="myfile")
  54. # [END create_file_client]
  55. # Upload a file
  56. with open(SOURCE_FILE, "rb") as source_file:
  57. file.upload_file(source_file)
  58. finally:
  59. # Delete the share
  60. share.delete_share()
  61. if __name__ == '__main__':
  62. sample = HelloWorldSamples()
  63. sample.create_client_with_connection_string()
  64. sample.create_file_share()
  65. sample.upload_a_file_to_share()