public static async Task <string> UploadFileToStorage(Stream fileStream, string fileName, TypeTable typeTable) { try { // Create storagecredentials object by reading the values from the configuration (appsettings.json) StorageCredentials storageCredentials = new StorageCredentials(accountName: CloudStorage.Instance.GetAccountCloudStorage(typeTable).AccountName, CloudStorage.Instance.GetAccountCloudStorage(typeTable).AccountKey); // Create cloudstorage account by passing the storagecredentials CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true); // Create the blob client. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Get reference to the blob container by passing the name by reading the value from the configuration (appsettings.json) CloudBlobContainer container = blobClient.GetContainerReference(CloudStorage.Instance.GetAccountCloudStorage(typeTable).Container); container.CreateIfNotExists(); // Get the reference to the block blob from the container CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName); string ext = System.IO.Path.GetExtension(fileName).ToLower(); blockBlob.Properties.ContentType = MimeTypes.GetMimeType(ext); // Upload the file await blockBlob.UploadFromStreamAsync(fileStream); string sas = blockBlob.GetSharedAccessSignature( new SharedAccessBlobPolicy() { Permissions = SharedAccessBlobPermissions.Read, SharedAccessStartTime = DateTime.Now.AddMinutes(-5), SharedAccessExpiryTime = DateTime.Now.AddYears(999) }); var url = blockBlob.Uri + sas; return(url); } catch (Exception e) { Console.WriteLine(e); throw; } }
public void BlobIngressEgressCounters() { CloudBlobContainer container = GetRandomContainerReference(); container.CreateIfNotExists(); CloudBlockBlob blob = container.GetBlockBlobReference("blob1"); string[] blockIds = new string[] { Convert.ToBase64String(Guid.NewGuid().ToByteArray()), Convert.ToBase64String(Guid.NewGuid().ToByteArray()), Convert.ToBase64String(Guid.NewGuid().ToByteArray()) }; try { // 1 byte TestHelper.ValidateIngressEgress(Selectors.IfUrlContains(blob.Uri.ToString()), () => { OperationContext opContext = new OperationContext(); blob.PutBlock(blockIds[0], new MemoryStream(GetRandomBuffer(1)), null, null, new BlobRequestOptions() { RetryPolicy = new RetryPolicies.NoRetry() }, opContext); return(opContext.LastResult); }); // 1024 TestHelper.ValidateIngressEgress(Selectors.IfUrlContains(blob.Uri.ToString()), () => { OperationContext opContext = new OperationContext(); blob.PutBlock(blockIds[1], new MemoryStream(GetRandomBuffer(1024)), null, null, new BlobRequestOptions() { RetryPolicy = new RetryPolicies.NoRetry() }, opContext); return(opContext.LastResult); }); // 98765 TestHelper.ValidateIngressEgress(Selectors.IfUrlContains(blob.Uri.ToString()), () => { OperationContext opContext = new OperationContext(); blob.PutBlock(blockIds[2], new MemoryStream(GetRandomBuffer(98765)), null, null, new BlobRequestOptions() { RetryPolicy = new RetryPolicies.NoRetry() }, opContext); return(opContext.LastResult); }); // PutBlockList TestHelper.ValidateIngressEgress(Selectors.IfUrlContains(blob.Uri.ToString()), () => { OperationContext opContext = new OperationContext(); blob.PutBlockList(blockIds, null, new BlobRequestOptions() { RetryPolicy = new RetryPolicies.NoRetry() }, opContext); return(opContext.LastResult); }); // GetBlockList TestHelper.ValidateIngressEgress(Selectors.IfUrlContains(blob.Uri.ToString()), () => { OperationContext opContext = new OperationContext(); blob.DownloadBlockList(BlockListingFilter.All, null, new BlobRequestOptions() { RetryPolicy = new RetryPolicies.NoRetry() }, opContext); return(opContext.LastResult); }); // Download TestHelper.ValidateIngressEgress(Selectors.IfUrlContains(blob.Uri.ToString()), () => { OperationContext opContext = new OperationContext(); blob.DownloadToStream(Stream.Null, null, new BlobRequestOptions() { RetryPolicy = new RetryPolicies.NoRetry() }, opContext); return(opContext.LastResult); }); Assert.AreEqual(blob.Properties.Length, 98765 + 1024 + 1); // Error Case CloudBlockBlob nullBlob = container.GetBlockBlobReference("null"); OperationContext errorContext = new OperationContext(); try { nullBlob.DownloadToStream(Stream.Null, null, new BlobRequestOptions() { RetryPolicy = new RetryPolicies.NoRetry() }, errorContext); Assert.Fail("Null blob, null stream, no download possible."); } catch (StorageException) { Assert.IsTrue(errorContext.LastResult.IngressBytes > 0); } } finally { container.DeleteIfExists(); } }