public async Task SaveImage_GetImage_DeleteFile() { try { var notifier = new ProgressCancelNotifier(); string connectionString = ConfigurationManager.AppSettings["AzureStorageConnectionString"]; string containerName = Guid.NewGuid().ToString(); var blobContainer = new BlobContainer(notifier, false); await blobContainer.CreateContainerAsync(connectionString, containerName, BlobContainerPublicAccessEnum.Off); var theImage = Image.FromFile(ImagePath1); theImage = theImage.ScaleImage(500, 500); string fileName = "imagetest.jpeg"; string fileName2 = "imagetest2.jpeg"; string fakeName = "imagetest241.jpeg"; //save if (await blobContainer.SaveFileAsync(connectionString, containerName, theImage.ToStream(), fileName) == null) { throw new Exception(); } if (await blobContainer.SaveFileAsync(connectionString, containerName, theImage.ToStream(), fileName2) == null) { throw new Exception(); } //get var getImage = await blobContainer.GetFileAsync(connectionString, containerName, fileName); if (getImage == null) { throw new Exception(); } var getImage2 = await blobContainer.GetFileAsync(connectionString, containerName, fakeName); if (getImage2 != null) { throw new Exception(); } //delete if (!await blobContainer.DeleteFileAsync(connectionString, containerName, fileName)) { throw new Exception(); } if (!await blobContainer.DeleteFileAsync(connectionString, containerName, fileName2)) { throw new Exception(); } await blobContainer.DeleteContainerAsync(connectionString, containerName); } catch (Exception) { throw; } }
public async Task CreateContainer() { var notifier = new ProgressCancelNotifier(); string connectionString = ConfigurationManager.AppSettings["AzureStorageConnectionString"]; string privateContainer = Guid.NewGuid().ToString(); string publicContainer = Guid.NewGuid().ToString(); var blobContainer = new BlobContainer(notifier, false); await blobContainer.CreateContainerAsync(connectionString, privateContainer, BlobContainerPublicAccessEnum.Off); var theImage = Image.FromFile(ImagePath1); Uri thePrivateUrl = await blobContainer.SaveFileAsync(connectionString, privateContainer, theImage.ToStream(), Guid.NewGuid().ToString() + ".jpg"); try { using (var webClient = new WebClient()) { Stream stream = webClient.OpenRead(thePrivateUrl); Image.FromStream(stream); throw new ArgumentException(); } } catch (ArgumentException) { throw; } catch (Exception) { } await blobContainer.CreateContainerAsync(connectionString, publicContainer, BlobContainerPublicAccessEnum.Blob); var theImage2 = Image.FromFile(ImagePath1); Uri thePublicUrl = await blobContainer.SaveFileAsync(connectionString, publicContainer, theImage.ToStream(), Guid.NewGuid().ToString() + ".jpg"); using (var webClient = new WebClient()) { Stream stream = webClient.OpenRead(thePublicUrl); var img = Image.FromStream(stream); if (img == null) { throw new Exception(); } } }
public async Task SavePDF_GetImage_DeleteFile() { var notifier = new ProgressCancelNotifier(); string connectionString = ConfigurationManager.AppSettings["AzureStorageConnectionString"]; string containerName = Guid.NewGuid().ToString(); var blobContainer = new BlobContainer(notifier, false); await blobContainer.CreateContainerAsync(connectionString, containerName, BlobContainerPublicAccessEnum.Off); var theFile = File.ReadAllBytes(pdfPath); string fileName = "pdf1.pdf"; string fileName2 = "pdf2.pdf"; string fakeName = "pdf1001.pdf"; //save if (await blobContainer.SaveFileAsync(connectionString, containerName, theFile, fileName) == null) { throw new Exception(); } if (await blobContainer.SaveFileAsync(connectionString, containerName, theFile, fileName2) == null) { throw new Exception(); } //get var getImage = await blobContainer.GetFileAsync(connectionString, containerName, fileName); if (getImage == null) { throw new Exception(); } var getImage2 = await blobContainer.GetFileAsync(connectionString, containerName, fakeName); if (getImage2 != null) { throw new Exception(); } //delete if (!await blobContainer.DeleteFileAsync(connectionString, containerName, fileName)) { throw new Exception(); } if (!await blobContainer.DeleteFileAsync(connectionString, containerName, fileName2)) { throw new Exception(); } await blobContainer.DeleteContainerAsync(connectionString, containerName); }