1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- def table_operations_with_sas(self, account):
- table_name = 'sastable' + self.random_data.get_random_name(6)
-
- try:
- # Create a Table Service object
- table_service = account.create_table_service()
-
- print('1. Create table with name - ' + table_name)
- table_service.create_table(table_name)
-
- # Create a Shared Access Signature for the table
- print('2. Get sas for table')
-
- table_sas = table_service.generate_table_shared_access_signature(
- table_name,
- TablePermissions.QUERY + TablePermissions.ADD + TablePermissions.UPDATE + TablePermissions.DELETE,
- datetime.datetime.utcnow() + datetime.timedelta(hours=1))
- shared_account = TableStorageAccount(account_name=account.account_name, sas_token=table_sas, endpoint_suffix=account.endpoint_suffix)
- shared_table_service = shared_account.create_table_service()
- # Create a sample entity to insert into the table
- customer = {'PartitionKey': 'Harp', 'RowKey': '1', 'email' : 'harp@contoso.com', 'phone' : '555-555-5555'}
- # Insert the entity into the table
- print('3. Insert new entity into table with sas - ' + table_name)
- shared_table_service.insert_entity(table_name, customer)
-
- # Demonstrate how to query the entity
- print('4. Read the inserted entity with sas.')
- entity = shared_table_service.get_entity(table_name, 'Harp', '1')
-
- print(entity['email'])
- print(entity['phone'])
- # Demonstrate how to update the entity by changing the phone number
- print('5. Update an existing entity by changing the phone number with sas')
- customer = {'PartitionKey': 'Harp', 'RowKey': '1', 'email' : 'harp@contoso.com', 'phone' : '425-123-1234'}
- shared_table_service.update_entity(table_name, customer)
- # Demonstrate how to delete an entity
- print('6. Delete the entity with sas')
- shared_table_service.delete_entity(table_name, 'Harp', '1')
- finally:
- print('7. Delete table')
- if(table_service.exists(table_name)):
- table_service.delete_table(table_name)
-
- print("Table operations with sas completed")
|