4MB보다 큰 파일을 업로드하려고 RequestBodyTooLarge
하면 다음 메시지와 함께 예외가 발생합니다.
The request body is too large and exceeds the maximum permissible limit.
이 제한은 REST API 참조 ( https://docs.microsoft.com/en-us/rest/api/storageservices/put-range )에 문서화되어 있지만 SDK Upload * 메서드 ( https : // docs.microsoft.com/en-us/dotnet/api/azure.storage.files.shares.sharefileclient.uploadasync?view=azure-dotnet ). 이 문제를 해결하는 예도 없습니다.
그렇다면 대용량 파일을 업로드하는 방법은 무엇입니까?
많은 시행 착오 끝에 파일 업로드 제한을 해결하기 위해 다음 방법을 만들 수있었습니다. 아래 코드에는 업로드 할 폴더 _dirClient
로 이미 초기화 된 ShareDirectoryClient
세트가 있습니다.
수신 스트림이 4MB보다 크면 코드는 4MB 청크를 읽고 완료 될 때까지 업로드합니다. 는 HttpRange
바이트가 이미 푸른 업로드 파일에 추가됩니다 곳입니다. 인덱스는 새 바이트가 추가되도록 Azure 파일의 끝을 가리 키도록 증분되어야합니다.
public async Task WriteFileAsync(string filename, Stream stream) {
// Azure allows for 4MB max uploads (4 x 1024 x 1024 = 4194304)
const int uploadLimit = 4194304;
stream.Seek(0, SeekOrigin.Begin); // ensure stream is at the beginning
var fileClient = await _dirClient.CreateFileAsync(filename, stream.Length);
// If stream is below the limit upload directly
if (stream.Length <= uploadLimit) {
await fileClient.Value.UploadRangeAsync(new HttpRange(0, stream.Length), stream);
return;
}
int bytesRead;
long index = 0;
byte[] buffer = new byte[uploadLimit];
// Stream is larger than the limit so we need to upload in chunks
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) {
// Create a memory stream for the buffer to upload
using MemoryStream ms = new MemoryStream(buffer, 0, bytesRead);
await fileClient.Value.UploadRangeAsync(new HttpRange(index, ms.Length), ms);
index += ms.Length; // increment the index to the account for bytes already written
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다