Azure Blob Storage에 파일 업로드

Sep 11 2020

다음 코드를 사용하여 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

        }

오류 메시지 : System.ArgumentException : 'content.Position은 content.Length. Please set content.Position to the start of the data to upload.'

답변

8 ToanNguyen Sep 11 2020 at 11:57

이것은 현재 위치가 스트림의 끝에 있기 때문에 발생했습니다. 업로드하기 전에 스트림 시작 위치를 설정할 수 있습니다.

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