public WindowsAzureStorageHelper() { ContainerName = CONTAINER_NAME; // Open blob storage. StorageAccountInfo = StorageAccountInfo.GetDefaultBlobStorageAccountFromConfiguration(); BlobStorageType = BlobStorage.Create(StorageAccountInfo); BlobStorageType.RetryPolicy = RetryPolicies.RetryN(2, TimeSpan.FromMilliseconds(100)); // Open queue storage. StorageAccountInfo queueAccount = StorageAccountInfo.GetDefaultQueueStorageAccountFromConfiguration(); QueueStorageType = QueueStorage.Create(queueAccount); QueueStorageType = QueueStorage.Create(queueAccount); QueueStorageType.RetryPolicy = RetryPolicies.RetryN(2, TimeSpan.FromMilliseconds(100)); // Open table storage. StorageAccountInfo tableAccount = StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration(); TableStorageType = TableStorage.Create(tableAccount); TableStorageType = TableStorage.Create(tableAccount); TableStorageType.RetryPolicy = RetryPolicies.RetryN(2, TimeSpan.FromMilliseconds(100)); }
private BlobContainer GetContainer() { BlobStorage blobStorage = BlobStorage.Create(StorageAccountInfo.GetDefaultBlobStorageAccountFromConfiguration()); BlobContainer newContainer = blobStorage.GetBlobContainer(RoleEnvironment.GetConfigurationSettingValue("ContainerName")); newContainer.CreateContainer(null, ContainerAccessControl.Public); return(newContainer); }
public override void Initialize(string name, NameValueCollection config) { // Verify that config isn't null if (config == null) { throw new ArgumentNullException("config"); } // Assign the provider a default name if it doesn't have one if (String.IsNullOrEmpty(name)) { name = "TableServiceSessionStateProvider"; } // Add a default "description" attribute to config if the // attribute doesn't exist or is empty if (string.IsNullOrEmpty(config["description"])) { config.Remove("description"); config.Add("description", "Session state provider using table storage"); } // Call the base class's Initialize method base.Initialize(name, config); bool allowInsecureRemoteEndpoints = Configuration.GetBooleanValue(config, "allowInsecureRemoteEndpoints", false); // structure storage-related properties _applicationName = Configuration.GetStringValueWithGlobalDefault(config, "applicationName", Configuration.DefaultProviderApplicationNameConfigurationString, Configuration.DefaultProviderApplicationName, false); _accountName = Configuration.GetStringValue(config, "accountName", null, true); _sharedKey = Configuration.GetStringValue(config, "sharedKey", null, true); _tableName = Configuration.GetStringValueWithGlobalDefault(config, "sessionTableName", Configuration.DefaultSessionTableNameConfigurationString, Configuration.DefaultSessionTableName, false); _tableServiceBaseUri = Configuration.GetStringValue(config, "tableServiceBaseUri", null, true); _containerName = Configuration.GetStringValueWithGlobalDefault(config, "containerName", Configuration.DefaultSessionContainerNameConfigurationString, Configuration.DefaultSessionContainerName, false); if (!SecUtility.IsValidContainerName(_containerName)) { throw new ProviderException("The provider configuration for the TableStorageSessionStateProvider does not contain a valid container name. " + "Please refer to the documentation for the concrete rules for valid container names." + "The current container name is: " + _containerName); } _blobServiceBaseUri = Configuration.GetStringValue(config, "blobServiceBaseUri", null, true); config.Remove("allowInsecureRemoteEndpoints"); config.Remove("accountName"); config.Remove("sharedKey"); config.Remove("containerName"); config.Remove("applicationName"); config.Remove("blobServiceBaseUri"); config.Remove("tableServiceBaseUri"); config.Remove("sessionTableName"); // Throw an exception if unrecognized attributes remain if (config.Count > 0) { string attr = config.GetKey(0); if (!String.IsNullOrEmpty(attr)) { throw new ProviderException ("Unrecognized attribute: " + attr); } } StorageAccountInfo tableInfo = null; StorageAccountInfo blobInfo = null; try { tableInfo = StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration(true); blobInfo = StorageAccountInfo.GetDefaultBlobStorageAccountFromConfiguration(true); if (_tableServiceBaseUri != null) { tableInfo.BaseUri = new Uri(_tableServiceBaseUri); } if (_blobServiceBaseUri != null) { blobInfo.BaseUri = new Uri(_blobServiceBaseUri); } if (_accountName != null) { tableInfo.AccountName = _accountName; blobInfo.AccountName = _accountName; } if (_sharedKey != null) { tableInfo.Base64Key = _sharedKey; blobInfo.Base64Key = _sharedKey; } tableInfo.CheckComplete(); SecUtility.CheckAllowInsecureEndpoints(allowInsecureRemoteEndpoints, tableInfo); blobInfo.CheckComplete(); SecUtility.CheckAllowInsecureEndpoints(allowInsecureRemoteEndpoints, blobInfo); _tableStorage = TableStorage.Create(tableInfo); _tableStorage.RetryPolicy = _tableRetry; _tableStorage.TryCreateTable(_tableName); _blobProvider = new BlobProvider(blobInfo, _containerName); } catch (SecurityException) { throw; } // catch InvalidOperationException as well as StorageException catch (Exception e) { string exceptionDescription = Configuration.GetInitExceptionDescription(tableInfo, blobInfo); string tableName = (_tableName == null) ? "no session table name specified" : _tableName; string containerName = (_containerName == null) ? "no container name specified" : _containerName; Log.Write(EventKind.Error, "Initialization of data service structures (tables and/or blobs) failed!" + exceptionDescription + Environment.NewLine + "Configured blob container: " + containerName + Environment.NewLine + "Configured table name: " + tableName + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace); throw new ProviderException("Initialization of data service structures (tables and/or blobs) failed!" + "The most probable reason for this is that " + "the storage endpoints are not configured correctly. Please look at the configuration settings " + "in your .cscfg and Web.config files. More information about this error " + "can be found in the logs when running inside the hosting environment or in the output " + "window of Visual Studio.", e); } Debug.Assert(_blobProvider != null); }
internal static void RunSamples() { StorageAccountInfo account = StorageAccountInfo.GetDefaultBlobStorageAccountFromConfiguration(); string containerName = StorageAccountInfo.GetConfigurationSetting("ContainerName", null, true); NameValueCollection containerMetadata = new NameValueCollection(); containerMetadata.Add("Name", "StorageSample"); BlobStorage blobStorage = BlobStorage.Create(account); blobStorage.RetryPolicy = RetryPolicies.RetryN(2, TimeSpan.FromMilliseconds(100)); try { BlobContainer container = blobStorage.GetBlobContainer(containerName); //Create the container if it does not exist. container.CreateContainer(containerMetadata, ContainerAccessControl.Private); ContainerProperties containerProperties = container.GetContainerProperties(); Console.WriteLine("Container {0} LastModified {1} ETag {2} Metadata {3}", containerProperties.Name, containerProperties.LastModifiedTime, containerProperties.ETag, StringBlob.MetadataToString(containerProperties.Metadata) ); ContainerAccessControl acl = container.GetContainerAccessControl(); Console.WriteLine("Container has access control {0}", acl); // write some text blobs NameValueCollection nv1 = new NameValueCollection(); nv1["m1"] = "v1"; nv1["m2"] = "v2"; StringBlob hello1 = new StringBlob("hello.txt", "Hello World"); hello1.Blob.Metadata = nv1; Console.WriteLine("Creating blob hello.txt"); PutTextBlob(container, hello1); BlobProperties prop = container.GetBlobProperties("hello.txt"); Console.WriteLine("hello.txt content length = " + prop.ContentLength); StringBlob goodbye1 = new StringBlob("goodbye.txt", "Goodbye world"); Console.WriteLine("Creating blob goodbye.txt"); PutTextBlob(container, goodbye1); // read back the blobs Console.WriteLine("Getting hello.txt and goodbye.txt"); StringBlob hello2 = GetTextBlob(container, "hello.txt"); Console.WriteLine("hello.txt: " + hello2.ToString()); StringBlob goodbye2 = GetTextBlob(container, "goodbye.txt"); Console.WriteLine("goodbye.txt " + goodbye2.ToString()); //Try to get a blob that does not exist try { GetTextBlob(container, "noSuchBlob"); } catch (StorageClientException sce) { //The extended error information when present provides more specific and detailed information // about the cause of the error. Console.WriteLine( "Error attempting to get blob 'noSuchBlob' Error Code = {0} Message = {1}", sce.ExtendedErrorInformation != null ? sce.ExtendedErrorInformation.ErrorCode : sce.ErrorCode.ToString(), sce.Message ); } //update metadata of hello.txt hello2.Blob.Metadata["m3"] = "v3"; Console.WriteLine("Updating metadata of hello.txt"); container.UpdateBlobMetadata(hello2.Blob); hello2.Blob.Metadata["m4"] = "v4"; container.UpdateBlobMetadataIfNotModified(hello2.Blob); //Refresh hello.txt. It has changed. bool refreshed = RefreshTextBlob(container, hello1); if (refreshed) { Console.WriteLine("hello.txt refreshed " + hello1.ToString()); } else { Console.WriteLine("hello.txt not refreshed"); } Console.WriteLine("Uploading a large blob"); PutLargeString( container, new StringBlob("LargeBlob.txt", "Let us repeat this string a large number of times "), 50000 ); Console.WriteLine("Downloading large blob to file LargeBlob.txt"); DownloadToFile(container, "LargeBlob.txt", "LargeBlob.txt"); //Refresh hello.txt. It hasn't changed. refreshed = RefreshTextBlob(container, hello2); if (refreshed) { Console.WriteLine("hello.txt refreshed " + hello2.ToString()); } else { Console.WriteLine("hello.txt not refreshed"); } //Change goodbye.txt and refresh it StringBlob goodbye3 = new StringBlob("goodbye.txt", "Goodbye again world"); PutTextBlob(container, goodbye3); //Now refresh the other reference to goodbye.txt (goodbye2) refreshed = RefreshTextBlob(container, goodbye2); if (refreshed) { Console.WriteLine("goodbye.txt refreshed " + goodbye2.ToString()); } else { Console.WriteLine("goodbye.txt not refreshed"); } //Update hello.txt hello2.Value = "Hello again world"; bool updated = UpdateTextBlob(container, hello2); if (updated) { Console.WriteLine("hello.txt updated " + hello2.ToString()); } else { Console.WriteLine("hello.txt not updated because it has been changed"); } //Try to update goodbye.txt through goodbye1. //This should fail because it has been updated thru goodbye3 goodbye1.Value = "Farewell world"; updated = UpdateTextBlob(container, goodbye1); if (updated) { Console.WriteLine("goodbye.txt updated " + goodbye1.ToString()); } else { Console.WriteLine("goodbye.txt not updated because it has been changed"); } Console.WriteLine("Creating blob 'deleteme.txt'"); PutTextBlob(container, new StringBlob("deleteme.txt", "deleteme")); Console.WriteLine("Creating blobs f/a.txt and f/b.txt"); PutTextBlob(container, new StringBlob("f/a.txt", "This is a.txt")); PutTextBlob(container, new StringBlob("f/b.txt", "This is b.txt")); Console.WriteLine("Enumerating all blobs"); // enumerate all the blobs foreach (object b1 in container.ListBlobs("", false)) { Console.WriteLine("{0}", ((BlobProperties)b1).Uri); } Console.WriteLine("Enumerating all blobs with combining common prefixes"); foreach (object b2 in container.ListBlobs("", true)) { BlobProperties blobProperties = b2 as BlobProperties; if (blobProperties != null) { Console.WriteLine("{0}", blobProperties.Uri); } else { Console.WriteLine("Common prefix: {0}", (string)b2); } } Console.WriteLine("Deleting blob 'deleteme.txt'"); container.DeleteBlob("deleteme.txt"); Console.WriteLine("Enumerate the blobs again"); foreach (object b3 in container.ListBlobs("", false)) { Console.WriteLine("{0}", ((BlobProperties)b3).Uri); } // Create another container Console.WriteLine("Creating container 'deleteme'"); BlobContainer container2 = blobStorage.GetBlobContainer("deleteme"); container2.CreateContainer(); // enumerate containers foreach (BlobContainer c in blobStorage.ListBlobContainers()) { Console.WriteLine("Container: {0}", c.ContainerUri); } // Delete the container Console.WriteLine("Deleting container 'deleteme'"); container2.DeleteContainer(); // enumerate containers foreach (BlobContainer c in blobStorage.ListBlobContainers()) { Console.WriteLine("Container: {0}", c.ContainerUri); } } catch (System.Net.WebException we) { Console.WriteLine("Network error: " + we.Message); if (we.Status == System.Net.WebExceptionStatus.ConnectFailure) { Console.WriteLine("Please check if the blob storage service is running at " + account.BaseUri.ToString()); Console.WriteLine("Detailed information on how to run the development storage tool " + "locally can be found in the readme file that comes with this sample."); } } catch (StorageException se) { Console.WriteLine("Storage service error: " + se.Message); } }