How to use CreateOrUpdateBlobAsync method of PetImages.Storage.BlobContainer class

Best Coyote code snippet using PetImages.Storage.BlobContainer.CreateOrUpdateBlobAsync

ImageController.cs

Source:ImageController.cs Github

copy

Full Screen

...38 }39 var imageItem = image.ToItem();40 // We upload the image to Azure Storage, before adding an entry to Cosmos DB41 // so that it is guaranteed to be there when user does a GET request.42 // Note: we're calling CreateOrUpdateBlobAsync because Azure Storage doesn't43 // have a create-only API.44 await this.BlobContainer.CreateContainerIfNotExistsAsync(accountName);45 await this.BlobContainer.CreateOrUpdateBlobAsync(accountName, image.Name, image.Content);46 try47 {48 imageItem = await this.ImageContainer.CreateItem(imageItem);49 }50 catch (DatabaseItemAlreadyExistsException)51 {52 return this.Conflict();53 }54 catch (DatabaseException)55 {56 // We handle an exception thrown by Cosmos DB layer, perhaps due to some57 // intermittent failure, by cleaning up the image to not waste resources.58 await this.BlobContainer.DeleteBlobIfExistsAsync(accountName, image.Name);59 return this.StatusCode(503);60 }61 return this.Ok(imageItem.ToImage());62 }63 /// <summary>64 /// Scenario 2 - Fixed CreateImageAsync version.65 /// </summary>66 [HttpPost]67 public async Task<ActionResult<Image>> CreateImageAsyncFixed(string accountName, Image image)68 {69 if (!await StorageHelper.DoesItemExist<AccountItem>(this.AccountContainer, partitionKey: accountName, id: accountName))70 {71 return this.NotFound();72 }73 var imageItem = image.ToItem();74 await this.BlobContainer.CreateContainerIfNotExistsAsync(accountName);75 await this.BlobContainer.CreateOrUpdateBlobAsync(accountName, image.Name, image.Content);76 try77 {78 imageItem = await this.ImageContainer.CreateItem(imageItem);79 }80 catch (DatabaseItemAlreadyExistsException)81 {82 return this.Conflict();83 }84 // We don't delete the blob in the controller; orphaned blobs (i.e., blobs with no corresponding85 // Cosmos DB entry) are cleaned up asynchronously by a background "garbage collector" worker86 // (not shown in this sample).87 return this.Ok(imageItem.ToImage());88 }89 [HttpGet]90 public async Task<ActionResult<byte[]>> GetImageContentsAsync(string accountName, string imageName)91 {92 if (!await StorageHelper.DoesItemExist<AccountItem>(this.AccountContainer, partitionKey: accountName, id: accountName))93 {94 return this.NotFound();95 }96 ImageItem imageItem;97 try98 {99 imageItem = await this.ImageContainer.GetItem<ImageItem>(partitionKey: imageName, id: imageName);100 }101 catch (DatabaseItemDoesNotExistException)102 {103 return this.NotFound();104 }105 if (!await this.BlobContainer.ExistsBlobAsync(accountName, imageItem.StorageName))106 {107 return this.NotFound();108 }109 return this.Ok(await this.BlobContainer.GetBlobAsync(accountName, imageItem.StorageName));110 }111 [HttpGet]112 public async Task<ActionResult<byte[]>> GetImageThumbnailAsync(string accountName, string imageName)113 {114 if (!await StorageHelper.DoesItemExist<AccountItem>(this.AccountContainer, partitionKey: accountName, id: accountName))115 {116 return this.NotFound();117 }118 ImageItem imageItem;119 try120 {121 imageItem = await this.ImageContainer.GetItem<ImageItem>(partitionKey: imageName, id: imageName);122 }123 catch (DatabaseItemDoesNotExistException)124 {125 return this.NotFound();126 }127 var containerName = accountName + Constants.ThumbnailContainerNameSuffix;128 var blobName = imageItem.StorageName + Constants.ThumbnailSuffix;129 if (!await this.BlobContainer.ExistsBlobAsync(containerName, blobName))130 {131 return this.NotFound();132 }133 return this.Ok(await this.BlobContainer.GetBlobAsync(containerName, blobName));134 }135 /// <summary>136 /// Scenario 3 - Buggy CreateOrUpdateImageAsync version.137 /// </summary>138 [HttpPut]139 public async Task<ActionResult<Image>> CreateOrUpdateImageAsync(string accountName, Image image)140 {141 if (!await StorageHelper.DoesItemExist<AccountItem>(this.AccountContainer, partitionKey: accountName, id: accountName))142 {143 return this.NotFound();144 }145 var imageItem = image.ToItem();146 await this.BlobContainer.CreateContainerIfNotExistsAsync(accountName);147 await this.BlobContainer.CreateOrUpdateBlobAsync(accountName, image.Name, image.Content);148 imageItem = await this.ImageContainer.UpsertItem(imageItem);149 await this.MessagingClient.SubmitMessage(new GenerateThumbnailMessage()150 {151 AccountName = accountName,152 ImageStorageName = image.Name153 });154 return this.Ok(imageItem.ToImage());155 }156 /// <summary>157 /// Scenario 3 - Fixed CreateOrUpdateImageAsync version.158 /// </summary>159 [HttpPut]160 public async Task<ActionResult<Image>> CreateOrUpdateImageAsyncFixed(string accountName, Image image)161 {162 if (!await StorageHelper.DoesItemExist<AccountItem>(this.AccountContainer, partitionKey: accountName, id: accountName))163 {164 return this.NotFound();165 }166 var imageItem = image.ToItem();167 var uniqueId = Guid.NewGuid().ToString();168 imageItem.StorageName = uniqueId;169 await this.BlobContainer.CreateContainerIfNotExistsAsync(accountName);170 await this.BlobContainer.CreateOrUpdateBlobAsync(accountName, imageItem.StorageName, image.Content);171 imageItem = await this.ImageContainer.UpsertItem(imageItem);172 await this.MessagingClient.SubmitMessage(new GenerateThumbnailMessage()173 {174 AccountName = accountName,175 ImageStorageName = imageItem.StorageName176 });177 return this.Ok(imageItem.ToImage());178 }179 }180}...

Full Screen

Full Screen

GenerateThumbnailWorker.cs

Source:GenerateThumbnailWorker.cs Github

copy

Full Screen

...22 var thumbnail = GenerateThumbnail(imageContents);23 var containerName = accountName + Constants.ThumbnailContainerNameSuffix;24 var blobName = imageStorageName + Constants.ThumbnailSuffix;25 await this.BlobContainer.CreateContainerIfNotExistsAsync(containerName);26 await this.BlobContainer.CreateOrUpdateBlobAsync(containerName, blobName, thumbnail);27 }28 /// <summary>29 /// Dummy implementation of GenerateThumbnail that returns the same bytes as the image.30 /// </summary>31 private static byte[] GenerateThumbnail(byte[] imageContents) => imageContents;32 }33}...

Full Screen

Full Screen

CreateOrUpdateBlobAsync

Using AI Code Generation

copy

Full Screen

1using System;2using System.IO;3using System.Threading.Tasks;4using Microsoft.WindowsAzure.Storage;5using Microsoft.WindowsAzure.Storage.Blob;6{7 {8 private readonly CloudBlobContainer _container;9 public BlobContainer(string connectionString, string containerName)10 {11 var storageAccount = CloudStorageAccount.Parse(connectionString);12 var blobClient = storageAccount.CreateCloudBlobClient();13 _container = blobClient.GetContainerReference(containerName);14 }15 public async Task CreateBlobContainerAsync()16 {17 await _container.CreateIfNotExistsAsync();18 await _container.SetPermissionsAsync(19 new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }20 );21 }22 public async Task CreateOrUpdateBlobAsync(string blobName, Stream stream)23 {24 var blockBlob = _container.GetBlockBlobReference(blobName);25 await blockBlob.UploadFromStreamAsync(stream);26 }27 }28}29using System.IO;30using System.Threading.Tasks;31using Microsoft.AspNetCore.Http;32using Microsoft.AspNetCore.Mvc;33using PetImages.Storage;34{35 [Route("api/[controller]")]36 {37 private readonly BlobContainer _blobContainer;38 public PetImagesController(BlobContainer blobContainer)39 {40 _blobContainer = blobContainer;41 }42 public async Task<IActionResult> Post(IFormFile file)43 {44 var stream = file.OpenReadStream();45 var fileName = file.FileName;46 await _blobContainer.CreateOrUpdateBlobAsync(fileName, stream);47 return Ok();48 }49 }50}51using Microsoft.AspNetCore.Builder;52using Microsoft.AspNetCore.Hosting;53using Microsoft.Extensions.Configuration;54using Microsoft.Extensions.DependencyInjection;55using Microsoft.Extensions.Hosting;56using PetImages.Storage;57{58 {59 public Startup(IConfiguration configuration)60 {61 Configuration = configuration;62 }63 public IConfiguration Configuration { get; }64 public void ConfigureServices(IServiceCollection services)65 {66 services.AddControllers();67 services.AddSingleton(new BlobContainer(Configuration["StorageConnectionString"], "pet-images"));68 }69 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)70 {71 if (

Full Screen

Full Screen

CreateOrUpdateBlobAsync

Using AI Code Generation

copy

Full Screen

1public async Task CreateOrUpdateBlobAsync(string blobName, string contentType, Stream content)2{3 var blob = this.GetBlockBlobReference(blobName);4 blob.Properties.ContentType = contentType;5 await blob.UploadFromStreamAsync(content);6}7public async Task CreateOrUpdateBlobAsync(string blobName, string contentType, Stream content)8{9 var blob = this.GetBlockBlobReference(blobName);10 blob.Properties.ContentType = contentType;11 await blob.UploadFromStreamAsync(content);12}13public async Task CreateOrUpdateBlobAsync(string blobName, string contentType, Stream content)14{15 var blob = this.GetBlockBlobReference(blobName);16 blob.Properties.ContentType = contentType;17 await blob.UploadFromStreamAsync(content);18}19public async Task CreateOrUpdateBlobAsync(string blobName, string contentType, Stream content)20{21 var blob = this.GetBlockBlobReference(blobName);22 blob.Properties.ContentType = contentType;23 await blob.UploadFromStreamAsync(content);24}25public async Task CreateOrUpdateBlobAsync(string blobName, string contentType, Stream content)26{27 var blob = this.GetBlockBlobReference(blobName);28 blob.Properties.ContentType = contentType;29 await blob.UploadFromStreamAsync(content);30}31public async Task CreateOrUpdateBlobAsync(string blobName, string contentType, Stream content)32{33 var blob = this.GetBlockBlobReference(blobName);34 blob.Properties.ContentType = contentType;35 await blob.UploadFromStreamAsync(content);36}37public async Task CreateOrUpdateBlobAsync(string blobName, string contentType, Stream content)38{39 var blob = this.GetBlockBlobReference(blobName);40 blob.Properties.ContentType = contentType;41 await blob.UploadFromStreamAsync(content);42}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Coyote automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful