简体   繁体   English

如何使用C#HttpClient或AWS开发工具包通过CloudFront将视频上传到S3

[英]How to upload video to S3 via CloudFront using C# HttpClient or AWS SDK

I'm trying to upload files (Images & Video) to an AWS CloudFront distribution, that points to an s3 bucket. 我正在尝试将文件(图像和视频)上传到指向s3存储桶的AWS CloudFront发行版。

Currently, I can use the HttpClient to GET and PUT files using signed URLs generated via the CloudFront SDK. 目前,我可以使用HttpClient通过CloudFront SDK生成的签名URL来获取和放置文件。

using (HttpClient client = new HttpClient())
using (var stream = File.OpenRead(filepath))
using (HttpResponseMessage response = await client.PutAsync(url, new StreamContent(stream)))
{
    response.EnsureSuccessStatusCode();
}

I originally tried a POST, but this didn't work at all (it timed out after 30 seconds) and I found from this SO Answer that I need to add client.DefaultRequestHeaders.Add("x-amz-acl", "bucket-owner-full-control"); 我最初尝试过POST,但是这根本不起作用(30秒后超时),我从该SO Answer中发现我需要添加client.DefaultRequestHeaders.Add("x-amz-acl", "bucket-owner-full-control"); to give my object ACL access permissions so the bucket owner can access via the console. 授予我的对象ACL访问权限,以便存储桶所有者可以通过控制台进行访问。

I know I can upload to S3 using the AWS S3 SDK and I could enable transfer acceleration, though the AWS FAQ states that CloudFront is a better choice when uploading smaller files or datasets (< 1GB). 我知道我可以使用AWS S3 SDK上传到S3,并且可以启用传输加速,尽管AWS FAQ指出,在上传较小的文件或数据集(小于1GB)时,CloudFront是更好的选择。

I've found the CloudFront documentation vague, wrong or non-existant for anything other than the initial setup of the CloudFront distribution. 除了CloudFront发行版的初始设置外,我发现CloudFront文档含糊不清,不正确或不存在。

Is the above method the correct way to upload any files to S3 via CloudFront, or is there an optimised, more robust way (eg multi-part uploads, so larger files can be resumed) - I want to optimise this for uploading Video, so if answers could focus on this it would be appreciated. 以上方法是通过CloudFront将任何文件上传到S3的正确方法,还是有一种优化,更可靠的方法(例如,分段上传,因此可以恢复更大的文件)-我想针对上传视频进行优化,因此如果答案可以集中于此,将不胜感激。

AWS Support suggest response in case this helps someone: 如果这对某人有帮助,AWS Support会建议响应:

... three possible solutions. ...三种可能的解决方案。 The first being that you can PUT objects to S3 via a signed URL to the S3 origin. 首先,您可以通过指向S3源的签名URL将对象放入S3。 The second option, PUTing the file through CF into S3 via S3 pre-signed URL. 第二种选择是通过CF通过S3预签名URL将文件放入S3。 The third and most favorable option, using the Transfer Acceleration Endpoint to PUT the object into S3. 第三个也是最有利的选择,使用传输加速端点将对象放入S3。

From my understanding, the FAQ stated that using CF instead of the TA endpoint is better for files smaller than 1 GB because TA is optimized for larger files. 根据我的理解,常见问题解答指出,对于小于1 GB的文件,使用CF而不是TA终结点更好,因为TA是为较大的文件而优化的。 However, there are many factors that can influence the performance, I suggest testing both methods to see which service works best for your environment. 但是,有许多因素会影响性能,我建议测试这两种方法以查看哪种服务最适合您的环境。

They also mention CF is much more complex to do multipart uploads: 他们还提到CF进行分段上传要复杂得多:

Its going to be much more difficult if you need to use CloudFront signed URL for other reasons. 如果您出于其他原因需要使用CloudFront签名的URL,则将更加困难。 You will need to use the Multipart Upload APIs (InitiateMultipartUpload, UploadPart, CompleteMultipartUpload) and sign them accordingly. 您将需要使用分段上传API(InitiateMultipartUpload,UploadPart,CompleteMultipartUpload)并进行相应的签名。 Unfortunately we don't have any documentation or steps on how to do this. 不幸的是,我们没有任何文档或有关如何执行此操作的步骤。 You can find more information on the Multipart Upload process here [2]. 您可以在[2]中找到有关分段上传过程的更多信息。

I highly recommend using the TransferUtility and S3 Transfer Acceleration endpoints if possible. 如果可能,我强烈建议使用TransferUtility和S3 Transfer Acceleration端点。

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

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