Esempio n. 1
0
 def __init__(self, arguments):
     super().__init__(arguments)
     connection_string = self.get_from_env("AZURE_TABLES_CONNECTION_STRING")
     if self.args.no_client_share:
         self.service_client = SyncTableServiceClient.from_connection_string(connection_string)
         self.async_service_client = AsyncTableServiceClient.from_connection_string(connection_string)
     else:
         if not _ServiceTest.service_client:
             _ServiceTest.service_client = SyncTableServiceClient.from_connection_string(connection_string)
             _ServiceTest.async_service_client = AsyncTableServiceClient.from_connection_string(connection_string)
         self.service_client = _ServiceTest.service_client
         self.async_service_client =_ServiceTest.async_service_client
    def create_if_not_exists(self):
        from azure.data.tables import TableServiceClient

        # [START create_table_if_not_exists]
        with TableServiceClient.from_connection_string(self.connection_string) as table_service_client:
            table_item = TableServiceClient.create_table_if_not_exists(table_name="myTable")
            print("Table name: {}".format(table_item.table_name))
Esempio n. 3
0
    def tables_in_account(self):
        # Instantiate the TableServiceClient from a connection string
        from azure.data.tables import TableServiceClient
        table_service = TableServiceClient.from_connection_string(conn_str=self.connection_string)

        # [START tsc_create_table]
        table_service.create_table("mytable1")
        # [END tsc_create_table]

        try:
            # [START tsc_list_tables]
            # List all the tables in the service
            list_tables = table_service.query_tables()
            for table in list_tables:
                print(table)

            # List the tables in the service that start with the name "my"
            list_my_tables = table_service.query_tables(select="my")
            for table in list_my_tables:
                print(table)
            # [END tsc_list_tables]

        finally:
            # [START tsc_delete_table]
            table_service.delete_table(table_name="mytable1")
    def tables_in_account(self):
        # Instantiate the TableServiceClient from a connection string
        from azure.data.tables import TableServiceClient

        with TableServiceClient.from_connection_string(
                conn_str=self.connection_string) as table_service:

            # [START tsc_create_table]
            table_service.create_table("mytable1")
            table_service.create_table("mytable2")
            # [END tsc_create_table]

            try:
                # [START tsc_list_tables]
                # List all the tables in the service
                list_tables = table_service.list_tables()
                print("Listing tables:")
                for table in list_tables:
                    print("\t{}".format(table.name))
                # [END tsc_list_tables]

                # [START tsc_query_tables]
                table_name = "mytable1"
                name_filter = "TableName eq '{}'".format(table_name)
                queried_tables = table_service.query_tables(name_filter)

                print("Queried_tables")
                for table in queried_tables:
                    print("\t{}".format(table.name))
                # [END tsc_query_tables]

            finally:
                # [START tsc_delete_table]
                self.delete_tables()
    def delete_table(self):
        from azure.data.tables import TableServiceClient
        from azure.core.exceptions import HttpResponseError

        # [START delete_table_from_tc]
        with TableServiceClient.from_connection_string(self.connection_string) as table_service_client:
            table_service_client.delete_table(table_name="myTable")
            print("Deleted table {}!".format("myTable"))
Esempio n. 6
0
    def get_table_client(self):
        # Instantiate the TableServiceClient from a connection string
        from azure.data.tables import TableServiceClient, TableClient
        table_service = TableServiceClient.from_connection_string(conn_str=self.connection_string)

        # [START get_table_client]
        # Get the table client to interact with a specific table
        table = table_service.get_table_client(table="mytable2")
Esempio n. 7
0
 def authentication_by_connection_string(self):
     # Instantiate a TableServiceClient using a connection string
     # [START auth_from_connection_string]
     from azure.data.tables import TableServiceClient
     table_service = TableServiceClient.from_connection_string(
         conn_str=self.connection_string)
     properties = table_service.get_service_properties()
     print("Connection String: {}".format(properties))
Esempio n. 8
0
    def authentication_by_connection_string(self):
        # Instantiate a TableServiceClient using a connection string
        # [START auth_from_connection_string]
        from azure.data.tables import TableServiceClient
        table_service = TableServiceClient.from_connection_string(conn_str=self.connection_string)
        # [END auth_from_connection_string]

        # Get information for the Table Service
        properties = table_service.get_service_properties()
 def delete_tables(self):
     from azure.data.tables import TableServiceClient
     ts = TableServiceClient.from_connection_string(conn_str=self.connection_string)
     tables = ["mytable1", "mytable2"]
     for table in tables:
         try:
             ts.delete_table(table_name=table)
         except:
             pass
