Is stream Reading can make and send Null 到 blob storage
Is stream Reading can make and send Null to blob storage
我将文件流用于:内存流、读取流或文件流,例如
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);
我将文件流用于:内存流、读取流或文件流,例如
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);