簡體   English   中英

無法從Azure功能中的Blob存儲讀取文件

[英]Unable To Read File From Blob Storage In Azure Function

我正在嘗試使用Azure Function應用程序從Blob存儲讀取文本文件。 我的目標是讀取CSV文件,然后將其重新格式化為新的CSV文件,並添加原始CSV文件中沒有的其他詳細信息。

我不斷收到以下編譯錯誤:

2018-11-28T00:22:34.125 [Error] run.csx(60,19): error CS1061: 'CloudBlockBlob' does not contain a definition for 'DownloadToStream' and no extension method 'DownloadToStream' accepting a first argument of type 'CloudBlockBlob' could be found (are you missing a using directive or an assembly reference?) 

我可以將代碼的“ Blob存儲”部分復制到控制台應用程序的項目中,它將很好地進行編譯。

我是否缺少參考?

這是完整功能減去Blob存儲的連接字符串。

#r "Newtonsoft.Json"

#r "System.Configuration"
#r "System.Data"

#r "System.Collections"
#r "System.IO.Compression"
#r "System.Net"
#r "Microsoft.WindowsAzure.Storage"
#r "System.Linq"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

using System;
using System.Configuration;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Linq;
using System.Threading.Tasks;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string filePath = req.Query["filePath"];

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    filePath = filePath ?? data?.filePath;

    var fileInfo = GetFileInfo(filePath);

    string line = "";

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("Connection String goes Here");

        CloudBlobClient client = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = client.GetContainerReference(fileInfo.Container);

        var fileNameWithFolder =
                    fileInfo.DirectoryName == ""
                        ? fileInfo.FileName
                        : $"{fileInfo.DirectoryName}/{fileInfo.FileName}";

        CloudBlockBlob blockBlob2 = container.GetBlockBlobReference(fileNameWithFolder);

        using (var memoryStream = new MemoryStream())
        {
            try
            {
                blockBlob2.DownloadToStream(memoryStream);
                line = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
            }
            catch (Exception ex)
            {
                line = ex.Message;
            }
        }   

    return filePath != null
        ? (ActionResult)new OkObjectResult($"filePath: {filePath} Container: {fileInfo.Container} DirectoryName: {fileInfo.DirectoryName} FileName: {fileInfo.FileName}*********{line}")
        : new BadRequestObjectResult("Please pass a filePath on the query string or in the request body");
}

private static FileInfo GetFileInfo(string filePath)
{
    int index = filePath.IndexOf("/");
    filePath = (index < 0)
        ? filePath
        : filePath.Remove(index, 1);

    var filePathSplit = filePath.Split('/');
    var fileInfo = new FileInfo();
    fileInfo.Container = filePathSplit[0];

    if ((filePathSplit.Length - 2) > 0)
    {
        var folderName = "";

        for(var i = 1; i < filePathSplit.Length - 1; i++)
        {
            if (folderName.Trim().Length > 0)
            {
                folderName += "/";
            }

            folderName += filePathSplit[i];
        }

        fileInfo.DirectoryName = folderName;
    }

    fileInfo.FileName = filePathSplit[filePathSplit.Length - 1];

    return fileInfo;
}

public class FileInfo
{
    public string Container { get; set; }
    public string DirectoryName { get; set; }
    public string FileName { get; set; }
}

V2函數基於.Net Core env,因此它根據.Net Standard引用了Microsoft.WindowsAzure.Storage程序集,該程序集沒有同步API,這意味着我們需要* Async方法。

await blockBlob2.DownloadToStreamAsync(memoryStream);

根據您的代碼和錯誤消息,您沒有正確使用方法DownloadToStream。 有關更多詳細信息,請參閱文檔

       CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
        var blobRequestOptions = new BlobRequestOptions
        {
            ServerTimeout = TimeSpan.FromSeconds(30),
            MaximumExecutionTime = TimeSpan.FromSeconds(120),
            RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(3), maxRetryCount),
        };

        using (var memoryStream = new MemoryStream())
        {
            blockBlob.DownloadToStream(memoryStream, null, blobRequestOptions);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM