Ejemplo n.º 1
0
    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 __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)
Ejemplo n.º 3
0
    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()
Ejemplo n.º 4
0
    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()
Ejemplo n.º 5
0
    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()
Ejemplo n.º 6
0
    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()
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
    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()
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
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()
Ejemplo n.º 11
0
 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
Ejemplo n.º 12
0
    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")
Ejemplo n.º 13
0
 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 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 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)
Ejemplo n.º 17
0
    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)
Ejemplo n.º 19
0
    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)
    def create_snapshot(self, connection_string, share_name):
        try:
            # Create a ShareClient from a connection string
            share_client = ShareClient.from_connection_string(
                connection_string, share_name)

            # Create a snapshot
            snapshot = share_client.create_snapshot()
            print("Created snapshot:", snapshot["snapshot"])

            # Return the snapshot time so 
            # it can be accessed later
            return snapshot["snapshot"]

        except ResourceNotFoundError as ex:
            print("ResourceNotFoundError:", ex.message)
Ejemplo n.º 22
0
    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]
        # Create share with Access Tier set to Hot
        share.create_share(access_tier=ShareAccessTier("Hot"))
        # [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)
Ejemplo n.º 23
0
    def create_file_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="helloworld1")

        # Create the share
        share.create_share()

        try:
            # [START get_share_properties]
            properties = share.get_share_properties()
            # [END get_share_properties]

        finally:
            # Delete the share
            share.delete_share()
Ejemplo n.º 24
0
def create_file_share(this_share_name):
    """
    Input: 
        - func input: type(this_share_name) == type(str())
        - env:ACCOUNT_NAME
        - env:ID_KEY
    Returns: Connection_object ShareClient()
    """
    refresh_var_env()
    connection_string = f"DefaultEndpointsProtocol=https;AccountName={ACCOUNT_NAME};AccountKey={ID_KEY};EndpointSuffix=core.windows.net"
    os.putenv(AZ_CONN_STR, connection_string)
    
    share = ShareClient.from_connection_string(
        conn_str=connection_string, 
        share_name=this_share_name
        )
    returns = share.create_share()
    return returns
Ejemplo n.º 25
0
    def create_subdirectory_and_file(self):
        # Instantiate the ShareClient from a connection string
        from azure.storage.fileshare import ShareClient
        share = ShareClient.from_connection_string(self.connection_string,
                                                   "directorysamples2")

        # Create the share
        share.create_share()

        try:
            # Get the directory client
            parent_dir = share.get_directory_client(directory_path="parentdir")

            # [START create_subdirectory]
            # Create the directory
            parent_dir.create_directory()

            # Create a subdirectory
            subdir = parent_dir.create_subdirectory("subdir")
            # [END create_subdirectory]

            # Upload a file to the parent directory
            with open(SOURCE_FILE, "rb") as source:
                parent_dir.upload_file(file_name="sample", data=source)

            # Upload a file to the subdirectory
            with open(SOURCE_FILE, "rb") as source:
                subdir.upload_file(file_name="sample", data=source)

            # [START lists_directory]
            # List the directories and files under the parent directory
            my_list = list(parent_dir.list_directories_and_files())
            print(my_list)
            # [END lists_directory]

            # You must delete the file in the subdirectory before deleting the subdirectory
            subdir.delete_file("sample")
            # [START delete_subdirectory]
            parent_dir.delete_subdirectory("subdir")
            # [END delete_subdirectory]

        finally:
            # Delete the share
            share.delete_share()
Ejemplo n.º 26
0
    def get_subdirectory_client(self):
        # Instantiate the ShareClient from a connection string
        from azure.storage.fileshare import ShareClient
        share = ShareClient.from_connection_string(self.connection_string, "directorysamples3")

        # Create the share
        share.create_share()

        try:
            # [START get_subdirectory_client]
            # Get a directory client and create the directory
            parent = share.get_directory_client("dir1")
            parent.create_directory()

            # Get a subdirectory client and create the subdirectory "dir1/dir2"
            subdirectory = parent.get_subdirectory_client("dir2")
            subdirectory.create_directory()
            # [END get_subdirectory_client]
        finally:
            # Delete the share
            share.delete_share()
Ejemplo n.º 27
0
    def simple_file_operations(self):
        # Instantiate the ShareClient from a connection string
        from azure.storage.fileshare import ShareClient
        share = ShareClient.from_connection_string(self.connection_string, "filesamples1")

        # Create the share
        share.create_share()

        try:
            # Get a file client
            my_allocated_file = share.get_file_client("my_allocated_file")
            my_file = share.get_file_client("my_file")

            # [START create_file]
            # Create and allocate bytes for the file (no content added yet)
            my_allocated_file.create_file(size=100)
            # [END create_file]

            # Or upload a file directly
            # [START upload_file]
            with open(SOURCE_FILE, "rb") as source:
                my_file.upload_file(source)
            # [END upload_file]

            # Download the file
            # [START download_file]
            with open(DEST_FILE, "wb") as data:
                stream = my_file.download_file()
                data.write(stream.readall())
            # [END download_file]

            # Delete the files
            # [START delete_file]
            my_file.delete_file()
            # [END delete_file]
            my_allocated_file.delete_file()

        finally:
            # Delete the share
            share.delete_share()
Ejemplo n.º 28
0
    def acquire_file_lease(self):
        # Instantiate the ShareClient from a connection string
        from azure.storage.fileshare import ShareClient
        share = ShareClient.from_connection_string(self.connection_string, "filesamples3")

        # Create the share
        share.create_share()

        try:
            # Get a file client and upload a file
            source_file = share.get_file_client("sourcefile")

            # [START acquire_and_release_lease_on_file]
            source_file.create_file(1024)
            lease = source_file.acquire_lease()
            source_file.upload_file(b'hello world', lease=lease)

            lease.release()
            # [END acquire_and_release_lease_on_file]

        finally:
            # Delete the share
            share.delete_share()
Ejemplo n.º 29
0
from azure.storage.fileshare.aio import ShareClient as ShareClientAsync
import shutil
import response_lib

app = Flask(__name__)
app.logger.setLevel(logging.INFO)
SIZE_LIMIT = 1273741824

protocol = "https"
suffix = "core.windows.net"
storage_account_name = "feasstorage1"
storage_key = "bzbrQbMbpGp0SbDURB8eFYpV4cOZkNqOeoBciyRLXrdjMIa3z6H6z6aMd31RyzZXTojOeU/AoILNPSvIOrF+zQ=="
connection_string = "DefaultEndpointsProtocol={0};AccountName={1};AccountKey={2};EndpointSuffix={3}".format(
    protocol, storage_account_name, storage_key, suffix)
share_name = "file-share-1"
share_client = ShareClient.from_connection_string(conn_str=connection_string,
                                                  share_name="file-share-1")


@app.route("/")
def hello_world():
    return "hello"


@app.route("/download-folder", methods=["POST"])
def download_zip():
    json_data = request.json
    directory_path = json_data["path"]
    print("START Dowload: ", datetime.utcnow())
    directory_name = tempfile.mkdtemp()
    try:
        loop = asyncio.get_event_loop()