如何使用C#HttpClient或AWS开发工具包通过CloudFront将视频上传到S3 [英] How to upload video to S3 via CloudFront using C# HttpClient or AWS SDK

查看:246
本文介绍了如何使用C#HttpClient或AWS开发工具包通过CloudFront将视频上传到S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件(图像和视频)上传到指向s3存储桶的AWS CloudFront发行版。

I'm trying to upload files (Images & Video) to an AWS CloudFront distribution, that points to an s3 bucket.

当前,我可以使用使用通过CloudFront SDK生成的签名URL从HttpClient到GET和PUT文件。

Currently, I can use the HttpClient to GET and PUT files using signed URLs generated via the CloudFront SDK.

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

我最初尝试过POST,但这根本不起作用(它30秒后超时),我从此 SO答复中发现我需要添加客户端。 DefaultRequestHeaders.Add( x-amz-acl, bucket-owner-full-control); 授予我的对象ACL访问权限,以便存储桶所有者可以通过控制台进行访问。

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"); to give my object ACL access permissions so the bucket owner can access via the console.

我知道我可以使用AWS S3 SDK上传到S3,并且可以启用传输加速,尽管AWS FAQ指出,在上传较小的文件或数据集时,CloudFront是更好的选择(< ; 1GB)。

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).

除了CloudFront发行版的初始设置外,我发现CloudFront文档含糊不清,错误或不存在。

I've found the CloudFront documentation vague, wrong or non-existant for anything other than the initial setup of the CloudFront distribution.

以上方法是通过CloudFront将任何文件上传到S3的正确方法,还是有一种优化,更可靠的方法(例如,分段上传)较大的文件可以是继续)-我想针对上传视频进行优化,因此,如果答案可以集中于此,将不胜感激。

Is the above method the correct way to upload any files to S3 via CloudFront, or is there an optimised, more robust way (e.g. 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.

推荐答案

AWS支持建议建议以防万一,以帮助他人:

AWS Support suggest response in case this helps someone:


...三种可能的解决方案。第一个是您可以通过S3源的签名URL将对象放入
S3。第二种选择是通过CF通过S3预签名URL将
文件放入S3。
的第三个也是最有利的选择,使用传输加速端点将
对象放入S3。

... three possible solutions. The first being that you can PUT objects to S3 via a signed URL to the S3 origin. The second option, PUTing the file through CF into S3 via S3 pre-signed URL. The third and most favorable option, using the Transfer Acceleration Endpoint to PUT the object into S3.

据我了解,常见问题解答指出对于小于1 GB的文件,使用CF而不是TA
端点更好,因为TA已针对较大文件进行了优化
。但是,有很多因素会影响
的性能,我建议测试两种方法以查看哪种服务
最适合您的环境。

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. However, there are many factors that can influence the performance, I suggest testing both methods to see which service works best for your environment.

他们还提到CF进行分段上传要复杂得多:

They also mention CF is much more complex to do multipart uploads:


要困难得多如果您出于其他原因需要使用CloudFront
签名URL。您将需要使用Multipart
上传API(InitiateMultipartUpload,UploadPart,
CompleteMultipartUpload)并进行相应的签名。很遗憾,我们
没有任何文档或步骤说明如何执行此操作。您可以在[2]中找到有关分段上传过程的
更多信息。

Its going to be much more difficult if you need to use CloudFront signed URL for other reasons. You will need to use the Multipart Upload APIs (InitiateMultipartUpload, UploadPart, CompleteMultipartUpload) and sign them accordingly. 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].

我强烈建议使用TransferUtility和S3 Transfer
Acceleration端点,如果可能。

I highly recommend using the TransferUtility and S3 Transfer Acceleration endpoints if possible.

这篇关于如何使用C#HttpClient或AWS开发工具包通过CloudFront将视频上传到S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