Is stream Reading can make and send Null to blob storage

蹲街弑〆低调 提交于 2019-12-12 03:14:53

问题


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.


回答1:


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);



回答2:


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)


来源:https://stackoverflow.com/questions/34474850/is-stream-reading-can-make-and-send-null-to-blob-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!