Esempio n. 10
0
 def to_table(self, provider, connection_string, data, path):
     '''
     '''
     if provider == 'azure':
         table = TableServiceClient.from_connection_string(
             conn_str=connection_string)
         table_client = table_service_client.get_table_client(
             table_name=path)
         for row in data:
             table_client.create_entity(entity=row)
    def create_table(self):
        from azure.data.tables import TableServiceClient
        from azure.core.exceptions import ResourceExistsError

        # [START create_table_from_tc]
        with TableServiceClient.from_connection_string(self.connection_string) as table_service_client:
            try:
                table_item = table_service_client.create_table(table_name="myTable")
                print("Created table {}!".format(table_item.table_name))
            except ResourceExistsError:
                print("Table already exists")
Esempio n. 12
0
    def test_create_client_for_cosmos_emulator(self):
        emulator_credential = AzureNamedKeyCredential(
            'localhost', self.tables_primary_cosmos_account_key)
        emulator_connstr = "DefaultEndpointsProtocol=http;AccountName=localhost;AccountKey={};TableEndpoint=http://localhost:8902/;".format(
            self.tables_primary_cosmos_account_key)

        client = TableServiceClient.from_connection_string(emulator_connstr)
        assert client.url == "http://localhost:8902"
        assert client.account_name == 'localhost'
        assert client.credential.named_key.name == 'localhost'
        assert client.credential.named_key.key == self.tables_primary_cosmos_account_key
        assert client._cosmos_endpoint
        assert client.scheme == 'http'

        client = TableServiceClient("http://localhost:8902/",
                                    credential=emulator_credential)
        assert client.url == "http://localhost:8902"
        assert client.account_name == 'localhost'
        assert client.credential.named_key.name == 'localhost'
        assert client.credential.named_key.key == self.tables_primary_cosmos_account_key
        assert client._cosmos_endpoint
        assert client.scheme == 'http'

        table = TableClient.from_connection_string(emulator_connstr,
                                                   'tablename')
        assert table.url == "http://localhost:8902"
        assert table.account_name == 'localhost'
        assert table.table_name == 'tablename'
        assert table.credential.named_key.name == 'localhost'
        assert table.credential.named_key.key == self.tables_primary_cosmos_account_key
        assert table._cosmos_endpoint
        assert table.scheme == 'http'

        table = TableClient("http://localhost:8902/",
                            "tablename",
                            credential=emulator_credential)
        assert table.url == "http://localhost:8902"
        assert table.account_name == 'localhost'
        assert table.table_name == 'tablename'
        assert table.credential.named_key.name == 'localhost'
        assert table.credential.named_key.key == self.tables_primary_cosmos_account_key
        assert table._cosmos_endpoint
        assert table.scheme == 'http'

        table = TableClient.from_table_url(
            "http://localhost:8902/Tables('tablename')",
            credential=emulator_credential)
        assert table.url == "http://localhost:8902"
        assert table.account_name == 'localhost'
        assert table.table_name == 'tablename'
        assert table.credential.named_key.name == 'localhost'
        assert table.credential.named_key.key == self.tables_primary_cosmos_account_key
        assert table._cosmos_endpoint
        assert table.scheme == 'http'
    def delete_table(self):
        from azure.data.tables import TableServiceClient
        from azure.core.exceptions import ResourceNotFoundError

        # [START delete_table_from_tc]
        with TableServiceClient.from_connection_string(self.connection_string) as table_service_client:
            try:
                table_service_client.delete_table(table_name="myTable")
                print("Deleted table {}!".format("myTable"))
            except ResourceNotFoundError:
                print("Table could not be found")
Esempio n. 14
0
    def get_table_service(self) -> TableServiceClient:
        """
        Returns azure table service client from connection string.
        Table service client facilitate interaction with tables. Please read more here - https://docs.microsoft.com/en-us/rest/api/storageservices/operations-on-tables

        """
        try:
            return TableServiceClient.from_connection_string(
                conn_str=self.connection_string)
        except Exception as e:
            raise Exception(f"An exception occurred: {str(e)}")
