Last active
August 12, 2020 01:26
-
-
Save xximjasonxx/4adcb617252de0cf1bb8a43607477bd4 to your computer and use it in GitHub Desktop.
Azure Function File Upload
This file contains 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 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