/// <summary> /// The main entry point. /// </summary> /// <param name="args"> /// The command line arguments. Not used. /// </param> private static void Main(string[] args) { var credentials = new StorageCredentials("rwwa", "vyMfaSPxURHXZaIhhFJQRg5ZLEN6qDj4yU78r3oeOH+pZzdcf4S86QvGAsB6L8JaPti9qJbB929hy1Y9hipFmw=="); // Retrieve storage account from connection string. var storageAccount = new CloudStorageAccount(credentials, true); CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient(); // Retrieve a reference to a queue CloudQueue queue = queueClient.GetQueueReference("dataqueue"); queue.CreateIfNotExists(); Console.WriteLine("Up and listening to the queue."); while (true) { CloudQueueMessage message; do { message = queue.GetMessage(); if (message != null) { Console.WriteLine("Received Message for BLOB " + message.AsString); var blobUrl = message.AsString; var blockBlob = new CloudBlockBlob(new Uri(blobUrl), credentials); if (blockBlob.Exists()) { // TODO: download and process BLOB /*using (var fileStream = System.IO.File.OpenWrite(@"path\myfile")) { blockBlob.DownloadToStream(fileStream); }*/ blockBlob.Delete(); } queue.DeleteMessage(message); Console.WriteLine("BLOB " + message.AsString + " and queue message deleted."); } } while (message != null); Thread.Sleep(TimeSpan.FromSeconds(5)); } }
/// <summary> /// The main entry point. /// </summary> /// <param name="args"> /// The command line arguments. Not used. /// </param> private static void Main(string[] args) { var credentials = new StorageCredentials("[Enter your account name]", "[Enter your account key]"); // Retrieve storage account from connection string. var storageAccount = new CloudStorageAccount(credentials, true); CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient(); // Retrieve a reference to a queue CloudQueue queue = queueClient.GetQueueReference("dataqueue"); Console.WriteLine("Up and listening to the queue."); while (true) { CloudQueueMessage message; do { message = queue.GetMessage(); if (message != null) { Console.WriteLine("Received Message for BLOB " + message.AsString); var blobUrl = message.AsString; var blockBlob = new CloudBlockBlob(new Uri(blobUrl), credentials); if (blockBlob.Exists()) { // TODO: download and process BLOB /*using (var fileStream = System.IO.File.OpenWrite(@"path\myfile")) { blockBlob.DownloadToStream(fileStream); }*/ blockBlob.Delete(); } queue.DeleteMessage(message); Console.WriteLine("BLOB " + message.AsString + " and queue message deleted."); } } while (message != null); Thread.Sleep(TimeSpan.FromSeconds(5)); } }
//Delete a blob file public void DeleteBlob(string UserId, string BlobName) { Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = GetCloudBlobContainer(UserId); Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName); if (blockBlob.Exists()) { blockBlob.Delete(); } }
public async Task LargeDownloadVerifyRangeSizeRestrictions() { string inputFileName = Path.GetTempFileName(); string outputFileName = Path.GetTempFileName(); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob blob = container.GetBlockBlobReference("largeblob1"); await blob.UploadTextAsync("Tent"); try { await blob.DownloadToFileParallelAsync(outputFileName, FileMode.Create, 16, 16 *Constants.MB + 3, CancellationToken.None); Assert.Fail("Expected a failure"); } catch (ArgumentException) {} try { await blob.DownloadToFileParallelAsync(outputFileName, FileMode.Create, 16, 2 *Constants.MB, CancellationToken.None); Assert.Fail("Expected a failure"); } catch (ArgumentOutOfRangeException) {} try { BlobRequestOptions options = new BlobRequestOptions(); options.UseTransactionalMD5 = true; await blob.DownloadToFileParallelAsync(outputFileName, FileMode.Create, 16, 16 *Constants.MB, 0, null, null, options, null, CancellationToken.None); Assert.Fail("Expected a failure"); } catch (ArgumentException) {} blob.Delete(); } finally { container.DeleteIfExists(); File.Delete(inputFileName); File.Delete(outputFileName); } }
public void CloudBlockBlobCopyTestAPM() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudBlockBlob source = container.GetBlockBlobReference("source"); string data = "String data"; UploadText(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; source.SetMetadata(); CloudBlockBlob copy = container.GetBlockBlobReference("copy"); using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { IAsyncResult result = copy.BeginStartCopyFromBlob(TestHelper.Defiddler(source), ar => waitHandle.Set(), null); waitHandle.WaitOne(); string copyId = copy.EndStartCopyFromBlob(result); WaitForCopy(copy); Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status); Assert.AreEqual(source.Uri.AbsolutePath, copy.CopyState.Source.AbsolutePath); Assert.AreEqual(data.Length, copy.CopyState.TotalBytes); Assert.AreEqual(data.Length, copy.CopyState.BytesCopied); Assert.AreEqual(copyId, copy.CopyState.CopyId); Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); result = copy.BeginAbortCopy(copyId, ar => waitHandle.Set(), null); waitHandle.WaitOne(); TestHelper.ExpectedException( () => copy.EndAbortCopy(result), "Aborting a copy operation after completion should fail", HttpStatusCode.Conflict, "NoPendingCopyOperation"); } source.FetchAttributes(); Assert.IsNotNull(copy.Properties.ETag); Assert.AreNotEqual(source.Properties.ETag, copy.Properties.ETag); Assert.IsTrue(copy.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); string copyData = DownloadText(copy, Encoding.UTF8); Assert.AreEqual(data, copyData, "Data inside copy of blob not similar"); copy.FetchAttributes(); BlobProperties prop1 = copy.Properties; BlobProperties prop2 = source.Properties; Assert.AreEqual(prop1.CacheControl, prop2.CacheControl); Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding); Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage); Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5); Assert.AreEqual(prop1.ContentType, prop2.ContentType); Assert.AreEqual("value", copy.Metadata["Test"], false, "Copied metadata not same"); copy.Delete(); } finally { container.DeleteIfExists(); } }
/// <summary> /// Test blob writing, expecting success. /// </summary> /// <param name="testBlob">The blob to test.</param> /// <param name="sourceBlob">A blob to use as the source of a copy.</param> /// <param name="testAccessCondition">The access condition to use.</param> private void BlobWriteExpectLeaseSuccess(CloudBlockBlob testBlob, CloudBlob sourceBlob, AccessCondition testAccessCondition) { testBlob.SetMetadata(testAccessCondition, null /* options */); testBlob.SetProperties(testAccessCondition, null /* options */); UploadText(testBlob, "No Problem", Encoding.UTF8, testAccessCondition, null /* options */); testBlob.StartCopy(TestHelper.Defiddler(sourceBlob.Uri), null /* source access condition */, testAccessCondition, null /* options */); while (testBlob.CopyState.Status == CopyStatus.Pending) { Thread.Sleep(1000); testBlob.FetchAttributes(); } Stream stream = testBlob.OpenWrite(testAccessCondition, null /* options */); stream.WriteByte(0); stream.Flush(); testBlob.Delete(DeleteSnapshotsOption.None, testAccessCondition, null /* options */); }
/// <summary> /// Test blob write and creation, expecting lease failure. /// </summary> /// <param name="testBlob">The blob to test.</param> /// <param name="sourceBlob">A blob to use as the source of a copy.</param> /// <param name="testAccessCondition">The failing access condition to use.</param> /// <param name="expectedErrorCode">The expected error code.</param> /// <param name="description">The reason why these calls should fail.</param> private void BlobWriteExpectLeaseFailure(CloudBlockBlob testBlob, CloudBlockBlob sourceBlob, AccessCondition testAccessCondition, HttpStatusCode expectedStatusCode, string expectedErrorCode, string description) { this.BlobCreateExpectLeaseFailure(testBlob, sourceBlob, testAccessCondition, expectedStatusCode, expectedErrorCode, description); TestHelper.ExpectedException( () => testBlob.SetMetadata(testAccessCondition, null /* options */), description + " (Set Metadata)", expectedStatusCode, expectedErrorCode); TestHelper.ExpectedException( () => testBlob.SetProperties(testAccessCondition, null /* options */), description + " (Set Properties)", expectedStatusCode, expectedErrorCode); TestHelper.ExpectedException( () => testBlob.Delete(DeleteSnapshotsOption.None, testAccessCondition, null /* options */), description + " (Delete)", expectedStatusCode, expectedErrorCode); }
/// <summary> /// Process sample Files for generating test data /// </summary> /// <param name="path"></param> /// <param name="outputStorageAccount"></param> /// <param name="folderPath"></param> /// <param name="outputTableName"></param> /// <param name="logger"></param> public static void ProcessFiles(string path, CloudStorageAccount outputStorageAccount, string folderPath, string outputTableName, IActivityLogger logger) { string[] files = Directory.GetFiles(path); foreach (var file in files) { if (file.Contains(@"artist_data.txt") && outputTableName.ToLower().Contains(@"catalog")) { Uri outputBlobUri = new Uri(outputStorageAccount.BlobEndpoint, folderPath + "artist_data.txt"); CloudBlockBlob outputBlob = new CloudBlockBlob(outputBlobUri, outputStorageAccount.Credentials); if (outputBlob.Exists()) { outputBlob.Delete(); } outputBlob.UploadFromFile(path + "/artist_data.txt", FileMode.Open); } if (file.Contains(@"customers.txt") && outputTableName.ToLower().Contains(@"customers")) { Uri outputBlobUri = new Uri(outputStorageAccount.BlobEndpoint, folderPath + "customers.txt"); CloudBlockBlob outputBlob = new CloudBlockBlob(outputBlobUri, outputStorageAccount.Credentials); if (outputBlob.Exists()) { outputBlob.Delete(); } outputBlob.UploadFromFile(path + "/customers.txt", FileMode.Open); } else if (file.Contains(@"artist_customer_data.txt") && outputTableName.ToLower().Contains(@"rawproducts")) { Uri outputBlobUri = new Uri(outputStorageAccount.BlobEndpoint, folderPath + "artist_customer_data.txt"); CloudBlockBlob outputBlob = new CloudBlockBlob(outputBlobUri, outputStorageAccount.Credentials); List<string> lines = File.ReadAllLines(path + "/artist_customer_data.txt").ToList(); int index = 0; if (outputBlob.Exists() && index == 0) { outputBlob.Delete(); } //add new column value for each row. lines.Skip(0).ToList().ForEach(line => { if (index >= 0 & index <= 1000) { lines[index] += "," + DateTime.UtcNow.AddMonths(-1).ToString("yyyy-MM-dd"); UploadBlobStream(outputBlob, lines[index]); index++; } else if (index >= 1001 & index <= 2000) { lines[index] += "," + DateTime.UtcNow.AddMonths(-2).ToString("yyyy-MM-dd"); UploadBlobStream(outputBlob, lines[index]); index++; } else if (index >= 2001 & index <= 3000) { lines[index] += "," + DateTime.UtcNow.AddMonths(-3).ToString("yyyy-MM-dd"); UploadBlobStream(outputBlob, lines[index]); index++; } else if (index >= 3001 & index <= 4000) { lines[index] += "," + DateTime.UtcNow.AddMonths(-4).ToString("yyyy-MM-dd"); UploadBlobStream(outputBlob, lines[index]); index++; } else if (index >= 4001 & index <= 5000) { lines[index] += "," + DateTime.UtcNow.AddMonths(-5).ToString("yyyy-MM-dd"); UploadBlobStream(outputBlob, lines[index]); index++; } else if (index >= 5001 & index <= 6000) { lines[index] += "," + DateTime.UtcNow.AddMonths(-6).ToString("yyyy-MM-dd"); UploadBlobStream(outputBlob, lines[index]); index++; } else if (index >= 6001 & index <= 7000) { lines[index] += "," + DateTime.UtcNow.AddMonths(-7).ToString("yyyy-MM-dd"); UploadBlobStream(outputBlob, lines[index]); index++; } else if (index >= 7001 & index <= 8000) { lines[index] += "," + DateTime.UtcNow.AddMonths(-8).ToString("yyyy-MM-dd"); UploadBlobStream(outputBlob, lines[index]); index++; } else if (index >= 8001 & index <= 9000) { lines[index] += "," + DateTime.UtcNow.AddMonths(-9).ToString("yyyy-MM-dd"); UploadBlobStream(outputBlob, lines[index]); index++; } else if (index >= 9001 & index <= 10000) { lines[index] += "," + DateTime.UtcNow.AddMonths(-10).ToString("yyyy-MM-dd"); UploadBlobStream(outputBlob, lines[index]); index++; } else if (index >= 10001 & index <= 11000) { lines[index] += "," + DateTime.UtcNow.AddMonths(-11).ToString("yyyy-MM-dd"); UploadBlobStream(outputBlob, lines[index]); index++; } else if (index >= 11001 & index <= 12000) { lines[index] += "," + DateTime.UtcNow.AddMonths(-12).ToString("yyyy-MM-dd"); UploadBlobStream(outputBlob, lines[index]); index++; } Console.WriteLine("Writing blob number: {0}", index); logger.Write(TraceEventType.Information, "Writing blob number: {0}", index); }); } } }
private static void CloudBlockBlobCopy(bool sourceIsSas, bool destinationIsSas) { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); // Create Source on server CloudBlockBlob source = container.GetBlockBlobReference("source"); string data = "String data"; UploadText(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; source.SetMetadata(); // Create Destination on server CloudBlockBlob destination = container.GetBlockBlobReference("destination"); destination.PutBlockList(new string[] { }); CloudBlockBlob copySource = source; CloudBlockBlob copyDestination = destination; if (sourceIsSas) { // Source SAS must have read permissions SharedAccessBlobPermissions permissions = SharedAccessBlobPermissions.Read; SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), Permissions = permissions, }; string sasToken = source.GetSharedAccessSignature(policy); // Get source StorageCredentials credentials = new StorageCredentials(sasToken); copySource = new CloudBlockBlob(credentials.TransformUri(source.Uri)); } if (destinationIsSas) { if (!sourceIsSas) { // Source container must be public if source is not SAS BlobContainerPermissions containerPermissions = new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }; container.SetPermissions(containerPermissions); } // Destination SAS must have write permissions SharedAccessBlobPermissions permissions = SharedAccessBlobPermissions.Write; SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), Permissions = permissions, }; string sasToken = destination.GetSharedAccessSignature(policy); // Get destination StorageCredentials credentials = new StorageCredentials(sasToken); copyDestination = new CloudBlockBlob(credentials.TransformUri(destination.Uri)); } // Start copy and wait for completion string copyId = copyDestination.StartCopyFromBlob(TestHelper.Defiddler(copySource)); WaitForCopy(destination); // Check original blob references for equality Assert.AreEqual(CopyStatus.Success, destination.CopyState.Status); Assert.AreEqual(source.Uri.AbsolutePath, destination.CopyState.Source.AbsolutePath); Assert.AreEqual(data.Length, destination.CopyState.TotalBytes); Assert.AreEqual(data.Length, destination.CopyState.BytesCopied); Assert.AreEqual(copyId, destination.CopyState.CopyId); Assert.IsTrue(destination.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); if (!destinationIsSas) { // Abort Copy is not supported for SAS destination TestHelper.ExpectedException( () => copyDestination.AbortCopy(copyId), "Aborting a copy operation after completion should fail", HttpStatusCode.Conflict, "NoPendingCopyOperation"); } source.FetchAttributes(); Assert.IsNotNull(destination.Properties.ETag); Assert.AreNotEqual(source.Properties.ETag, destination.Properties.ETag); Assert.IsTrue(destination.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); string copyData = DownloadText(destination, Encoding.UTF8); Assert.AreEqual(data, copyData, "Data inside copy of blob not equal."); destination.FetchAttributes(); BlobProperties prop1 = destination.Properties; BlobProperties prop2 = source.Properties; Assert.AreEqual(prop1.CacheControl, prop2.CacheControl); Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding); Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage); Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5); Assert.AreEqual(prop1.ContentType, prop2.ContentType); Assert.AreEqual("value", destination.Metadata["Test"], false, "Copied metadata not same"); destination.Delete(); source.Delete(); } finally { container.DeleteIfExists(); } }
public ActionResult DeleteContent(int idContent) { Content content = db.Contents.Where(c => c.ID_CO == idContent).Single(); List<ContentData> _contentDatas = db.ContentDatas.Where(c => c.ID_CO == idContent).ToList(); foreach(ContentData cd in _contentDatas) // Удаление из storage { CloudBlockBlob blob = new CloudBlockBlob(new Uri(cd.URL)); blob.Delete(); } db.Contents.Remove(content); db.SaveChanges(); List<ContentView> contentViews = new List<ContentView>(); int userId = getUserId(); List<Content> contents = db.Contents.Where(c => c.ID_USER == userId).ToList(); foreach (Content _content in contents) { List<ContentData> contentDatas = db.ContentDatas.Where(cd => cd.ID_CO == _content.ID_CO).ToList(); contentViews.Add(new ContentView(_content.CONTENT_TEXT, _content.CONTENT_TITLE, contentDatas.ToArray(), _content.ID_CO)); } return RedirectToAction("Index"); }
static void UseBlobSAS(string sas) { //Try performing blob operations using the SAS provided. //Return a reference to the blob using the SAS URI. CloudBlockBlob blob = new CloudBlockBlob(new Uri(sas)); //Write operation: Write a new blob to the container. try { string blobContent = "This blob was created with a shared access signature granting write permissions to the blob. "; MemoryStream msWrite = new MemoryStream(Encoding.UTF8.GetBytes(blobContent)); msWrite.Position = 0; using (msWrite) { blob.UploadFromStream(msWrite); } Console.WriteLine("Write operation succeeded for SAS " + sas); Console.WriteLine(); } catch (StorageException e) { Console.WriteLine("Write operation failed for SAS " + sas); Console.WriteLine("Additional error information: " + e.Message); Console.WriteLine(); } //Read operation: Read the contents of the blob. try { MemoryStream msRead = new MemoryStream(); using (msRead) { blob.DownloadToStream(msRead); msRead.Position = 0; using (StreamReader reader = new StreamReader(msRead, true)) { string line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } } } Console.WriteLine("Read operation succeeded for SAS " + sas); Console.WriteLine(); } catch (StorageException e) { Console.WriteLine("Read operation failed for SAS " + sas); Console.WriteLine("Additional error information: " + e.Message); Console.WriteLine(); } //Delete operation: Delete the blob. try { blob.Delete(); Console.WriteLine("Delete operation succeeded for SAS " + sas); Console.WriteLine(); } catch (StorageException e) { Console.WriteLine("Delete operation failed for SAS " + sas); Console.WriteLine("Additional error information: " + e.Message); Console.WriteLine(); } }