Is stream Reading can make and send Null to blob s

2019-09-19 13:34发布

I use stream for file with: memory stream, stream readinng or file stream such as

byte[] buff = System.IO.File.ReadAllBytes(open.FileName);
System.IO.MemoryStream ms = new System.IO.MemoryStream(buff);

and I want to send it to blob storage and at that point my blob is empty, is it because of reading file by stream or it refers to other problem such as miss configuration on blob or CloudStorageAccount connection string.

2条回答
看我几分像从前
2楼-- · 2019-09-19 14:03

Just use the below code. No need to convert memory stream, You can pass stream to blob storage using Blob.UploadfromStream method.

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);
查看更多
相关推荐>>
3楼-- · 2019-09-19 14:08

Please ensure that your stream is positioned at 0 just before you start uploading to blob from that stream. As mentioned in the comments above, you could try the following:

ms.Position = 0

or

ms.Seek(0, SeekOrigin.Begin)
查看更多
登录 后发表回答