我使用流作为文件:内存流、流读取或文件流,如
byte[] buff = System.IO.File.ReadAllBytes(open.FileName);
System.IO.MemoryStream ms = new System.IO.MemoryStream(buff);
并且我想将其发送到blob存储,此时我的blob是空的,是因为按流读取文件,还是指其他问题,如blob或CloudStorageAccount连接字符串上的配置丢失。
在开始从流上传到blob之前,请确保流位于0
。正如上面评论中提到的,你可以尝试以下方法:
ms.Position = 0
或
ms.Seek(0, SeekOrigin.Begin)
只需使用以下代码。无需转换内存流,您可以使用blob.UploadfromStream方法将流传递到blob存储。
StorageCredentials creds = new StorageCredentials(
ConfigurationManager.AppSettings["accountName"],
ConfigurationManager.AppSettings["accountKey"]
);
CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);
CloudBlobClient client = account.CreateCloudBlobClient();
CloudBlobContainer contain = client.GetContainerReference("your container name");
contain.CreateIfNotExists();
CloudBlockBlob blob = contain.GetBlockBlobReference("your blob name");
using (var stream = System.IO.File.OpenRead("your file"))
blob.UploadFromStream(stream);