private BlobContainer GetContainer() { // we have to make sure that only one thread tries to create the container lock (_lock) { if (_container != null) { return(_container); } try { BlobContainer container = BlobStorage.Create(_info).GetBlobContainer(_containerName); container.Timeout = _Timeout; container.RetryPolicy = _RetryPolicy; container.CreateContainer(); _container = container; return(_container); } catch (StorageException se) { Log.Write(EventKind.Error, "Error creating container {0}: {1}", ContainerUrl, se.Message); throw; } } }
public static BlobContainer GetAzureContainer(string containerName) { StorageAccountInfo accountInfo = StorageAccountInfo.GetAccountInfoFromConfiguration("BlobStorageEndpoint"); BlobStorage blobStorage = BlobStorage.Create(accountInfo); //The default timeout of 30 seconds is far too short, make it 6 hours. blobStorage.Timeout = new TimeSpan(6, 0, 0); if (String.IsNullOrEmpty(containerName)) { // Default name for new container; Container names have the same restrictions as DNS names containerName = String.Format("media{0}{1}", DateTime.Now.Year, DateTime.Now.DayOfYear); } else { //We have received a path from a media file containerName = containerName.Substring(0, containerName.IndexOf("/")); } BlobContainer container = blobStorage.GetBlobContainer(containerName); //If the Container already exists, false is returned, so go get it if (!container.DoesContainerExist()) { container.CreateContainer(null, ContainerAccessControl.Private); } return(container); }
public WindowsAzureStorageHelper(string accountName, string accountKey, bool isLocal, string blobEndpointURI, string queueEndpointURI, string tableEndpointURI) { ContainerName = CONTAINER_NAME; StorageAccountInfo = new StorageAccountInfo(new Uri(blobEndpointURI), isLocal, accountName, accountKey); BlobStorageType = BlobStorage.Create(StorageAccountInfo); BlobStorageType.RetryPolicy = RetryPolicies.RetryN(2, TimeSpan.FromMilliseconds(100)); Container = BlobStorageType.GetBlobContainer(ContainerName); //Create the container if it does not exist. Container.CreateContainer(ContainerMetaData, ContainerAccessControl.Private); // Open queue storage. //StorageAccountInfo qaccount = StorageAccountInfo.GetDefaultQueueStorageAccountFromConfiguration(); StorageAccountInfo qaccount = new StorageAccountInfo(new Uri(queueEndpointURI), isLocal, accountName, accountKey); QueueStorageType = QueueStorage.Create(qaccount); QueueStorageType = QueueStorage.Create(qaccount); QueueStorageType.RetryPolicy = RetryPolicies.RetryN(2, TimeSpan.FromMilliseconds(100)); // Open table storage. //StorageAccountInfo taccount = StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration(); StorageAccountInfo taccount = new StorageAccountInfo(new Uri(tableEndpointURI), isLocal, accountName, accountKey); TableStorageType = TableStorage.Create(taccount); TableStorageType = TableStorage.Create(taccount); TableStorageType.RetryPolicy = RetryPolicies.RetryN(2, TimeSpan.FromMilliseconds(100)); }
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); }
// ********************************************************* // Enumerate all Blobs in a Specific Blob Container // ********************************************************* public IEnumerable <Object> ListBlobs(string ContainerName, string BlobEndPoint, string Account, string SharedKey) { StorageAccountInfo AccountInfo = new StorageAccountInfo(new Uri(BlobEndPoint), null, Account, SharedKey); var blobStore = BlobStorage.Create(AccountInfo); var container = blobStore.GetBlobContainer(ContainerName); return(container.ListBlobs(string.Empty, false)); }
// ************************************ // Get Blob Containers // ************************************ public IEnumerable <BlobContainer> ListContainers(string BlobEndPoint, string Account, string SharedKey) { StorageAccountInfo AccountInfo = new StorageAccountInfo(new Uri(BlobEndPoint), null, Account, SharedKey); var blobStore = BlobStorage.Create(AccountInfo); return(blobStore.ListBlobContainers()); }
// ************************** // Delete Blob Container // ************************** public void DeleteContainer(string ContainerName, string BlobEndPoint, string Account, string SharedKey) { StorageAccountInfo AccountInfo = new StorageAccountInfo(new Uri(BlobEndPoint), null, Account, SharedKey); var blobStore = BlobStorage.Create(AccountInfo); blobStore.GetBlobContainer(ContainerName).DeleteContainer(); }
public void Test_CanUploadRetrieveDeleteTextBlob() { var storage = new BlobStorage(account, _containerName); Assert.IsTrue(storage.Create()); const string dataContent = "data content"; Assert.IsTrue(storage.UploadText("test.txt", dataContent)); string content = storage.Download("test.txt"); Assert.AreEqual(dataContent, content); Assert.IsTrue(storage.Delete("test.txt")); Assert.IsTrue(storage.Delete()); }
public void Setup() { this.sourceOfTruthData = new List <TestItem>(); for (int i = 0; i < 10; i++) { this.sourceOfTruthData.Add(new TestItem(Guid.NewGuid(), DateTimeOffset.Now)); Thread.Sleep(1); } cache = BlobStorage <TestItem> .Create( StorageFactory.Blobs.InMemory(), new JsonSerializer <TestItem>()); }
// ************************************ // Create a new Blob Container // ************************************ public void CreateContainer(string ContainerName, bool isPublic, string BlobEndPoint, string Account, string SharedKey) { StorageAccountInfo AccountInfo = new StorageAccountInfo(new Uri(BlobEndPoint), null, Account, SharedKey); var blobStore = BlobStorage.Create(AccountInfo); ContainerAccessControl accessControl = isPublic ? ContainerAccessControl.Public : ContainerAccessControl.Private; blobStore.GetBlobContainer(ContainerName).CreateContainer(null, accessControl); }
// ********************************************************* // Delete a Blob in a Specific Blob Container // ********************************************************* public void DeleteBlob(string ContainerName, string BlobName, string BlobEndPoint, string Account, string SharedKey) { StorageAccountInfo AccountInfo = new StorageAccountInfo(new Uri(BlobEndPoint), null, Account, SharedKey); var blobStore = BlobStorage.Create(AccountInfo); var container = blobStore.GetBlobContainer(ContainerName); if (container.DoesBlobExist(BlobName)) { container.DeleteBlob(BlobName); } }
// ********************************************************* // Create a new Blob in a Specific Blob Container // ********************************************************* public void CreateBlob(string ContainerName, string FileName, string BlobEndPoint, string Account, string SharedKey) { StorageAccountInfo AccountInfo = new StorageAccountInfo(new Uri(BlobEndPoint), null, Account, SharedKey); var blobStore = BlobStorage.Create(AccountInfo); var container = blobStore.GetBlobContainer(ContainerName); BlobProperties props = new BlobProperties(System.IO.Path.GetFileName(FileName)); BlobContents blobContents = new BlobContents(new System.IO.FileStream(FileName, FileMode.Open)); container.CreateBlob(props, blobContents, true); }
public void Test_CanUploadRetrieveDeleteXMLBlob() { var storage = new BlobStorage(account, _containerName); Assert.IsTrue(storage.Create()); XDocument xml = new XDocument( new XComment("This is a comment"), new XElement("Root", new XElement("Child1", "data1"), new XElement("Child2", "data2"), new XElement("Child3", "data3"), new XElement("Child2", "data4"), new XElement("Info5", "info5"), new XElement("Info6", "info6"), new XElement("Info7", "info7"), new XElement("Info8", "info8") ) ); Assert.IsTrue(storage.UploadXml("test.xml", xml)); XDocument content = storage.DownloadXml("test.xml"); Assert.AreEqual(xml.ToString(), content.ToString()); Assert.IsTrue(storage.Delete("test.xml")); Assert.IsTrue(storage.Delete()); }
public async Task <string> ReadStringDataManual() { var emailEbayFactor = new EbayEmailFactory(); var orderList = emailEbayFactor.GetOrderInfoFromEbayEmail(); var updateToken = await Monzo.UpdateToken(); var token = updateToken.Value; var monzo = new MonzoClient(token); var getAccount = await monzo.GetAccountsAsync(); var accountId = getAccount[1].Id; var sinceDays = new PaginationOptions { SinceTime = DateTime.UtcNow.AddDays(-7) }; var transactions = await monzo.GetTransactionsAsync(accountId, "merchant", sinceDays); var blobStorage = new BlobStorage(); var _container = await blobStorage.Create(); foreach (var transaction in transactions) { if (transaction.Notes.Contains("#ebay") && !transaction.Notes.Contains("#automated")) { var findEmail = orderList.Where(x => x.Price == transaction.Amount && x.OrderDate.Date.DayOfYear >= transaction.Created.Date.DayOfYear); if (findEmail.Count() > 0) { var order = findEmail.First(); // Do Matching var newNote = new Dictionary <string, string>() { { "notes", $"{order.Name} \n #ebay #automated" } }; //upload to azure using (var client = new WebClient()) { client.DownloadFile(order.Image, "temp.jpg"); } var uploadedImageURL = ""; using (FileStream stream = new FileStream("temp.jpg", FileMode.Open, FileAccess.Read)) { var blockBlob = _container.GetBlockBlobReference($"{transaction.Id}-ebay.jpg"); blockBlob.Properties.ContentType = "image/jpeg"; stream.Seek(0, SeekOrigin.Begin); await blockBlob.UploadFromStreamAsync(stream); uploadedImageURL = blockBlob.StorageUri.PrimaryUri.ToString(); } await monzo.CreateAttachmentAsync(transaction.Id, uploadedImageURL, "image/jpeg"); await monzo.AnnotateTransactionAsync(transaction.Id, newNote); } } } return("ok"); }
public void Test_CanUploadRetrieveDeleteBinaryBlob() { var storage = new BlobStorage(account, _containerName); Assert.IsTrue(storage.Create()); byte[] dataContent = new byte[] { 1, 2, 3 }; const string fileName = "test.bin"; Assert.IsTrue(storage.UploadBinary(fileName, dataContent, "binary/binary")); byte[] content = storage.DownloadBinary(fileName); for (int i = 0; i < 3; i++) { Assert.AreEqual(dataContent[i], content[i]); } Assert.IsTrue(storage.Delete(fileName)); Assert.IsTrue(storage.Delete()); }
public void Test_CanCreateAndDeleteContainer() { var storage = new BlobStorage(account, _containerName); Assert.IsTrue(storage.Create()); Assert.IsTrue(storage.Delete()); }
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); } }