Skip to content

Instantly share code, notes, and snippets.

@Pownraj-2818
Last active August 19, 2022 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pownraj-2818/4ad0f0eace4ca6e0355699b2e33c828c to your computer and use it in GitHub Desktop.
Save Pownraj-2818/4ad0f0eace4ca6e0355699b2e33c828c to your computer and use it in GitHub Desktop.
public async Task<string> UploadCover(IFormFile form)
{
BlobContainerClient blobContainerClient = new BlobContainerClient(_config.GetConnectionString("BlobStorage"), "space-cover-images");
try
{
BlobClient blobClient = blobContainerClient.GetBlobClient(form.FileName);
// add _copy to filename if blob already exists
while (blobClient.Exists())
{
var splitArray = blobClient.Name.Split('.');
var newFileName = splitArray[0] + "_copy." + splitArray[1];
blobClient = blobContainerClient.GetBlobClient(newFileName);
}
// add blob
using (Stream stream = form.OpenReadStream())
{
await blobClient.UploadAsync(stream);
}
return blobClient.Uri.ToString();
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment