Skip to content

Instantly share code, notes, and snippets.

@xximjasonxx

xximjasonxx/upload.cs

Last active Aug 12, 2020
Embed
What would you like to do?
Azure Function File Upload
public static class UploadFile
{
[FunctionName("UploadFile")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "file/add")] HttpRequest uploadFile,
[Blob("files", FileAccess.Write, Connection = "StorageAccountConnectionString")] CloudBlobContainer blobContainer,
[Table("metadata", Connection = "TableConnectionString")] CloudTable metadataTable,
ILogger log)
{
var fileName = Guid.NewGuid().ToString();
await blobContainer.CreateIfNotExistsAsync();
var cloudBlockBlob = blobContainer.GetBlockBlobReference(fileName);
await cloudBlockBlob.UploadFromStreamAsync(uploadFile.Body);
await metadataTable.CreateIfNotExistsAsync();
var addOperation = TableOperation.Insert(new FileMetadata
{
RowKey = fileName,
PartitionKey = fileName,
});
await metadataTable.ExecuteAsync(addOperation);
return new CreatedResult(string.Empty, fileName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.