gpt4 book ai didi

c# - 为什么 CloudBlockBlob.DownloadToStream 始终返回空流?

转载 作者:行者123 更新时间:2023-12-02 03:54:12 26 4
gpt4 key购买 nike

我有以下代码:

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,所以我假设它在那里。我只是不明白为什么我的流是空的。

顺便说一句,我在两次调用中都传递了 test 和 test 的containerName 和 blobName。

有什么想法吗?

最佳答案

啊,我明白了...

以下几行:

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

需要更改为

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

关于c# - 为什么 CloudBlockBlob.DownloadToStream 始终返回空流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18114432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com