def set_share_properties(self): from azure.storage.fileshare import ShareClient share1 = ShareClient.from_connection_string(self.connection_string, "sharesamples3a") share2 = ShareClient.from_connection_string(self.connection_string, "sharesamples3b") # Create the share share1.create_share() share2.create_share() try: # [START set_share_properties] # Set the tier for the first share to Hot share1.set_share_properties(access_tier="Hot") # Set the quota for the first share to 3 share1.set_share_properties(quota=3) # Set the tier for the second share to Cool and quota to 2 share2.set_share_properties(access_tier=ShareAccessTier("Cool"), quota=2) # Get the shares' properties print(share1.get_share_properties().access_tier) print(share1.get_share_properties().quota) print(share2.get_share_properties().access_tier) print(share2.get_share_properties().quota) # [END set_share_properties] finally: # Delete the shares share1.delete_share() share2.delete_share()
def test_create_snapshot_with_metadata(self): # Arrange share = self._get_share_reference() metadata = {"test1": "foo", "test2": "bar"} metadata2 = {"test100": "foo100", "test200": "bar200"} # Act created = share.create_share(metadata=metadata) snapshot = share.create_snapshot(metadata=metadata2) share_props = share.get_share_properties() snapshot_client = ShareClient( self.get_file_url(), share_name=share.share_name, snapshot=snapshot, credential=self.settings.STORAGE_ACCOUNT_KEY) snapshot_props = snapshot_client.get_share_properties() # Assert self.assertTrue(created) self.assertIsNotNone(snapshot['snapshot']) self.assertIsNotNone(snapshot['etag']) self.assertIsNotNone(snapshot['last_modified']) self.assertEqual(share_props.metadata, metadata) self.assertEqual(snapshot_props.metadata, metadata2) self._delete_shares(share.share_name)
def test_create_snapshot_with_metadata(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference() metadata = {"test1": "foo", "test2": "bar"} metadata2 = {"test100": "foo100", "test200": "bar200"} # Act created = share.create_share(metadata=metadata) snapshot = share.create_snapshot(metadata=metadata2) share_props = share.get_share_properties() snapshot_client = ShareClient(self.account_url(storage_account_name, "file"), share_name=share.share_name, snapshot=snapshot, credential=storage_account_key) snapshot_props = snapshot_client.get_share_properties() # Assert self.assertTrue(created) self.assertIsNotNone(snapshot['snapshot']) self.assertIsNotNone(snapshot['etag']) self.assertIsNotNone(snapshot['last_modified']) self.assertEqual(share_props.metadata, metadata) self.assertEqual(snapshot_props.metadata, metadata2) self._delete_shares(share.share_name)
def test_acquire_lease_on_sharesnapshot(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference("testshare1") # Act share.create_share() snapshot = share.create_snapshot() snapshot_client = ShareClient(self.account_url(storage_account_name, "file"), share_name=share.share_name, snapshot=snapshot, credential=storage_account_key) share_lease = share.acquire_lease() share_snapshot_lease = snapshot_client.acquire_lease() # Assert with self.assertRaises(HttpResponseError): share.get_share_properties(lease=share_snapshot_lease) with self.assertRaises(HttpResponseError): snapshot_client.get_share_properties(lease=share_lease) self.assertIsNotNone(snapshot['snapshot']) self.assertIsNotNone(snapshot['etag']) self.assertIsNotNone(snapshot['last_modified']) self.assertIsNotNone(share_lease) self.assertIsNotNone(share_snapshot_lease) self.assertNotEqual(share_lease, share_snapshot_lease) share_snapshot_lease.release() share_lease.release() self._delete_shares(share.share_name)
def test_create_share_client(self): share_client = ShareClient.from_share_url("http://127.0.0.1:11002/account/customized/path/share?snapshot=baz&", credential={"account_name": "myaccount", "account_key": "key"}) self.assertEqual(share_client.share_name, "share") self.assertEqual(share_client.snapshot, "baz") share_client = ShareClient.from_share_url("http://127.0.0.1:11002/account/share?snapshot=baz&", credential="credential") self.assertEqual(share_client.share_name, "share") self.assertEqual(share_client.snapshot, "baz")
def test_invalid_api_version(self): with pytest.raises(ValueError) as error: ShareServiceClient("https://foo.file.core.windows.net/account", credential="fake_key", api_version="foo") self.assertTrue( str(error.value).startswith("Unsupported API version 'foo'.")) with pytest.raises(ValueError) as error: ShareClient("https://foo.file.core.windows.net/account", "share_name", credential="fake_key", api_version="foo") self.assertTrue( str(error.value).startswith("Unsupported API version 'foo'.")) with pytest.raises(ValueError) as error: ShareDirectoryClient("https://foo.file.core.windows.net/account", "share_name", "dir_path", credential="fake_key", api_version="foo") self.assertTrue( str(error.value).startswith("Unsupported API version 'foo'.")) with pytest.raises(ValueError) as error: ShareFileClient("https://foo.file.core.windows.net/account", "share", self._get_file_reference(), credential="fake_key", api_version="foo") self.assertTrue( str(error.value).startswith("Unsupported API version 'foo'."))
def create_directory_and_file(self): # Instantiate the ShareClient from a connection string from azure.storage.fileshare import ShareClient share = ShareClient.from_connection_string(self.connection_string, "directorysamples1") # Create the share share.create_share() try: # Get the directory client my_directory = share.get_directory_client(directory_path="mydirectory") # [START create_directory] my_directory.create_directory() # [END create_directory] # [START upload_file_to_directory] # Upload a file to the directory with open(SOURCE_FILE, "rb") as source: my_directory.upload_file(file_name="sample", data=source) # [END upload_file_to_directory] # [START delete_file_in_directory] # Delete the file in the directory my_directory.delete_file(file_name="sample") # [END delete_file_in_directory] # [START delete_directory] my_directory.delete_directory() # [END delete_directory] finally: # Delete the share share.delete_share()
def __init__(self, connection_string, share_name, file_path): self.share_cli = ShareClient.from_connection_string( conn_str=connection_string, share_name=share_name) self.file_cli = ShareFileClient.from_connection_string( conn_str=connection_string, share_name=share_name, file_path=file_path)
def upload_a_file_to_share(self): # Instantiate the ShareClient from a connection string from azure.storage.fileshare import ShareClient share = ShareClient.from_connection_string(self.connection_string, share_name="helloworld2") # Create the share share.create_share() try: # Instantiate the ShareFileClient from a connection string # [START create_file_client] from azure.storage.fileshare import ShareFileClient file = ShareFileClient.from_connection_string( self.connection_string, share_name="helloworld2", file_path="myfile") # [END create_file_client] # Upload a file with open(SOURCE_FILE, "rb") as source_file: file.upload_file(source_file) finally: # Delete the share share.delete_share()
def list_directories_and_files(self): # Instantiate the ShareClient from a connection string from azure.storage.fileshare import ShareClient share = ShareClient.from_connection_string(self.connection_string, "sharesamples4") # Create the share share.create_share() try: # [START share_list_files_in_dir] # Create a directory in the share dir_client = share.create_directory("mydir") # Upload a file to the directory with open(SOURCE_FILE, "rb") as source_file: dir_client.upload_file(file_name="sample", data=source_file) # List files in the directory my_files = list( share.list_directories_and_files(directory_name="mydir")) print(my_files) # [END share_list_files_in_dir] finally: # Delete the share share.delete_share()
def set_share_quota_and_metadata(self): # [START create_share_client_from_conn_string] from azure.storage.fileshare import ShareClient share = ShareClient.from_connection_string(self.connection_string, "sharesamples2") # [END create_share_client_from_conn_string] # Create the share share.create_share() try: # [START set_share_quota] # Set the quota for the share to 1GB share.set_share_quota(quota=1) # [END set_share_quota] # [START set_share_metadata] data = {'category': 'test'} share.set_share_metadata(metadata=data) # [END set_share_metadata] # Get the metadata for the share props = share.get_share_properties().metadata finally: # Delete the share share.delete_share()
def get_az_share_client(storage_account_name: str, storage_key: str, share_name: str, is_async: bool = False, **kwargs) -> Union[ShareClient, ShareClientAsync]: """Get Azure Share Client. :param storage_account_name: Storage account name. :param storage_key: Storage key. :param share_name: Share name :param is_async: If True create a Azure share client with async mode. :return: Azure share client. """ # Create connection String. conn_str = "DefaultEndpointsProtocol={0};AccountName={1};AccountKey={2};EndpointSuffix={3}".format( config_storage.protocol, storage_account_name, storage_key, config_storage.suffix, ) # Create Share client. if is_async: share_client = ShareClientAsync.from_connection_string( conn_str=conn_str, share_name=share_name) else: share_client = ShareClient.from_connection_string( conn_str=conn_str, share_name=share_name) return share_client
def copy_file_from_url(self): # Instantiate the ShareClient from a connection string from azure.storage.fileshare import ShareClient share = ShareClient.from_connection_string(self.connection_string, "filesamples2") # Create the share share.create_share() try: # Get a file client and upload a file source_file = share.get_file_client("sourcefile") with open(SOURCE_FILE, "rb") as source: source_file.upload_file(source) # Create another file client which will copy the file from url destination_file = share.get_file_client("destinationfile") # Build the url from which to copy the file source_url = "{}://{}.file.core.windows.net/{}/{}".format( self.protocol, self.storage_account_name, "filesamples2", "sourcefile" ) # Copy the sample source file from the url to the destination file # [START copy_file_from_url] destination_file.start_copy_from_url(source_url=source_url) # [END copy_file_from_url] finally: # Delete the share share.delete_share()
def __init__(self, blob_connection_string=None, file_connection_string=None, production_store_id=None, production_store_name=None): import os import json config = configparser.RawConfigParser() config.read( os.path.join(os.path.dirname(__file__), 'productionstore.cfg')) from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__ from azure.core.exceptions import ResourceNotFoundError, ResourceExistsError from azure.storage.fileshare import ShareClient, ResourceTypes, AccountSasPermissions if blob_connection_string: self.blob_connection_string = blob_connection_string else: self.blob_connection_string = str( config.get('storage account credentials', 'blob_storage_connection_string')) if file_connection_string: self.file_connection_string = file_connection_string else: self.file_connection_string = config.get( 'storage account credentials', 'file_storage_connection_string') self.production_store_id = production_store_id self.production_store_name = production_store_name # global, name for all metadata files at the root of each production prefix self.metadata_file_name = config.get('global settings', 'metadata_filename') # global, prefix for ingest location in each production self.ingest_prefix = config.get('global settings', 'ingest_path') # global, default for directory structures self.default_production_tree = json.loads( config.get('production defaults', 'default_tree')) # blob storage connection logging.info("init connection to blob storage") self.blob_service_client = BlobServiceClient.from_connection_string( self.blob_connection_string) if production_store_id: self.get_production_store() else: self.create_production_store() # file service connetion logging.info("init connection to file service") self.share_client = ShareClient.from_connection_string( self.file_connection_string, share_name=self.production_store_name)
def share_client(): share_name = "apmagentpythonci" + str(uuid.uuid4().hex) share_client = ShareClient.from_connection_string( conn_str=CONNECTION_STRING, share_name=share_name) share_client.create_share() yield share_client share_client.delete_share()
def __init__(self, connection_string, share_name='funcstatemarkershare', file_path='funcstatemarkerfile'): self.share_cli = ShareClient.from_connection_string( conn_str=connection_string, share_name=share_name) self.file_cli = ShareFileClient.from_connection_string( conn_str=connection_string, share_name=share_name, file_path=file_path)
def __init__(self): self.conn_str = settings.FILES_CONN_STRING self.share = f"{settings.ENVIRONMENT}-{settings.FILES_SHARE}" self.client = ShareServiceClient.from_connection_string(self.conn_str) try: self.share_client = ShareClient.from_connection_string( self.conn_str, self.share) self.share_client.create_share() except ResourceExistsError: pass
def test_delete_snapshot(self): # Arrange share = self._get_share_reference() share.create_share() snapshot = share.create_snapshot() # Act with self.assertRaises(HttpResponseError): share.delete_share() snapshot_client = ShareClient( self.get_file_url(), share_name=share.share_name, snapshot=snapshot, credential=self.settings.STORAGE_ACCOUNT_KEY) deleted = snapshot_client.delete_share() self.assertIsNone(deleted) self._delete_shares()
def get_directory_or_file_client(self): # Instantiate the ShareClient from a connection string from azure.storage.fileshare import ShareClient share = ShareClient.from_connection_string(self.connection_string, "sharesamples4") # Get the directory client to interact with a specific directory my_dir = share.get_directory_client("dir1") # Get the file client to interact with a specific file my_file = share.get_file_client("dir1/myfile")
def test_delete_snapshot(self, storage_account_name, storage_account_key): self._setup(storage_account_name, storage_account_key) share = self._get_share_reference() share.create_share() snapshot = share.create_snapshot() # Act with self.assertRaises(HttpResponseError): share.delete_share() snapshot_client = ShareClient(self.account_url(storage_account_name, "file"), share_name=share.share_name, snapshot=snapshot, credential=storage_account_key) deleted = snapshot_client.delete_share() self.assertIsNone(deleted) self._delete_shares()
def create_file_share(self, connection_string, share_name): try: # Create a ShareClient from a connection string share_client = ShareClient.from_connection_string( connection_string, share_name) print("Creating share:", share_name) share_client.create_share() except ResourceExistsError as ex: print("ResourceExistsError:", ex.message)
def delete_snapshot(self, connection_string, share_name, snapshot_time): try: # Create a ShareClient for a snapshot snapshot_client = ShareClient.from_connection_string(conn_str=connection_string, share_name=share_name, snapshot=snapshot_time) print("Deleting snapshot:", snapshot_time) # Delete the snapshot snapshot_client.delete_share() except ResourceNotFoundError as ex: print("ResourceNotFoundError:", ex.message)
def test_share_client_api_version_property(self): share_client = ShareClient("https://foo.file.core.windows.net/account", "share_name", credential="fake_key") self.assertEqual(share_client.api_version, self.api_version_2) self.assertEqual(share_client._client._config.version, self.api_version_2) share_client = ShareClient("https://foo.file.core.windows.net/account", "share_name", credential="fake_key", api_version=self.api_version_1) self.assertEqual(share_client.api_version, self.api_version_1) self.assertEqual(share_client._client._config.version, self.api_version_1) dir_client = share_client.get_directory_client("foo") self.assertEqual(dir_client.api_version, self.api_version_1) self.assertEqual(dir_client._client._config.version, self.api_version_1) file_client = share_client.get_file_client("foo") self.assertEqual(file_client.api_version, self.api_version_1) self.assertEqual(file_client._client._config.version, self.api_version_1)
def delete_share(self, connection_string, share_name): try: # Create a ShareClient from a connection string share_client = ShareClient.from_connection_string( connection_string, share_name) print("Deleting share:", share_name) # Delete the share and snapshots share_client.delete_share(delete_snapshots=True) except ResourceNotFoundError as ex: print("ResourceNotFoundError:", ex.message)
def acquire_share_lease(self): # Instantiate the ShareClient from a connection string from azure.storage.fileshare import ShareClient share = ShareClient.from_connection_string(self.connection_string, "sharesamples") # Create the share share.create_share() # [START acquire_and_release_lease_on_share] share.create_directory("mydir") lease = share.acquire_lease() share.get_share_properties(lease=lease) share.delete_share(lease=lease)
def list_files_and_dirs(self, connection_string, share_name, dir_name): try: # Create a ShareClient from a connection string share_client = ShareClient.from_connection_string( connection_string, share_name) for item in list(share_client.list_directories_and_files(dir_name)): if item["is_directory"]: print("Directory:", item["name"]) else: print("File:", dir_name + "/" + item["name"]) except ResourceNotFoundError as ex: print("ResourceNotFoundError:", ex.message)
def list_files_in_directory_recursive(share_client: ShareClient, dir_path: str) -> Iterable[dict]: """Get all files in the directory. :param share_client:Azure share client. :param dir_path: Directory path. """ for item in list(share_client.list_directories_and_files(dir_path)): file_path = dir_path + "/" + item["name"] if item["is_directory"]: yield from list_files_in_directory_recursive( share_client, file_path) else: item["path"] = file_path yield item
def validate_request_data(share_client: ShareClient, dir_path: str) -> bool: """Validate request data. :param share_client: Azure share client. :param dir_path: Directory path :return: True if request data is valid. """ is_valid = True # Get file client file_client = share_client.get_file_client(file_path=dir_path) # Check if is a directory path. if exist_az_file(file_client): is_valid = False return is_valid
def create_share_snapshot(self): # Instantiate the ShareClient from a connection string from azure.storage.fileshare import ShareClient share = ShareClient.from_connection_string(self.connection_string, "sharesamples1") # [START create_share] share.create_share() # [END create_share] try: # [START create_share_snapshot] share.create_snapshot() # [END create_share_snapshot] finally: # [START delete_share] share.delete_share(delete_snapshots=True)
def browse_snapshot_dir(self, connection_string, share_name, snapshot_time, dir_name): try: # Create a ShareClient from a connection string snapshot = ShareClient.from_connection_string( conn_str=connection_string, share_name=share_name, snapshot=snapshot_time) print("Snapshot:", snapshot_time) for item in list(snapshot.list_directories_and_files(dir_name)): if item["is_directory"]: print("Directory:", item["name"]) else: print("File:", dir_name + "/" + item["name"]) except ResourceNotFoundError as ex: print("ResourceNotFoundError:", ex.message)