简体   繁体   English

适用于视频文件的AWS S3和Cloudfront CDN

[英]AWS S3 and Cloudfront CDN for video files

I'm just getting started using AWS S3 and Cloudfront service. 我刚刚开始使用AWS S3和Cloudfront服务。 So far I've created a S3 instance and a Node.js server which streams the files from S3 to the user reaching the server's endpoint. 到目前为止,我已经创建了一个S3实例和一个Node.js服务器,该服务器将文件从S3流传输到到达服务器端点的用户。

const params = {
    Bucket: BUCKET_NAME,
    Key: KEY
 };  
const obj = s3.getObject(params);
const stream = obj.createReadStream()
stream.pipe(res); // RES is the result send back to user

From my understanding, you get a CDN for your files, if you spin up a CloudFront distribution so my next step was to add Cloudfront to my S3 bucket. 据我了解,如果您启动一个CloudFront发行版,您将获得文件的CDN,所以下一步是将Cloudfront添加到我的S3存储桶中。

My next action was to add the endpoint to the S3 instance in the code: 我的下一个动作是将端点添加到代码中的S3实例中:

const s3 = new aws.S3({
  endpoint: CLOUDFRONT_URL
})

Now however I get the following error when people reach the endpoint: 现在,当人们到达终点时,我得到以下错误:

UnknownEndpoint: Inaccessible host: BUCKET_NAME.CLOUDFRONT_URL. 未知端点:无法访问的主机:BUCKET_NAME.CLOUDFRONT_URL。 This service may not be available in the `us-east-1' region. 此服务在'us-east-1'地区可能不可用。

It seems from the error message, Cloudfront is not available in the US-EAST region which seems weird to me? 从错误消息看来,Cloudfront在美国东部地区不可用,这对我来说很奇怪? I'm thinking I just made a mistake configuring it in the code. 我以为我在代码中配置它时犯了一个错误。 Any advice? 有什么建议吗?

Edit: 编辑:

My use case is as follows: 我的用例如下:

I have an app that allow downloads of video content. 我有一个允许下载视频内容的应用程序。 These videos are stored on AWS S3. 这些视频存储在AWS S3上。 Since people from all over the world are accessing these videos, my idea was to enable Cloudfront on the bucket to make downloads available at servers closer to the user and therefore getting better download speed. 由于来自世界各地的人们都在访问这些视频,所以我的想法是使存储桶中的Cloudfront能够在离用户更近的服务器上进行下载,从而提高下载速度。

Currently the user ask the endpoint to download a file and the Node.js server streams the video file from S3 to the user. 当前,用户要求端点下载文件,并且Node.js服务器将视频文件从S3流传输到用户。

I might have misunderstood the use case for Cloudfront. 我可能误解了Cloudfront的用例。

This can be tried by making few changes in the given configuration as below: 可以通过在给定配置中进行一些更改来尝试此操作,如下所示:

Change the piece of code: 更改代码段:

const s3 = new aws.S3({
  endpoint: CLOUDFRONT_URL
})  

to

const s3 = new aws.S3({
      "accessKeyId": "Access_Key",
      "secretAccessKey": "Secret_access_key",
      "s3BucketEndpoint": "true",
      "endpoint": "YourS3ENDPoint"
    })

This should help! 这应该有所帮助!

Instead of creating a readStream, you could use the SDK to get a presigned URL for download. 您可以使用SDK获取预签名的URL来下载,而不是创建readStream。 That way you don't need to pay extra for cloudfront. 这样,您无需为Cloudfront支付额外费用。

Here is an example: 这是一个例子:

   const Aws = require('aws-sdk');
   const s3 = new Aws.S3();

   Aws.config.update({accessKeyId: 'accessKey created on IAM',
                      secretAccessKey: 'secretKey created on IAM',
                      region: 'default region'
                      });

   const download_url = s3.getSignedUrl('getObject', {
                                        Bucket: 'bucket of file',
                                        Key: 'filename of filepath within the bucket',
                                        Expires: 'case you need to expire the url (value in seconds)'})

console.log(download_url); 

If this solved, let me know :) 如果解决了,请告诉我:)

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

相关问题 Amazon S3 / Cloudfront视频解码失败 - Amazon S3/Cloudfront Video Decoding Failed 如何在没有NodeJ的情况下使用javascript sdk为s3视频创建AWS CloudFront签名的URL - How to create AWS CloudFront signed url for s3 video using javascript sdk without NodeJs 通过AWS SDK创建签名的S3和Cloudfront URL - Creating signed S3 and Cloudfront URLs via the AWS SDK 使用CloudFront和Amazon S3或另一CDN,我可以将所有类似的请求发送到一个静态文件吗? - Using CloudFront and Amazon S3, or another CDN, can I send all similar requests to one static file? 我需要为 s3 和 cloudfront 生成 br 和 gz 文件吗? - Do I need to generate br and gz files for s3 and cloudfront? 如何在云端和 s3 static 文件中提供服务器页面? - How to serve server page inside cloudfront and s3 static files? S3 子目录的 Cloudfront 重定向 - Cloudfront redirect for S3 subdirectories 使用 NodeJS 从 AWS S3 流式传输文件 - streaming files from AWS S3 with NodeJS 访问子文件夹AWS S3中的文件 - Accessing files in subfolder AWS S3 如何使用 AWS Lambda + CloudFront + S3 设置为内联脚本/样式动态添加 CSP 随机数属性? - How to dynamically adds CSP nonce attributes for inline scripts/styles with AWS Lambda + CloudFront + S3 setup?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM