TableServiceClient Class
A client to interact with the Table Service at the account level.
This client provides operations to retrieve and configure the account properties as well as list, create and delete tables within the account. For operations relating to a specific table, a client for this entity can be retrieved using the get_table_client function.
- Inheritance
-
azure.data.tables.aio._base_client_async.AsyncTablesBaseClientTableServiceClient
Constructor
TableServiceClient(endpoint: str, *, credential: AzureSasCredential | AzureNamedKeyCredential | AsyncTokenCredential | None = None, api_version: str | None = None, **kwargs: Any)
Parameters
Name | Description |
---|---|
endpoint
Required
|
The URL to the table service endpoint. Any other entities included in the URL path (e.g. table) will be discarded. This URL can be optionally authenticated with a SAS token. |
endpoint
Required
|
A URL to an Azure Tables account. |
Keyword-Only Parameters
Name | Description |
---|---|
credential
|
The credentials with which to authenticate. This is optional if the account URL already has a SAS token. The value can be one of AzureNamedKeyCredential (azure-core), AzureSasCredential (azure-core), or an AsyncTokenCredential implementation from azure-identity. |
api_version
|
The Storage API version to use for requests. Default value is '2019-02-02'. Setting to an older version may result in reduced feature compatibility. |
credential
|
The credentials with which to authenticate. This is optional if the account URL already has a SAS token. The value can be one of AzureNamedKeyCredential (azure-core), AzureSasCredential (azure-core), or an AsyncTokenCredential implementation from azure-identity. |
api_version
|
Specifies the version of the operation to use for this request. Default value is "2019-02-02". |
Examples
Creating the tableServiceClient with an account url and credential.
from azure.data.tables.aio import TableServiceClient
from azure.core.credentials import AzureNamedKeyCredential
credential = AzureNamedKeyCredential(self.account_name, self.access_key)
async with TableServiceClient(endpoint=self.endpoint, credential=credential) as table_service_client:
properties = await table_service_client.get_service_properties()
print(f"{properties}")
Creating the tableServiceClient with Shared Access Signature.
from datetime import datetime, timedelta
from azure.data.tables.aio import TableServiceClient
from azure.data.tables import generate_account_sas, ResourceTypes, AccountSasPermissions
from azure.core.credentials import AzureNamedKeyCredential, AzureSasCredential
credential = AzureNamedKeyCredential(self.account_name, self.access_key)
# Create a SAS token to use for authentication of a client
sas_token = generate_account_sas(
credential,
resource_types=ResourceTypes(service=True),
permission=AccountSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=1),
)
async with TableServiceClient(
endpoint=self.endpoint, credential=AzureSasCredential(sas_token)
) as table_service_client:
properties = await table_service_client.get_service_properties()
print(f"{properties}")
Variables
Name | Description |
---|---|
account_name
|
The name of the Tables account. |
url
|
The full URL to the Tables account. |
Methods
close |
This method is to close the sockets opened by the client. It need not be used when using with a context manager. |
create_table |
Creates a new table under the given account. |
create_table_if_not_exists |
Creates a new table if it does not currently exist. If the table currently exists, the current table is returned. |
delete_table |
Deletes a table under the current account. No error will be raised if the table is not found. |
from_connection_string |
Create TableServiceClient from a Connection String. |
get_service_properties |
Gets the properties of an account's Table service, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules. |
get_service_stats |
Retrieves statistics related to replication for the Table service. It is only available on the secondary location endpoint when read-access geo-redundant replication is enabled for the account. |
get_table_client |
Get a client to interact with the specified table. The table need not already exist. |
list_tables |
Queries tables under the given account. |
query_tables |
Queries tables under the given account. |
set_service_properties |
Sets properties for an account's Table service endpoint, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules. |
close
This method is to close the sockets opened by the client. It need not be used when using with a context manager.
async close() -> None
create_table
Creates a new table under the given account.
async create_table(table_name: str, **kwargs) -> TableClient
Parameters
Name | Description |
---|---|
table_name
Required
|
The Table name. |
Returns
Type | Description |
---|---|
TableClient, or the result of cls(response) |
Exceptions
Type | Description |
---|---|
Examples
Creating a table from TableServiceClient.
async with TableServiceClient.from_connection_string(self.connection_string) as table_service_client:
try:
table_client = await table_service_client.create_table(table_name=self.table_name)
print(f"Created table {table_client.table_name}!")
except ResourceExistsError:
print("Table already exists")
create_table_if_not_exists
Creates a new table if it does not currently exist. If the table currently exists, the current table is returned.
async create_table_if_not_exists(table_name: str, **kwargs) -> TableClient
Parameters
Name | Description |
---|---|
table_name
Required
|
The Table name. |
Returns
Type | Description |
---|---|
TableClient |
Examples
Creating a table if it does not already exist
async with TableServiceClient.from_connection_string(self.connection_string) as table_service_client:
table_client = await table_service_client.create_table_if_not_exists(table_name=self.table_name)
print(f"Table name: {table_client.table_name}")
delete_table
Deletes a table under the current account. No error will be raised if the table is not found.
async delete_table(table_name: str, **kwargs) -> None
Parameters
Name | Description |
---|---|
table_name
Required
|
The Table name. |
Returns
Type | Description |
---|---|
None |
Exceptions
Type | Description |
---|---|
Examples
Deleting a table
async with TableServiceClient.from_connection_string(self.connection_string) as table_service_client:
await table_service_client.delete_table(table_name=self.table_name)
print(f"Deleted table {self.table_name}!")
from_connection_string
Create TableServiceClient from a Connection String.
from_connection_string(conn_str: str, **kwargs: Any) -> TableServiceClient
Parameters
Name | Description |
---|---|
conn_str
Required
|
A connection string to an Azure Tables account. |
Returns
Type | Description |
---|---|
A Table service client. |
Examples
Creating the tableServiceClient from a connection string
from azure.data.tables.aio import TableServiceClient
async with TableServiceClient.from_connection_string(conn_str=self.connection_string) as table_service_client:
properties = await table_service_client.get_service_properties()
print(f"{properties}")
get_service_properties
Gets the properties of an account's Table service, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules.
async get_service_properties(**kwargs) -> Dict[str, object]
Returns
Type | Description |
---|---|
Dictionary of service properties |
Exceptions
Type | Description |
---|---|
get_service_stats
Retrieves statistics related to replication for the Table service. It is only available on the secondary location endpoint when read-access geo-redundant replication is enabled for the account.
async get_service_stats(**kwargs) -> Dict[str, object]
Returns
Type | Description |
---|---|
Dictionary of service stats |
Exceptions
Type | Description |
---|---|
get_table_client
Get a client to interact with the specified table.
The table need not already exist.
get_table_client(table_name: str, **kwargs: Any) -> TableClient
Parameters
Name | Description |
---|---|
table_name
Required
|
The table name |
Returns
Type | Description |
---|---|
A TableClient object. |
list_tables
Queries tables under the given account.
list_tables(*, results_per_page: int | None = None, **kwargs) -> AsyncItemPaged[TableItem]
Keyword-Only Parameters
Name | Description |
---|---|
results_per_page
|
Number of tables per page in returned ItemPaged |
Returns
Type | Description |
---|---|
An async iterator of TableItem |
Exceptions
Type | Description |
---|---|
Examples
Listing all tables in an account
# List all the tables in the service
print("Listing tables:")
async for table in table_service.list_tables():
print(f"\t{table.name}")
query_tables
Queries tables under the given account.
query_tables(query_filter: str, *, results_per_page: int | None = None, parameters: Dict[str, Any] | None = None, **kwargs) -> AsyncItemPaged[TableItem]
Parameters
Name | Description |
---|---|
query_filter
Required
|
Specify a filter to return certain tables. |
Keyword-Only Parameters
Name | Description |
---|---|
results_per_page
|
Number of tables per page in return ItemPaged |
parameters
|
Dictionary for formatting query with additional, user defined parameters |
Returns
Type | Description |
---|---|
An async iterator of TableItem |
Exceptions
Type | Description |
---|---|
Examples
Querying tables in an account given specific parameters
# Query for "table1" in the tables created
table_name = "mytableasync1"
name_filter = f"TableName eq '{table_name}'"
print("Queried_tables")
async for table in table_service.query_tables(name_filter):
print(f"\t{table.name}")
set_service_properties
Sets properties for an account's Table service endpoint, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules.
async set_service_properties(*, analytics_logging: TableAnalyticsLogging | None = None, hour_metrics: TableMetrics | None = None, minute_metrics: TableMetrics | None = None, cors: List[TableCorsRule] | None = None, **kwargs) -> None
Keyword-Only Parameters
Name | Description |
---|---|
analytics_logging
|
Properties for analytics |
hour_metrics
|
TableMetrics or
None
Hour level metrics |
minute_metrics
|
TableMetrics or
None
Minute level metrics |
cors
|
Cross-origin resource sharing rules |
Returns
Type | Description |
---|---|
None |
Exceptions
Type | Description |
---|---|
Attributes
api_version
The version of the Storage API used for requests.
Returns
Type | Description |
---|---|
The Storage API version. |
url
The full endpoint URL to this entity, including SAS token if used.
This could be either the primary endpoint, or the secondary endpoint depending on the current <xref:azure.data.tables.aio.location_mode>.
Returns
Type | Description |
---|---|
The full endpoint URL including SAS token if used. |
Azure SDK for Python