Last active
October 5, 2021 19:24
-
-
Save mikecole/e09de696766e6a3e16cc0017d0136936 to your computer and use it in GitHub Desktop.
Upload a file to Azure Blob Stage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task CreateBlobAsync(string connectionString, string containerName, string blobName, string content, string contentType = null) | |
{ | |
var container = new BlobContainerClient(connectionString, containerName); | |
await container.CreateIfNotExistsAsync(); | |
var blob = container.GetBlobClient(blobName); | |
await blob.DeleteIfExistsAsync(); | |
using var stream = new MemoryStream(Encoding.Default.GetBytes(content), false); | |
var headers = new BlobHttpHeaders(); | |
if (!string.IsNullOrWhiteSpace(contentType)) | |
{ | |
headers.ContentType = contentType; | |
} | |
await blob.UploadAsync(stream, headers); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Link to NuGet library: https://www.nuget.org/packages/Azure.Storage.Blobs/