为什么CloudBlockBlob.DownloadToStream总是返回空流? - c#

我有以下代码:

public static void UploadStreamToBlob(Stream stream, string containerName, string blobName)
{
    CloudStorageAccount storageAccount = 
        CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);
    blobContainer.CreateIfNotExists();
    blobContainer.SetPermissions(
        new BlobContainerPermissions
        {
            PublicAccess = BlobContainerPublicAccessType.Blob
        });

    CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName);
    long streamlen = stream.Length;  <-- This shows 203 bytes
    blockBlob.UploadFromStream(stream);        
}

public static Stream DownloadStreamFromBlob(string containerName, string blobName)
{
    CloudStorageAccount storageAccount = 
        CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);

    Stream stream = new MemoryStream();
    CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName);

    if (blockBlob.Exists())
    {
        blockBlob.DownloadToStream(stream);
        long streamlen = stream.Length;  <-- This shows 0 bytes
        stream.Position = 0;          
    }

    return stream;
}

我正在Azure模拟器中运行此程序,我已将其指向Sql Server。

据我所知,似乎UploadFromStream正确发送了数据,但是,如果我尝试运行DownloadStreamFromBlob,它将返回长度为0的流。 blockBlob.Exists返回的是true,所以我认为它在那里。我只是不知道为什么我的信息流是空的。

顺便说一句,我正在传递测试,并在两个调用中都对containerName和blobName进行了测试。

有任何想法吗?

参考方案

啊,我知道了...

以下几行:

long streamlen = stream.Length;
blockBlob.UploadFromStream(stream);   

需要更改为

long streamlen = stream.Length;  
stream.Position = 0;
blockBlob.UploadFromStream(stream);   

Azure SQLite插入 - c#

我有一个Azure云应用,我正在尝试将用户上传的数据存储在〜/ AppData / mydb.db中的SQLite数据库中。由于存在许多问题,因此我不得不部署System.Data.SQLite,因此我修改了代码以使用此sharp-sqlite库。在调试模式下插入数据时,一切正常。在Azure上部署时,用户提供的数据将插入SQLite数据库。在此过程中不会引…

路径中的Python Azure Apps 404 - python

我遵循此Create a Python app in Azure App Service on Linux并使用以下功能上传了代码:@app.route('/predict_json', methods=['POST']) def add_message(): content = request.json tweets…

检查Optional中是否存在null属性,并返回String Java Stream API - java

我有以下class Person private String firstName; private String familyName; // Setters and Getters 我有以下方法public String getFullName(Optional<Person> persons) { return persons .map(p…

从Azure Data Factory执行python脚本 - python

有人可以帮我从Azure数据工厂执行python函数吗?我已经将python函数存储在blob中,并且我试图触发同样的功能。但是我无法做到这一点。请协助。第二,我可以从ADF参数化python函数调用吗? python参考方案 您可能会发现ADF中的Azure Function Activity概念,它允许您在Data Factory管道中运行Azure F…

使用Azure函数自动将Excel保存到CSV - c#

尝试使用Azure Functions自动将Excel文件保存为CSV格式的Blob,以便可以在Logic Apps或Azure Data Factory中使用。我希望使用ExcelDataReader C#库,但我可以将NuGet包下载到我的Function中,但之后被卡住了。当前它似乎卡住了,因为File.Open命令在本地路径中查找文件,并且出现以下错…