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

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

ImageController.cs

Source:ImageController.cs Github

copy

Full Screen

...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

...21 var imageContents = await this.BlobContainer.GetBlobAsync(accountName, imageStorageName);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

CreateContainerIfNotExistsAsync

Using AI Code Generation

copy

Full Screen

1var storageAccount = CloudStorageAccount.Parse(StorageConnectionString);2var blobClient = storageAccount.CreateCloudBlobClient();3var container = blobClient.GetContainerReference("petimages");4await container.CreateIfNotExistsAsync();5var blockBlob = container.GetBlockBlobReference("1.png");6using (var fileStream = System.IO.File.OpenRead(@"C:\Users\Public\Pictures\Sample Pictures\1.png"))7{8 await blockBlob.UploadFromStreamAsync(fileStream);9}10var storageAccount = CloudStorageAccount.Parse(StorageConnectionString);11var blobClient = storageAccount.CreateCloudBlobClient();12var container = blobClient.GetContainerReference("petimages");13await container.CreateIfNotExistsAsync();14var blockBlob = container.GetBlockBlobReference("2.png");15using (var fileStream = System.IO.File.OpenRead(@"C:\Users\Public\Pictures\Sample Pictures\2.png"))16{17 await blockBlob.UploadFromStreamAsync(fileStream);18}19var storageAccount = CloudStorageAccount.Parse(StorageConnectionString);20var blobClient = storageAccount.CreateCloudBlobClient();21var container = blobClient.GetContainerReference("petimages");22await container.CreateIfNotExistsAsync();23var blockBlob = container.GetBlockBlobReference("3.png");24using (var fileStream = System.IO.File.OpenRead(@"C:\Users\Public\Pictures\Sample Pictures\3.png"))25{26 await blockBlob.UploadFromStreamAsync(fileStream);27}28var storageAccount = CloudStorageAccount.Parse(StorageConnectionString);29var blobClient = storageAccount.CreateCloudBlobClient();30var container = blobClient.GetContainerReference("petimages");31await container.CreateIfNotExistsAsync();32var blockBlob = container.GetBlockBlobReference("4.png");33using (var fileStream = System.IO.File.OpenRead(@"C:\Users\Public\Pictures\Sample Pictures\4.png"))34{35 await blockBlob.UploadFromStreamAsync(fileStream);36}37var storageAccount = CloudStorageAccount.Parse(Storage

Full Screen

Full Screen

CreateContainerIfNotExistsAsync

Using AI Code Generation

copy

Full Screen

1PetImages.Storage.BlobContainer blobContainer = new PetImages.Storage.BlobContainer();2await blobContainer.CreateContainerIfNotExistsAsync("petimages");3PetImages.Storage.BlobContainer blobContainer = new PetImages.Storage.BlobContainer();4await blobContainer.GetBlobReference("petimages", "dog.jpg");5PetImages.Storage.BlobContainer blobContainer = new PetImages.Storage.BlobContainer();6await blobContainer.UploadBlobAsync("petimages", "dog.jpg", "C:\\Users\\Public\\Pictures\\Sample Pictures\\dog.jpg");7PetImages.Storage.BlobContainer blobContainer = new PetImages.Storage.BlobContainer();8await blobContainer.DownloadBlobAsync("petimages", "dog.jpg", "C:\\Users\\Public\\Pictures\\Sample Pictures\\dog.jpg");9PetImages.Storage.BlobContainer blobContainer = new PetImages.Storage.BlobContainer();10await blobContainer.DeleteBlobAsync("petimages", "dog.jpg");11PetImages.Storage.BlobContainer blobContainer = new PetImages.Storage.BlobContainer();12await blobContainer.DeleteContainerAsync("petimages");13PetImages.Storage.BlobContainer blobContainer = new PetImages.Storage.BlobContainer();14await blobContainer.ListBlobsAsync("petimages");15PetImages.Storage.BlobContainer blobContainer = new PetImages.Storage.BlobContainer();16await blobContainer.ListContainersAsync();17PetImages.Storage.BlobContainer blobContainer = new PetImages.Storage.BlobContainer();18await blobContainer.GetBlobSasUriAsync("petimages", "dog.jpg");

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