将视频上传到 .net 中的 AWS S3



所以我试图将视频上传到我的 S3 存储桶,代码几乎可以工作,它目前所做的是将一个空文件上传到存储桶,因此 API 与 AWS 正确交互,但它没有在请求中发送视频。当我发送图像(并将".mp4"更改为".png"(时,它工作正常,但是当我上传视频时,它不起作用

控制器

[HttpPost("{userId}/video")]
[RequestSizeLimit(999_000_000)]
public async Task<ActionResult> VideoUpload(int userId, [FromForm] PhotoForCreationDto photoForCreationDto)
{
AmazonS3VideoUploader amazonS3v = new AmazonS3VideoUploader();
var keyName = photoForCreationDto.Username + getKeyName() + ".mp4";
var file = photoForCreationDto.File;
amazonS3v.UploadFile(file, keyName);
return Ok();
}

存储 库

public async void UploadFile(IFormFile file, string keyName)
{
var client = new AmazonS3Client(myAwsAccesskey, myAwsSecret, Amazon.RegionEndpoint.EUWest2);
using (var stream = file.OpenReadStream())
{
try
{
PutObjectRequest putRequest = new PutObjectRequest
{
BucketName = bucketName,
Key = keyName,
InputStream = stream,
// ContentType = "image/png"
ContentType = "video/mp4"
};
PutObjectResponse response = await client.PutObjectAsync(putRequest);
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null &&
(amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
||
amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
throw new Exception("Check the provided AWS Credentials.");
}
else
{
throw new Exception("Error occurred: " + amazonS3Exception.Message);
}
}
}
}
}

过了一会儿,它给了我这个错误:

Unhandled Exception: System.ObjectDisposedException: Cannot access a closed file.
at System.IO.FileStream.get_Position()
at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.get_Position()
at Microsoft.AspNetCore.Http.Internal.ReferenceReadStream.VerifyPosition()
at Microsoft.AspNetCore.Http.Internal.ReferenceReadStream.set_Position(Int64 value)
at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext) in D:JenkinsWorkspacestrebuchet-stage-releaseAWSDotNetPublicsdksrcCoreAmazon.RuntimePipelineRetryHandlerRetryHandler.cs:line 149
at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.S3.Internal.AmazonS3ExceptionHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
at Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext executionContext)
at cartalk.api.Data.AmazonS3VideoUploader.UploadFile(IFormFile file, String keyName) in D:Projects in progressCar talk_application - currentcartalk.apiDataAmazonS3VideoUploader.cs:line 82
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
at System.Threading.ThreadPoolWorkQueue.Dispatch()

为了修复此错误,我不得不稍微更改代码,而不是将视频(System.IO.Stream(发送到存储库,我必须在控制器本身内部执行所有操作。我认为发生这种情况的原因是流在请求发送到 AWS 之前关闭。在此处阅读有关它的更多信息:System.ObjectDisposedException:无法访问关闭的流

这是我将其更改为的内容:

[HttpPost("{userId}/video")]
[RequestSizeLimit(999_000_000)]
public async Task<ActionResult> VideoUpload(int userId, [FromForm] PhotoForCreationDto photoForCreationDto)
{
AmazonS3VideoUploader amazonS3v = new AmazonS3VideoUploader();
var keyName = getKeyName() + ".mp4";
var file = photoForCreationDto.File;
var client = new AmazonS3Client(myAwsAccesskey, myAwsSecret, Amazon.RegionEndpoint.EUWest2);
if (file.Length > 0)
{
using (var stream = file.OpenReadStream())
{
try
{
PutObjectRequest putRequest = new PutObjectRequest
{
BucketName = bucketName,
Key = keyName,
InputStream = stream,
ContentType = "video/mp4"
};
await client.PutObjectAsync(putRequest);
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null &&
(amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
||
amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
throw new Exception("Check the provided AWS Credentials.");
}
else
{
throw new Exception("Error occurred: " + amazonS3Exception.Message);
}
}
}
}
return Ok();
}

最新更新