简体   繁体   English

在 azure blob 存储中上传文件

[英]upload file in azure blob storage

I am trying to upload the file that I have stored in MemoryStream using the following code.我正在尝试使用以下代码上传我存储在 MemoryStream 中的文件。

        private static void SaveStream(MemoryStream stream, string fileName)
        {
            var blobStorageService = new BlobStorageService();
            UploadBlob(stream, fileName);
        }

        public void UploadBlob(MemoryStream fileStream,string fileName)
        {
            var blobContainer = _blobServiceClient.GetBlobContainerClient(Environment
                               .GetEnvironmentVariable("ContainerName"));
            var blobClient = blobContainer.GetBlobClient(fileName);
            blobClient.Upload(fileStream);  <--- Error Message

        }

Error Message: System.ArgumentException: 'content.Position must be less than content.Length.Please set content.Position to the start of the data to upload.'错误消息:System.ArgumentException:“content.Position 必须小于 content.Length。请将 content.Position 设置为要上传的数据的开头。”

This happened because the current position is at the end of the stream.发生这种情况是因为当前位置位于流的末尾。 You can set the position to the start of the stream before uploading您可以在上传前将位置设置为流的开头

var blobClient = blobContainer.GetBlobClient(fileName);
fileStream.Position =0;
blobClient.Upload(fileStream)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM