我正在重写我的C#代码以使用Azure Blob存储而不是文件系统。到目前为止,没有问题重写正常文件操作的代码。但我有一些代码使用来自流的异步写入:
using (var stream = await Request.Content.ReadAsStreamAsync())
{
FileStream fileStream = new FileStream(@"c:\test.txt", FileMode.Create, FileAccess.Write, FileShare.None);
await stream.CopyToAsync(fileStream).ContinueWith(
(copyTask) =>
{
fileStream.Close();
});
}
我需要更改以上内容以使用Azure CloudBlockBlob或CloudBlobStream - 但无法找到一种方法来声明copyToAsync可以写入的流对象。
答案 0 :(得分:2)
您可能希望在CloudBlockBlob上使用UploadFromStreamAsync
方法。这是一个示例代码(虽然我没有尝试运行此代码):
var cred = new StorageCredentials(accountName, accountKey);
var account = new CloudStorageAccount(cred, true);
var blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("container-name");
var blob = container.GetBlockBlobReference("blob-name");
using (var stream = await Request.Content.ReadAsStreamAsync())
{
stream.Position = 0;
await blob.UploadFromStreamAsync(stream);
}