如何使用带有HTTP触发器的Azure功能将Azure文件上传到Azure Blob存储中?

时间:2019-06-05 12:46:58

标签: c# azure azure-functions azure-cosmosdb azure-blob-storage

我正在尝试使用Azure函数(HTTP触发器)将excel文件上传到azure blob存储中。我附上了我的代码。它无法正常工作。

我遇到类似"Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List<FileUpload1.Function1.FileDetails>>' to 'FileUpload1.Function1.FileDetails' FileUpload"

的错误

请帮助我。

    [FunctionName("Function1")]
    public static FileDetails  Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req,
        ILogger log)
    {
        if (!req.Content.IsMimeMultipartContent("form-data"))
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }
        var multipartStreamProvider = new AzureBlobStorageMultipartProvider(BlobHelper.GetWebApiContainer());
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("configuratorstorage1");
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference("excelfiles");
        //var count = container.ListBlobs().Count(); return
        return req.Content.ReadAsMultipartAsync(multipartStreamProvider).ContinueWith(t =>
        {
            if (t.IsFaulted)
            {
                throw t.Exception;
            }
            AzureBlobStorageMultipartProvider provider = t.Result;
            return provider.Files;
        });

我希望我必须使用azure函数HTTP触发器将Excel文件上传到azure blob存储中。

1 个答案:

答案 0 :(得分:0)

当前,req.Content.ReadAsMultipartAsync(multipartStreamProvider).ContinueWith(t => ...返回Task<FileDetails>。您需要await来获取要返回的对象public static FileDetails Run()

return await req.Content.ReadAsMultipartAsync(multipartStreamProvider).ContinueWith(t =>
{
    if (t.IsFaulted)
    {
        throw t.Exception;
    }
    AzureBlobStorageMultipartProvider provider = t.Result;
    return provider.Files;
});

编辑:快速浏览Google-req.Content.ReadAsMultipartAsync(multipartStreamProvider).ContinueWith(t => ...可能返回Task<List<FileDetails>>,因此,如果上述方法不起作用,请尝试使用