Esempio n. 15
0
    def authentication_by_shared_access_signature(self):
        # Instantiate a TableServiceClient using a connection string
        from azure.data.tables import TableServiceClient
        table_service = TableServiceClient.from_connection_string(conn_str=self.connection_string)

        # Create a SAS token to use for authentication of a client
        from azure.data.tables import generate_account_sas, ResourceTypes, AccountSasPermissions

        sas_token = generate_account_sas(
            self.account_name,
            self.access_key,
            resource_types=ResourceTypes(service=True),
            permission=AccountSasPermissions(read=True),
            expiry=datetime.utcnow() + timedelta(hours=1)
        )

        token_auth_table_service = TableServiceClient(account_url=self.account_url, credential=sas_token)

        # Get information for the Table Service
        properties = token_auth_table_service.get_service_properties()
Esempio n. 16
0
    def table_service_properties(self):
        # Instantiate the TableServiceClient from a connection string
        from azure.data.tables import TableServiceClient
        table_service = TableServiceClient.from_connection_string(conn_str=self.connection_string)

        # [START set_table_service_properties]
        # Create service properties
        from azure.data.tables import TableAnalyticsLogging, Metrics, CorsRule, RetentionPolicy

        # Create logging settings
        logging = TableAnalyticsLogging(read=True, write=True, delete=True,
                                        retention_policy=RetentionPolicy(enabled=True, days=5))

        # Create metrics for requests statistics
        hour_metrics = Metrics(enabled=True, include_apis=True, retention_policy=RetentionPolicy(enabled=True, days=5))
        minute_metrics = Metrics(enabled=True, include_apis=True,
                                 retention_policy=RetentionPolicy(enabled=True, days=5))

        # Create CORS rules
        cors_rule1 = CorsRule(['www.xyz.com'], ['GET'])
        allowed_origins = ['www.xyz.com', "www.ab.com", "www.bc.com"]
        allowed_methods = ['GET', 'PUT']
        max_age_in_seconds = 500
        exposed_headers = ["x-ms-meta-data*", "x-ms-meta-source*", "x-ms-meta-abc", "x-ms-meta-bcd"]
        allowed_headers = ["x-ms-meta-data*", "x-ms-meta-target*", "x-ms-meta-xyz", "x-ms-meta-foo"]
        cors_rule2 = CorsRule(
            allowed_origins,
            allowed_methods,
            max_age_in_seconds=max_age_in_seconds,
            exposed_headers=exposed_headers,
            allowed_headers=allowed_headers
        )

        cors = [cors_rule1, cors_rule2]

        # Set the service properties
        table_service.set_service_properties(logging, hour_metrics, minute_metrics, cors)
        # [END set_table_service_properties]

        # [START get_table_service_properties]
        properties = table_service.get_service_properties()
def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    if mytimer.past_due:
        logging.info('The timer is past due!')

    #Create Azure Tabel Service connection, form connection string
    table_service_client = TableServiceClient.from_connection_string(
        conn_str=tableConnectionString)
    #Connect to the right table
    table_client = table_service_client.get_table_client(
        table_name="scraperdata")

    #Let's fetch a html page
    page = requests.get("http://nos.nl")

    #Parse the page with BeautifulSoup
    soup = BeautifulSoup(page.content, 'html.parser')

    #Loop through all main articles
    for article in soup.find_all('li', class_="cb-mab"):
        #Find the title of the article
        title = article.find('h2').getText()
        print(title)

        #Create a Table Service entity
        tableEntity = {
            'PartitionKey': 'title',
            'RowKey': 'test' + str(random.randint(0, 10000)),
            'Title': title
        }

        #Save the entity in azure table storage
        entity = table_client.create_entity(entity=tableEntity)
        print(entity)

    logging.info('Python timer trigger function ran at %s', utc_timestamp)
