public void TableSasInvalidOperations() { CloudTableClient tableClient = GenerateCloudTableClient(); CloudTable table = tableClient.GetTableReference("T" + Guid.NewGuid().ToString("N")); try { table.Create(); // Prepare SAS authentication with full permissions string sasString = table.GetSharedAccessSignature( new SharedAccessTablePolicy { Permissions = SharedAccessTablePermissions.Delete, SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30) }, null, null, null, null, null); CloudTableClient sasClient = new CloudTableClient(tableClient.BaseUri, new StorageCredentials(sasString)); // Construct a valid set of service properties to upload. ServiceProperties properties = new ServiceProperties(); properties.Logging.Version = "1.0"; properties.Metrics.Version = "1.0"; properties.Logging.RetentionDays = 9; sasClient.GetServiceProperties(); sasClient.SetServiceProperties(properties); // Test invalid client operations // BUGBUG: ListTables hides the exception. We should fix this // TestHelpers.ExpectedException(() => sasClient.ListTablesSegmented(), "List tables with SAS", HttpStatusCode.NotFound); TestHelper.ExpectedException(() => sasClient.GetServiceProperties(), "Get service properties with SAS", HttpStatusCode.NotFound); TestHelper.ExpectedException(() => sasClient.SetServiceProperties(properties), "Set service properties with SAS", HttpStatusCode.NotFound); CloudTable sasTable = sasClient.GetTableReference(table.Name); // Verify that creation fails with SAS TestHelper.ExpectedException(() => sasTable.Create(), "Create a table with SAS", HttpStatusCode.NotFound); // Create the table. table.Create(); // Test invalid table operations TestHelper.ExpectedException(() => sasTable.Delete(), "Delete a table with SAS", HttpStatusCode.NotFound); TestHelper.ExpectedException(() => sasTable.GetPermissions(), "Get ACL with SAS", HttpStatusCode.NotFound); TestHelper.ExpectedException(() => sasTable.SetPermissions(new TablePermissions()), "Set ACL with SAS", HttpStatusCode.NotFound); } finally { table.DeleteIfExists(); } }
public ActionResult GetAzureQueueAndTableError() { string connectStorageString = @"DefaultEndpointsProtocol=https;AccountName=marystorage;AccountKey=lM069ZdGB5L70Tm5wRTPTp6hlfGmQdqk+sJp9pmoegHEn2ElWPfOW3tEcxV9fmbWTEaT7M9Km8Nzptzrt/Z1pQ==;BlobEndpoint=https://marystorage.blob.core.windows.net/;TableEndpoint=https://marystorage.table.core.windows.net/;QueueEndpoint=https://marystorage.queue.core.windows.net/;FileEndpoint=https://marystorage.file.core.windows.net"; CloudStorageAccount storageAccount = CloudStorageAccount.Parse( connectStorageString); //Create the queue client CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient(); // Create the table client. CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); // Retrieve a reference to a queue try { CloudQueue queue = queueClient.GetQueueReference("myqueueAIVENDOR"); queue.GetMessage(); } catch { } try { // Create the table failed because of existence. CloudTable table = tableClient.GetTableReference("people"); table.Create(); } catch { return(View()); } return(View()); }
private bool RecreateTableStorage() { CloudTable tableReference = QuoteDB.GetQuotesTableReference(); DateTime dtTimeout = DateTime.Now.AddSeconds(20); try { tableReference.Create(); } catch (StorageException ex) { if ((ex.RequestInformation.HttpStatusCode == 409) && (ex.RequestInformation.ExtendedErrorInformation.ErrorCode.Equals(TableErrorCodeStrings.TableBeingDeleted)) && (DateTime.Now < dtTimeout) ) { Thread.Sleep(2000);// The table is currently being deleted. Try again until it works. } else { new LogException(ex); throw new TimeoutException("Timeout waiting for empty database", ex); } } catch (Exception ex) { new LogException(ex); throw; } return(AddQuotesToNewTable()); }
private void EnsureExists(CloudTable table) { if (!table.Exists()) { table.Create(); } }
public ActionResult CreateTableIfNotExists(string tableName) { if (!string.IsNullOrEmpty(tableName)) { CloudTable table = AzureCommon.TableClient.GetTableReference(tableName); if (table.Exists()) { return(Content("Table already exist")); } try { table.Create(); } catch (Exception) { return(Content("Failed to create table")); } return(Content("Table created")); } else { return(Content("Please specify valid table name")); } }
private CloudTable InitializeTable() { CloudTable table = _manager.GetCloudTable("UseDevelopmentStorage=true;", "storagetest"); table.DeleteIfExists(); table.Create(); return(table); }
public Query() { var account = Util.GetStorageAccount(); var client = account.CreateCloudTableClient(); var name = $"CounterUtilTests{DateTime.Now.Ticks}"; _table = client.GetTableReference(name); _table.Create(); }
/// <summary> /// Event handler for the _timer's Elapsed event /// </summary> private void OnQueueProcessorTimerElapsed(object?_) { if (_isTimerRunning && (!_isDraining)) { return; } _isTimerRunning = true; if (_queue.Count > 0) { int counter = 0, total = _queue.Count; Stopwatch sw = new Stopwatch(); sw.Start(); while ((sw.ElapsedMilliseconds < __TIMER_RUN_TIME) && _queue.TryDequeue(out TableBatchOperationWrapper? value)) { string tableName = value.TableName; try { if (!tables.ContainsKey(tableName)) { CloudTable tableReference = tableClient.GetTableReference(tableName); if (!tableReference.Exists()) { tableReference.Create(); } tables.Add(tableName, tableReference); } tables[tableName].ExecuteBatch(value.Batch); TableOperation op = value.Batch[0]; Progress?.Invoke(++counter, total, tableName, Enum.GetName(typeof(TableOperationType), op.OperationType) ?? "Unknown", op.Entity.PartitionKey, op.Entity.RowKey); } catch (Exception ex) { Error?.Invoke(ex.Message, ex); #if TRACE Trace.WriteLine($"[OperationBatchQueue]: Table name: {tableName}\r\nException: {ex.Message}."); #endif // eat the exception, we dont want to miss our next timer // because of improper operations in the queue! } } sw.Stop(); } _isTimerRunning = false; _timer.Change(__TIMER_PERIOD, Timeout.Infinite); }
static void Main(string[] args) { Console.WriteLine("Table encryption sample"); // Retrieve storage account information from connection string // How to create a storage connection string - https://azure.microsoft.com/en-us/documentation/articles/storage-configure-connection-string/ CloudStorageAccount storageAccount = EncryptionShared.Utility.CreateStorageAccountFromConnectionString(); CloudTableClient client = storageAccount.CreateCloudTableClient(); CloudTable table = client.GetTableReference(DemoTable + Guid.NewGuid().ToString("N")); try { table.Create(); // Create the IKey used for encryption. RsaKey key = new RsaKey("private:key1"); EncryptedEntity ent = new EncryptedEntity() { PartitionKey = Guid.NewGuid().ToString(), RowKey = DateTime.Now.Ticks.ToString() }; ent.Populate(); TableRequestOptions insertOptions = new TableRequestOptions() { EncryptionPolicy = new TableEncryptionPolicy(key, null) }; // Insert Entity Console.WriteLine("Inserting the encrypted entity."); table.Execute(TableOperation.Insert(ent), insertOptions, null); // For retrieves, a resolver can be set up that will help pick the key based on the key id. LocalResolver resolver = new LocalResolver(); resolver.Add(key); TableRequestOptions retrieveOptions = new TableRequestOptions() { EncryptionPolicy = new TableEncryptionPolicy(null, resolver) }; // Retrieve Entity Console.WriteLine("Retrieving the encrypted entity."); TableOperation operation = TableOperation.Retrieve(ent.PartitionKey, ent.RowKey); TableResult result = table.Execute(operation, retrieveOptions, null); Console.WriteLine("Press enter key to exit"); Console.ReadLine(); } finally { table.DeleteIfExists(); } }
public TableHelper(string tableName) { _storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("DataConnectionString")); CloudTableClient tableClient = new CloudTableClient(new Uri(_storageAccount.TableEndpoint.AbsoluteUri), _storageAccount.Credentials); _table = tableClient.GetTableReference(tableName); if (_table.Exists() == false) { _table.Create(); } }
static CustomerManager() { storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]); tableClient = storageAccount.CreateCloudTableClient(); customersTable = tableClient.GetTableReference("Customers"); if (customersTable.Exists()) { customersTable.Delete(); } customersTable.DeleteIfExists(); customersTable.Create(); }
public CommentRepository(string partitionKey) { _partitionKey = partitionKey; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); _comments = tableClient.GetTableReference("comments"); if (!_comments.Exists()) { _comments.Create(); } }
/// <summary> /// Event handler for the _timer's Elapsed event /// </summary> private void OnProcessQueueTimerElapsed(object?_) { if (_isTimerRunning && (!_isDraining)) { return; } _isTimerRunning = true; if (_queue.Count == 0) { ResetTimer(); return; } while (_queueOrder.TryDequeue(out ulong queueID)) { if (!_queue.TryRemove(queueID, out TableOperationWrapper? value)) { continue; } try { if (!tables.ContainsKey(value.TableName)) { CloudTable tableReference = tableClient.GetTableReference(value.TableName); if (!tableReference.Exists()) { tableReference.Create(); } tables.Add(value.TableName, tableReference); } tables[value.TableName].Execute(value.Operation); } catch { // eat the exception, we dont want to miss our next timer // because of improper operations in the queue! } } ResetTimer(); }
public AzureStorageCache(IOptions <AzureStorageCacheOptions> options) { if (options is null) { throw new ArgumentNullException(nameof(options)); } CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(options.Value.ConnectionString); CloudTableClient cloutTableClient = cloudStorageAccount.CreateCloudTableClient(); CloudTable cloudTable = cloutTableClient.GetTableReference(options.Value.TableName); if (cloudTable.Exists() == false) { cloudTable.Create(); } _provider = new AzureStorageProvider(cloudTable, options.Value.PartitionKey); }
public static void ClassInitialize(TestContext context) { try { s_storageAccount = CloudStorageAccount.DevelopmentStorageAccount; CloudTableClient tableClient = s_storageAccount.CreateCloudTableClient(); s_eventTable = tableClient.GetTableReference("AzureEventStoreTestEventStore"); s_eventTable.DeleteIfExists(new TableRequestOptions { RetryPolicy = new NoRetry() }); s_eventTable.Create(); s_storageEmulatorConnected = true; } catch (StorageException exception) when(exception.InnerException is WebException) { context.WriteLine("{0}", exception); } }
private void InitializeCrawlingTable() { // Retrieve storage account from connection string CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString")); // Create the table client CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); // Retrieve a reference to CrawlingTable CloudTable crawlingTable = tableClient.GetTableReference(WebRole.AZURE_CRAWLING_TABLE); // Create the table if it doesn't exist crawlingTable.CreateIfNotExists(); // Clear the table crawlingTable.Delete(); crawlingTable.Create(); }
private CloudTable CreateConnection() { //CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(AccountName, AccessKey), true); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationString); // Create the table client. CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); // Get a reference to a table named "items" CloudTable table = tableClient.GetTableReference(TableName); // Create the table if it doesn't exist //table.CreateIfNotExists(); // DON"T use this, it throws an internal 409 in App insights: https://stackoverflow.com/questions/48893519/azure-table-storage-exception-409-conflict-unexpected if (!table.Exists()) { table.Create(); } return(table); }
static void Main(string[] args) { var planetes = JsonConvert.DeserializeObject <Dictionary <string, string> >(File.ReadAllText("planets.json")); CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("tableStorage")); CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); // Retrieve a reference to the table and delete it. CloudTable table = tableClient.GetTableReference("planets"); table.DeleteIfExists(); // Create the table if it doesn't exist. table.Create(); foreach (var item in planetes) { table.Execute(TableOperation.Insert(new Planet(item.Key, item.Value))); } }
// Constructor. public Logger(string tableName, string accountName, string accountKey) { //Create storage credentials object. credentials = new StorageCredentials(accountName, accountKey); //Create storage account object. account = new CloudStorageAccount(credentials, false); //Create table client object. tableClient = account.CreateCloudTableClient(); //Get the table reference. table = tableClient.GetTableReference(tableName); //Check whether table exist or not. if (!table.Exists()) { //Create the table if not exist. table.Create(); } }
public static AWCloudTable GetTable(MSGorillaTable table) { if (_tableCache.ContainsKey(table)) { return(_tableCache[table]); } var client = AzureStorageAccount.CreateCloudTableClient(); CloudTable aztable = client.GetTableReference(_tableDict[table]); aztable.CreateIfNotExists(); CloudTable wosstable = null; if (WossStorageAccount != null) { client = WossStorageAccount.CreateCloudTableClient(); wosstable = client.GetTableReference(_tableDict[table]); DateTimeOffset startTime = DateTimeOffset.UtcNow; OperationContext opContext = new OperationContext(); try { wosstable.Create(null, opContext); } catch (Exception e) { if (!(e is StorageException && e.InnerException != null && e.InnerException.Message.Equals("The remote server returned an error: (409) Conflict."))) { Logger.Error(e, startTime, DateTime.Now, wosstable.Uri.ToString(), "CreateTable", opContext); } } } AWCloudTable awTable = new AWCloudTable(aztable, wosstable); _tableCache[table] = awTable; return(awTable); }
public void TableSetGetPermissionsSync() { CloudTableClient tableClient = GenerateCloudTableClient(); CloudTable table = tableClient.GetTableReference("T" + Guid.NewGuid().ToString("N")); try { table.Create(); TableServiceContext context = tableClient.GetTableServiceContext(); context.AddObject(table.Name, new BaseEntity("PK", "RK")); context.SaveChangesWithRetries(); TablePermissions expectedPermissions = new TablePermissions(); TablePermissions testPermissions = table.GetPermissions(); AssertPermissionsEqual(expectedPermissions, testPermissions); // Add a policy, check setting and getting. expectedPermissions.SharedAccessPolicies.Add(Guid.NewGuid().ToString(), new SharedAccessTablePolicy { Permissions = SharedAccessTablePermissions.Query, SharedAccessStartTime = DateTimeOffset.Now - TimeSpan.FromHours(1), SharedAccessExpiryTime = DateTimeOffset.Now + TimeSpan.FromHours(1) }); table.SetPermissions(expectedPermissions); Thread.Sleep(30 * 1000); testPermissions = table.GetPermissions(); AssertPermissionsEqual(expectedPermissions, testPermissions); } finally { table.DeleteIfExists(); } }
public void TableSASConstructors() { CloudTableClient tableClient = GenerateCloudTableClient(); CloudTable table = tableClient.GetTableReference("T" + Guid.NewGuid().ToString("N")); try { table.Create(); TableServiceContext context = tableClient.GetTableServiceContext(); context.AddObject(table.Name, new BaseEntity("PK", "RK")); context.SaveChangesWithRetries(); // Prepare SAS authentication with full permissions string sasToken = table.GetSharedAccessSignature( new SharedAccessTablePolicy { Permissions = SharedAccessTablePermissions.Add | SharedAccessTablePermissions.Delete | SharedAccessTablePermissions.Query, SharedAccessExpiryTime = DateTimeOffset.Now.AddMinutes(30) }, null /* accessPolicyIdentifier */, null /* startPk */, null /* startRk */, null /* endPk */, null /* endRk */); CloudStorageAccount sasAccount; StorageCredentials sasCreds; CloudTableClient sasClient; CloudTable sasTable; TableServiceContext sasContext; Uri baseUri = new Uri(TestBase.TargetTenantConfig.TableServiceEndpoint); int count; // SAS via connection string parse sasAccount = CloudStorageAccount.Parse(string.Format("TableEndpoint={0};SharedAccessSignature={1}", baseUri.AbsoluteUri, sasToken)); sasClient = sasAccount.CreateCloudTableClient(); sasTable = sasClient.GetTableReference(table.Name); sasContext = sasClient.GetTableServiceContext(); count = sasContext.CreateQuery <BaseEntity>(sasTable.Name).AsTableServiceQuery(sasContext).Execute().Count(); Assert.AreEqual(1, count); // SAS via account constructor sasCreds = new StorageCredentials(sasToken); sasAccount = new CloudStorageAccount(sasCreds, null, null, baseUri); sasClient = sasAccount.CreateCloudTableClient(); sasTable = sasClient.GetTableReference(table.Name); sasContext = sasClient.GetTableServiceContext(); count = sasContext.CreateQuery <BaseEntity>(sasTable.Name).AsTableServiceQuery(sasContext).Execute().Count(); Assert.AreEqual(1, count); // SAS via client constructor URI + Creds sasCreds = new StorageCredentials(sasToken); sasClient = new CloudTableClient(baseUri, sasCreds); sasContext = sasClient.GetTableServiceContext(); count = sasContext.CreateQuery <BaseEntity>(sasTable.Name).AsTableServiceQuery(sasContext).Execute().Count(); Assert.AreEqual(1, count); // SAS via CloudTable constructor Uri + Client sasCreds = new StorageCredentials(sasToken); sasTable = new CloudTable(table.Uri, tableClient); sasClient = sasTable.ServiceClient; sasContext = sasClient.GetTableServiceContext(); count = sasContext.CreateQuery <BaseEntity>(sasTable.Name).AsTableServiceQuery(sasContext).Execute().Count(); Assert.AreEqual(1, count); } finally { table.DeleteIfExists(); } }
public void ClearTable() { _table.Delete(); _table.Create(); }
public void Create() { table.Create(); }
public void TableGetSetPermissionTest() { CloudTableClient tableClient = GenerateCloudTableClient(); CloudTable table = tableClient.GetTableReference("T" + Guid.NewGuid().ToString("N")); try { table.Create(); TableServiceContext context = tableClient.GetTableServiceContext(); context.AddObject(table.Name, new BaseEntity("PK", "RK")); context.SaveChangesWithRetries(); TablePermissions expectedPermissions; TablePermissions testPermissions; // Test new table permissions. expectedPermissions = new TablePermissions(); testPermissions = table.GetPermissions(); AssertPermissionsEqual(expectedPermissions, testPermissions); // Test setting empty permissions. table.SetPermissions(expectedPermissions); TestHelper.SpinUpTo30SecondsIgnoringFailures(() => { testPermissions = table.GetPermissions(); AssertPermissionsEqual(expectedPermissions, testPermissions); }); // Add a policy, check setting and getting. expectedPermissions.SharedAccessPolicies.Add(Guid.NewGuid().ToString(), new SharedAccessTablePolicy { Permissions = SharedAccessTablePermissions.Query, SharedAccessStartTime = DateTimeOffset.Now - TimeSpan.FromHours(1), SharedAccessExpiryTime = DateTimeOffset.Now + TimeSpan.FromHours(1) }); table.SetPermissions(expectedPermissions); TestHelper.SpinUpTo30SecondsIgnoringFailures(() => { testPermissions = table.GetPermissions(); AssertPermissionsEqual(expectedPermissions, testPermissions); }); // Add a policy, check setting and getting. expectedPermissions.SharedAccessPolicies.Add(Guid.NewGuid().ToString(), new SharedAccessTablePolicy { Permissions = SharedAccessTablePermissions.Delete | SharedAccessTablePermissions.Add, SharedAccessStartTime = DateTimeOffset.Now + TimeSpan.FromHours(1), SharedAccessExpiryTime = DateTimeOffset.Now + TimeSpan.FromDays(1) }); table.SetPermissions(expectedPermissions); TestHelper.SpinUpTo30SecondsIgnoringFailures(() => { testPermissions = table.GetPermissions(); AssertPermissionsEqual(expectedPermissions, testPermissions); }); // Add a null policy, check setting and getting. expectedPermissions.SharedAccessPolicies.Add(Guid.NewGuid().ToString(), new SharedAccessTablePolicy { Permissions = SharedAccessTablePermissions.None, }); table.SetPermissions(expectedPermissions); TestHelper.SpinUpTo30SecondsIgnoringFailures(() => { testPermissions = table.GetPermissions(); AssertPermissionsEqual(expectedPermissions, testPermissions); }); // Add a policy, check setting and getting. expectedPermissions.SharedAccessPolicies.Add(Guid.NewGuid().ToString(), new SharedAccessTablePolicy { Permissions = SharedAccessTablePermissions.Add | SharedAccessTablePermissions.Query | SharedAccessTablePermissions.Update | SharedAccessTablePermissions.Delete, SharedAccessStartTime = DateTimeOffset.Now + TimeSpan.FromDays(0.5), SharedAccessExpiryTime = DateTimeOffset.Now + TimeSpan.FromDays(1) }); table.SetPermissions(expectedPermissions); TestHelper.SpinUpTo30SecondsIgnoringFailures(() => { testPermissions = table.GetPermissions(); AssertPermissionsEqual(expectedPermissions, testPermissions); }); // Add a policy, check setting and getting. expectedPermissions.SharedAccessPolicies.Add(Guid.NewGuid().ToString(), new SharedAccessTablePolicy { Permissions = SharedAccessTablePermissions.Update, SharedAccessStartTime = DateTimeOffset.Now + TimeSpan.FromHours(6), SharedAccessExpiryTime = DateTimeOffset.Now + TimeSpan.FromHours(6.5) }); table.SetPermissions(expectedPermissions); TestHelper.SpinUpTo30SecondsIgnoringFailures(() => { testPermissions = table.GetPermissions(); AssertPermissionsEqual(expectedPermissions, testPermissions); }); } finally { table.DeleteIfExists(); } }
/// <summary> /// Imports data of DataTable to table storage /// </summary> /// <param name="dtSheetInfo"></param> /// <param name="strSheetName"></param> private void ImportDataToTable(System.Data.DataTable dtSheetInfo, string strSheetName) { var client = storageAccount.CreateCloudTableClient(); CloudTable table = client.GetTableReference(strPIITable); Response.Write(new string(' ', 1024)); Response.Write(String.Format("<div>Deleting existing data")); Response.Flush(); table.DeleteIfExists(); create: try { Response.Write("."); Response.Flush(); table.Create(); } catch (StorageException ex) when(ex.RequestInformation.ExtendedErrorInformation.ErrorCode.Equals(TableErrorCodeStrings.TableBeingDeleted)) { Thread.Sleep(1000); goto create; } Response.Write(String.Format("</div><div>Uploading {0} rows for sheet {1}", dtSheetInfo.Rows.Count, strSheetName.Replace("$", ""))); Response.Flush(); // Create a new partition key for this data instead of overwriting old data. var partitionKey = strSheetName; var batch = new TableBatchOperation(); for (int j = 0; j < dtSheetInfo.Rows.Count; j++) { ExcelTableEntity entity = new ExcelTableEntity(partitionKey, (j + 2).ToString("D5")); var hasContent = false; for (int i = 0; i < dtSheetInfo.Columns.Count; i++) { string strCloName = dtSheetInfo.Columns[i].ColumnName; if (!(dtSheetInfo.Rows[j][i] is DBNull) && (dtSheetInfo.Rows[j][i] != null)) { hasContent = true; string strValue = dtSheetInfo.Rows[j][i].ToString().Trim(); if (!CheckPropertyExist(strCloName, strValue, entity)) { EntityProperty property = entity.ConvertToEntityProperty(strCloName, dtSheetInfo.Rows[j][i]); if (!entity.properties.ContainsKey(strCloName)) { entity.properties.Add(strCloName, property); } else { entity.properties[strCloName] = property; } } } } if (hasContent) { batch.Add(TableOperation.InsertOrReplace(entity)); } if (batch.Count >= 100) { table.ExecuteBatch(batch); Response.Write("."); Response.Flush(); batch.Clear(); } } if (batch.Count > 0) { table.ExecuteBatch(batch); Response.Write("."); Response.Flush(); } Response.Write("</div><hr/>"); Response.Flush(); }
public void Create(TableRequestOptions requestOptions = null, OperationContext operationContext = null) { _cloudTable.Create(requestOptions, operationContext); }
public void TableGetSetPermissionTestSync() { CloudTableClient tableClient = GenerateCloudTableClient(); CloudTable table = tableClient.GetTableReference("T" + Guid.NewGuid().ToString("N")); try { table.Create(); table.Execute(TableOperation.Insert(new BaseEntity("PK", "RK"))); TablePermissions expectedPermissions; TablePermissions testPermissions; // Test new table permissions. expectedPermissions = new TablePermissions(); testPermissions = table.GetPermissions(); AssertPermissionsEqual(expectedPermissions, testPermissions); // Test setting empty permissions. table.SetPermissions(expectedPermissions); Thread.Sleep(30 * 1000); testPermissions = table.GetPermissions(); AssertPermissionsEqual(expectedPermissions, testPermissions); // Add a policy, check setting and getting. expectedPermissions.SharedAccessPolicies.Add(Guid.NewGuid().ToString(), new SharedAccessTablePolicy { Permissions = SharedAccessTablePermissions.Query, SharedAccessStartTime = DateTimeOffset.Now - TimeSpan.FromHours(1), SharedAccessExpiryTime = DateTimeOffset.Now + TimeSpan.FromHours(1) }); table.SetPermissions(expectedPermissions); Thread.Sleep(30 * 1000); testPermissions = table.GetPermissions(); AssertPermissionsEqual(expectedPermissions, testPermissions); // Add a policy, check setting and getting. expectedPermissions.SharedAccessPolicies.Add(Guid.NewGuid().ToString(), new SharedAccessTablePolicy { Permissions = SharedAccessTablePermissions.Delete | SharedAccessTablePermissions.Add, SharedAccessStartTime = DateTimeOffset.Now + TimeSpan.FromHours(1), SharedAccessExpiryTime = DateTimeOffset.Now + TimeSpan.FromDays(1) }); table.SetPermissions(expectedPermissions); Thread.Sleep(30 * 1000); testPermissions = table.GetPermissions(); AssertPermissionsEqual(expectedPermissions, testPermissions); // Add a null policy, check setting and getting. expectedPermissions.SharedAccessPolicies.Add(Guid.NewGuid().ToString(), new SharedAccessTablePolicy { Permissions = SharedAccessTablePermissions.None, }); table.SetPermissions(expectedPermissions); Thread.Sleep(30 * 1000); testPermissions = table.GetPermissions(); AssertPermissionsEqual(expectedPermissions, testPermissions); // Add a policy, check setting and getting. expectedPermissions.SharedAccessPolicies.Add(Guid.NewGuid().ToString(), new SharedAccessTablePolicy { Permissions = SharedAccessTablePermissions.Add | SharedAccessTablePermissions.Query | SharedAccessTablePermissions.Update | SharedAccessTablePermissions.Delete, SharedAccessStartTime = DateTimeOffset.Now + TimeSpan.FromDays(0.5), SharedAccessExpiryTime = DateTimeOffset.Now + TimeSpan.FromDays(1) }); table.SetPermissions(expectedPermissions); Thread.Sleep(30 * 1000); testPermissions = table.GetPermissions(); AssertPermissionsEqual(expectedPermissions, testPermissions); // Add a policy, check setting and getting. expectedPermissions.SharedAccessPolicies.Add(Guid.NewGuid().ToString(), new SharedAccessTablePolicy { Permissions = SharedAccessTablePermissions.Update, SharedAccessStartTime = DateTimeOffset.Now + TimeSpan.FromHours(6), SharedAccessExpiryTime = DateTimeOffset.Now + TimeSpan.FromHours(6.5) }); table.SetPermissions(expectedPermissions); Thread.Sleep(30 * 1000); testPermissions = table.GetPermissions(); AssertPermissionsEqual(expectedPermissions, testPermissions); } finally { table.DeleteIfExists(); } }
static void Main(string[] args) { Console.WriteLine("Table encryption sample"); // Retrieve storage account information from connection string // How to create a storage connection string - https://azure.microsoft.com/en-us/documentation/articles/storage-configure-connection-string/ CloudStorageAccount storageAccount = EncryptionShared.Utility.CreateStorageAccountFromConnectionString(); CloudTableClient client = storageAccount.CreateCloudTableClient(); CloudTable table = client.GetTableReference(DemoTable + Guid.NewGuid().ToString("N")); try { table.Create(); // Create the IKey used for encryption. RsaKey key = new RsaKey("private:key1"); DynamicTableEntity ent = new DynamicTableEntity() { PartitionKey = Guid.NewGuid().ToString(), RowKey = DateTime.Now.Ticks.ToString() }; ent.Properties.Add("EncryptedProp1", new EntityProperty(string.Empty)); ent.Properties.Add("EncryptedProp2", new EntityProperty("bar")); ent.Properties.Add("NotEncryptedProp", new EntityProperty(1234)); // This is used to indicate whether a property should be encrypted or not given the partition key, row key, // and the property name. Func <string, string, string, bool> encryptionResolver = (pk, rk, propName) => { if (propName.StartsWith("EncryptedProp")) { return(true); } return(false); }; TableRequestOptions insertOptions = new TableRequestOptions() { EncryptionPolicy = new TableEncryptionPolicy(key, null), EncryptionResolver = encryptionResolver }; // Insert Entity Console.WriteLine("Inserting the encrypted entity."); table.Execute(TableOperation.Insert(ent), insertOptions, null); // For retrieves, a resolver can be set up that will help pick the key based on the key id. LocalResolver resolver = new LocalResolver(); resolver.Add(key); TableRequestOptions retrieveOptions = new TableRequestOptions() { EncryptionPolicy = new TableEncryptionPolicy(null, resolver) }; // Retrieve Entity Console.WriteLine("Retrieving the encrypted entity."); TableOperation operation = TableOperation.Retrieve(ent.PartitionKey, ent.RowKey); TableResult result = table.Execute(operation, retrieveOptions, null); Console.WriteLine("Press enter key to exit"); Console.ReadLine(); } finally { table.DeleteIfExists(); } }
/// <summary> /// Tests table access permissions with SAS, using a stored policy and using permissions on the URI. /// </summary> /// <param name="accessPermissions">The permissions to test.</param> /// <param name="startPk">The start partition key range.</param> /// <param name="startRk">The start row key range.</param> /// <param name="endPk">The end partition key range.</param> /// <param name="endRk">The end row key range.</param> internal void TestTableSasWithRange( SharedAccessTablePermissions accessPermissions, string startPk, string startRk, string endPk, string endRk) { TestContext.WriteLine("Testing SAS range: spk={0}; epk={1}; srk={2}; erk={3}", startPk, endPk, startRk, endRk); CloudTableClient tableClient = GenerateCloudTableClient(); CloudTable table = tableClient.GetTableReference("T" + Guid.NewGuid().ToString("N")); try { table.Create(); // Set up a policy string identifier = Guid.NewGuid().ToString(); TablePermissions permissions = new TablePermissions(); permissions.SharedAccessPolicies.Add(identifier, new SharedAccessTablePolicy { Permissions = accessPermissions, SharedAccessExpiryTime = DateTimeOffset.Now.AddDays(1) }); table.SetPermissions(permissions); Thread.Sleep(30 * 1000); // Prepare SAS authentication using access identifier string sasString = table.GetSharedAccessSignature(new SharedAccessTablePolicy(), identifier, startPk, startRk, endPk, endRk); CloudTableClient identifierSasClient = new CloudTableClient(tableClient.BaseUri, new StorageCredentials(sasString)); // Prepare SAS authentication using explicit policy sasString = table.GetSharedAccessSignature( new SharedAccessTablePolicy { Permissions = accessPermissions, SharedAccessExpiryTime = DateTimeOffset.Now.AddMinutes(30) }, null, startPk, startRk, endPk, endRk); CloudTableClient explicitSasClient = new CloudTableClient(tableClient.BaseUri, new StorageCredentials(sasString)); // Point query TestPointQuery(identifierSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk); TestPointQuery(explicitSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk); // Add row TestAdd(identifierSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk); TestAdd(explicitSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk); // Update row (merge) TestUpdateMerge(identifierSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk); TestUpdateMerge(explicitSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk); // Update row (replace) TestUpdateReplace(identifierSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk); TestUpdateReplace(explicitSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk); // Delete row TestDelete(identifierSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk); TestDelete(explicitSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk); // Upsert row (merge) TestUpsertMerge(identifierSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk); TestUpsertMerge(explicitSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk); // Upsert row (replace) TestUpsertReplace(identifierSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk); TestUpsertReplace(explicitSasClient, table.Name, accessPermissions, startPk, startRk, endPk, endRk); } finally { table.DeleteIfExists(); } }