use*_*243 3 c# azure blobstorage
我正在重写我的 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();
});
}
Run Code Online (Sandbox Code Playgroud)
我需要更改上述内容以使用 Azure CloudBlockBlob 或 CloudBlobStream - 但找不到声明 copyToAsync 可以写入的流对象的方法。
您可能希望UploadFromStreamAsync在 CloudBlockBlob上使用方法。这是一个示例代码(虽然我没有尝试运行此代码):
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4129 次 |
| 最近记录: |