Esempio n. 18
0
    def test_create_client_for_azurite(self):
        azurite_credential = AzureNamedKeyCredential("myaccount", self.tables_primary_storage_account_key)
        http_connstr = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey={};TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;".format(
            self.tables_primary_storage_account_key
        )
        https_connstr = "DefaultEndpointsProtocol=https;AccountName=devstoreaccount1;AccountKey={};TableEndpoint=https://127.0.0.1:10002/devstoreaccount1;".format(
            self.tables_primary_storage_account_key
        )
        account_url = "https://127.0.0.1:10002/myaccount"
        client = TableServiceClient(account_url, credential=azurite_credential)
        assert client.account_name == "myaccount"
        assert client.url == "https://127.0.0.1:10002/myaccount"
        assert client._location_mode == "primary"
        assert client._secondary_endpoint == "https://127.0.0.1:10002/myaccount-secondary"
        assert client.credential.named_key.key == azurite_credential.named_key.key
        assert client.credential.named_key.name == azurite_credential.named_key.name
        assert not client._cosmos_endpoint

        client = TableServiceClient.from_connection_string(http_connstr)
        assert client.account_name == "devstoreaccount1"
        assert client.url == "http://127.0.0.1:10002/devstoreaccount1"
        assert client._location_mode == "primary"
        assert client._secondary_endpoint == "http://127.0.0.1:10002/devstoreaccount1-secondary"
        assert client.credential.named_key.key == self.tables_primary_storage_account_key
        assert client.credential.named_key.name == "devstoreaccount1"
        assert not client._cosmos_endpoint

        client = TableServiceClient.from_connection_string(https_connstr)
        assert client.account_name == "devstoreaccount1"
        assert client.url == "https://127.0.0.1:10002/devstoreaccount1"
        assert client._location_mode == "primary"
        assert client._secondary_endpoint == "https://127.0.0.1:10002/devstoreaccount1-secondary"
        assert client.credential.named_key.key == self.tables_primary_storage_account_key
        assert client.credential.named_key.name == "devstoreaccount1"
        assert not client._cosmos_endpoint

        table = TableClient(account_url, "tablename", credential=azurite_credential)
        assert table.account_name == "myaccount"
        assert table.table_name == "tablename"
        assert table.url == "https://127.0.0.1:10002/myaccount"
        assert table._location_mode == "primary"
        assert table._secondary_endpoint == "https://127.0.0.1:10002/myaccount-secondary"
        assert table.credential.named_key.key == azurite_credential.named_key.key
        assert table.credential.named_key.name == azurite_credential.named_key.name
        assert not table._cosmos_endpoint

        table = TableClient.from_connection_string(http_connstr, "tablename")
        assert table.account_name == "devstoreaccount1"
        assert table.table_name == "tablename"
        assert table.url == "http://127.0.0.1:10002/devstoreaccount1"
        assert table._location_mode == "primary"
        assert table._secondary_endpoint == "http://127.0.0.1:10002/devstoreaccount1-secondary"
        assert table.credential.named_key.key == self.tables_primary_storage_account_key
        assert table.credential.named_key.name == "devstoreaccount1"
        assert not table._cosmos_endpoint

        table = TableClient.from_connection_string(https_connstr, "tablename")
        assert table.account_name == "devstoreaccount1"
        assert table.table_name == "tablename"
        assert table.url == "https://127.0.0.1:10002/devstoreaccount1"
        assert table._location_mode == "primary"
        assert table._secondary_endpoint == "https://127.0.0.1:10002/devstoreaccount1-secondary"
        assert table.credential.named_key.key == self.tables_primary_storage_account_key
        assert table.credential.named_key.name == "devstoreaccount1"
        assert not table._cosmos_endpoint

        table_url = "https://127.0.0.1:10002/myaccount/Tables('tablename')"
        table = TableClient.from_table_url(table_url, azurite_credential)
        assert table.account_name == "myaccount"
        assert table.table_name == "tablename"
        assert table.url == "https://127.0.0.1:10002/myaccount"
        assert table._location_mode == "primary"
        assert table._secondary_endpoint == "https://127.0.0.1:10002/myaccount-secondary"
        assert table.credential.named_key.key == azurite_credential.named_key.key
        assert table.credential.named_key.name == azurite_credential.named_key.name
        assert not table._cosmos_endpoint
Esempio n. 19
0
"""
Simple code snippets to manipulate data in Azure table storage
"""

from azure.data.tables import TableServiceClient

data_dict = {
    "key": "value"
}

entity = {
    "PartitionKey": partition_index,
    "RowKey": object_index,
    "data": data_dict
}

connection_string = get_your_credential()

azure_table_name = "your_table_name"

# connect to Azure storage
table_service_client = TableServiceClient.from_connection_string(
    conn_str=connection_string
)

# connect to an existing table in Azure table storage
table_client = table_service_client.get_table_client(table_name=azure_table_name)

# insert new entity(=data) into Azure table storage
table_client = table_client.create_entity(entity=entity)
Esempio n. 20
0
 def getTableServiceClient():
     from azure.data.tables import TableServiceClient
     return TableServiceClient.from_connection_string(
         Config.get_connection_string())
    def connection_string_auth(self):
        from azure.data.tables import TableServiceClient

        table_service_client = TableServiceClient.from_connection_string(
            conn_str=self.connection_string)