async def test_batch_sas_auth(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange await self._set_up(tables_storage_account_name, tables_primary_storage_account_key) try: token = generate_table_sas( tables_storage_account_name, tables_primary_storage_account_key, self.table_name, permission=TableSasPermissions(add=True, read=True, update=True, delete=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) token = AzureSasCredential(token) # Act service = TableServiceClient( self.account_url(tables_storage_account_name, "table"), credential=token, ) table = service.get_table_client(self.table_name) entity = TableEntity() entity.PartitionKey = 'batch_inserts' entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) batch = table.create_batch() transaction_count = 0 for i in range(10): entity.RowKey = str(i) batch.create_entity(entity) transaction_count += 1 transaction_result = await table.send_batch(batch) assert transaction_result is not None total_entities = 0 async for e in table.list_entities(): total_entities += 1 assert total_entities == transaction_count finally: await self._tear_down()
def test_batch_sas_auth(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange self._set_up(tables_storage_account_name, tables_primary_storage_account_key) try: token = generate_table_sas( tables_primary_storage_account_key, self.table_name, permission=TableSasPermissions(add=True, read=True, update=True, delete=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) token = AzureSasCredential(token) # Act service = TableServiceClient( self.account_url(tables_storage_account_name, "table"), credential=token, ) table = service.get_table_client(self.table_name) entity = TableEntity() entity['PartitionKey'] = 'batch_inserts' entity['test'] = EntityProperty(True, EdmType.BOOLEAN) entity['test2'] = 'value' entity['test3'] = 3 entity['test4'] = EntityProperty(1234567890, EdmType.INT32) batch = [] transaction_count = 0 for i in range(10): entity['RowKey'] = str(i) batch.append(('create', entity.copy())) transaction_count += 1 transaction_result = table.submit_transaction(batch) assert transaction_result total_entities = 0 for e in table.list_entities(): total_entities += 1 assert total_entities == transaction_count finally: self._tear_down()
def set_access_policy(self): # [START create_table_client_from_connection_string] from azure.data.tables import TableClient table = TableClient.from_connection_string(self.connection_string, table_name="mytable1") # [END create_table_client_from_connection_string] # Create the Table table.create_table() try: # [START set_access_policy] # Create an access policy from azure.data.tables import AccessPolicy, TableSasPermissions access_policy = AccessPolicy() access_policy.start = datetime.utcnow() - timedelta(hours=1) access_policy.expiry = datetime.utcnow() + timedelta(hours=1) access_policy.permission = TableSasPermissions(add=True) identifiers = {'my-access-policy-id': access_policy} # Set the access policy table.set_table_access_policy(identifiers) # [END set_access_policy] # Use the access policy to generate a SAS token # [START table_client_sas_token] from azure.data.tables import generate_table_sas sas_token = generate_table_sas(table.account_name, table.table_name, table.credential.account_key, policy_id='my-access-policy-id') # [END table_client_sas_token] # Authenticate with the sas token # [START create_table_client] # token_auth_table = table.from_table_url( # table_url=table.url, # credential=sas_token # ) # [END create_table_client] finally: # Delete the table table.delete_table()