Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[FunctionName("DownloadFile")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "download/{fileId}")] HttpRequest req,
string fileId,
[Table("metadata", "{fileId}", "{fileId}", Connection = "TableConnectionString")] FileMetadata fileMetadata,
[Table("ocrdata", Connection = "TableConnectionString")] CloudTable fileOcrDataTable,
[Blob("files/{fileId}", FileAccess.Read, Connection = "StorageAccountConnectionString")] CloudBlockBlob fileBlob,
ILogger log)
{
if (!fileMetadata.ApprovedForDownload)
{
return new StatusCodeResult(403);
}
var readQuery = new TableQuery<OcrResult>();
TableQuery.GenerateFilterCondition(nameof(OcrResult.PartitionKey), QueryComparisons.Equal, fileId);
var ocrResults = fileOcrDataTable.ExecuteQuery(readQuery).ToList();
return new OkObjectResult(new DownloadResponse
{
Metadata = ocrResults,
FileId = fileId,
DownloadUrl = GenerateSasUrlForFileDownload(fileBlob, fileId)
});
}
static string GenerateSasUrlForFileDownload(CloudBlockBlob blob, string fileId)
{
var policy = new SharedAccessBlobPolicy()
{
SharedAccessExpiryTime = DateTime.Now.AddHours(1),
Permissions = SharedAccessBlobPermissions.Read
};
return blob.Uri + blob.GetSharedAccessSignature(policy);
}
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.