我们有一个应用程序(.Net核心),托管在azure应用服务中,我们正在尝试使用UI的表单数据通过web将大型文件上传到Azure blob。我们已经更改了请求长度和API请求超时,即使在上传200 We文件时,我们仍然面临连接超时错误。
下面是我使用的示例代码
[HttpPost]
[Route("upload")]
[Consumes("multipart/form-data")]
[RequestFormLimits(MultipartBodyLengthLimit = 2147483648)]
public async Task<IHttpActionResult> Upload([FromForm] FileRequestObject fileRequestObject)
{
var url = "upload_url_to_blob_storage";
var file = fileRequestObject.Files[0];
var blob = new CloudBlockBlob(new Uri(url));
blob.Properties.ContentType = file.ContentType;
await blob.UploadFromStreamAsync(file.InputStream);
//some other operations based on file upload
return Ok();
}
public class FileRequestObject
{
public List<IFormFile> Files { get; set; }
public string JSON { get; set; }
public string BlobUrls { get; set; }
}发布于 2019-11-07 02:35:39
根据您的代码,您希望将一个大文件上传到Azure存储中,作为块块存储。请注意,它有一个限制。有关更多细节,请参阅文档
通过Put blob创建的块Blob的最大大小对于2016-05-31及更高版本是256 MB,对于旧版本是64 MB。如果blob在2016-05-31及更高版本中大于256 MB,或者对于旧版本大于64 MB,则必须将其作为一组块上载。
因此,如果要使用大型文件来阻止blob,请使用以下步骤:
1.将整个文件读成字节,并在代码中将文件分成更小的部分。
2.用放置块 API上传每一篇文章。
3.利用放置阻止列表 API实现blob。
例如:
[HttpPost]
[Consumes("multipart/form-data")]
[RequestFormLimits(MultipartBodyLengthLimit = 2147483648)]
public async Task<ActionResult> PostAsync([FromForm]FileRequestObject fileRequestObject)
{
string storageAccountConnectionString = "DefaultEndpointsProtocol=https;AccountName=blobstorage0516;AccountKey=UVOOBCxQpr5BVueU+scUeVG/61CZbZmj9ymouAR9609WbqJhhma2N+WL/hvaoNs4p4DJobmT0F0KAs0hdtPcqA==;EndpointSuffix=core.windows.net";
CloudStorageAccount StorageAccount = CloudStorageAccount.Parse(storageAccountConnectionString);
CloudBlobClient BlobClient = StorageAccount.CreateCloudBlobClient();
CloudBlobContainer Container = BlobClient.GetContainerReference("test");
await Container.CreateIfNotExistsAsync();
CloudBlockBlob blob = Container.GetBlockBlobReference(fileRequestObject.File.FileName);
HashSet<string> blocklist = new HashSet<string>();
var file = fileRequestObject.File;
const int pageSizeInBytes = 10485760;
long prevLastByte = 0;
long bytesRemain = file.Length;
byte[] bytes;
using (MemoryStream ms = new MemoryStream())
{
var fileStream = file.OpenReadStream();
await fileStream.CopyToAsync(ms);
bytes = ms.ToArray();
}
// Upload each piece
do
{
long bytesToCopy = Math.Min(bytesRemain, pageSizeInBytes);
byte[] bytesToSend = new byte[bytesToCopy];
Array.Copy(bytes, prevLastByte, bytesToSend, 0, bytesToCopy);
prevLastByte += bytesToCopy;
bytesRemain -= bytesToCopy;
//create blockId
string blockId = Guid.NewGuid().ToString();
string base64BlockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(blockId));
await blob.PutBlockAsync(
base64BlockId,
new MemoryStream(bytesToSend, true),
null
);
blocklist.Add(base64BlockId);
} while (bytesRemain > 0);
//post blocklist
await blob.PutBlockListAsync(blocklist);
return Ok();
// For more information on protecting this API from Cross Site Request Forgery (CSRF) attacks, see https://go.microsoft.com/fwlink/?LinkID=717803
}
public class FileRequestObject
{
public IFormFile File { get; set; }
}


https://stackoverflow.com/questions/58724878
复制相似问题