File: sample_query_tables_async.py

package info (click to toggle)
python-azure 20201208%2Bgit-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,437,920 kB
  • sloc: python: 4,287,452; javascript: 269; makefile: 198; sh: 187; xml: 106
file content (78 lines) | stat: -rw-r--r-- 2,689 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

"""
FILE: sample_query_tables_async.py

DESCRIPTION:
    These samples demonstrate the following: listing and querying all Tables within
    a storage account.

USAGE:
    python sample_query_tables_async.py

    Set the environment variables with your own values before running the sample:
    1) AZURE_STORAGE_CONNECTION_STRING - the connection string to your storage account
"""

import os
import asyncio

class QueryTables(object):
    connection_string = os.getenv("AZURE_TABLES_CONNECTION_STRING")
    table_name = "OfficeSupplies"

    async def tables_in_account(self):
        # Instantiate the TableServiceClient from a connection string
        from azure.data.tables.aio import TableServiceClient
        table_service = TableServiceClient.from_connection_string(conn_str=self.connection_string)

        async with table_service:
            await table_service.create_table("mytable1")
            await table_service.create_table("mytable2")

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

                # [START tsc_query_tables]
                # Query for "table1" in the tables created
                table_name = "mytable1"
                name_filter = "TableName eq '{}'".format(table_name)
                print("Queried_tables")
                async for table in table_service.query_tables(filter=name_filter):
                    print("\t{}".format(table.table_name))
                # [END tsc_query_tables]

            finally:
                await self.delete_tables()

    async def delete_tables(self):
        from azure.data.tables.aio import TableServiceClient
        ts = TableServiceClient.from_connection_string(conn_str=self.connection_string)
        async with ts:
            tables = ["mytable1", "mytable2"]
            for table in tables:
                try:
                    await ts.delete_table(table_name=table)
                except:
                    pass


async def main():
    sample = QueryTables()
    await sample.delete_tables()
    await sample.tables_in_account()


if __name__ == '__main__':
    asyncio.run(main())