Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js CloudFront-NodeJS签名Cookie设置_Node.js_Amazon Web Services_Express_Amazon S3_Amazon Cloudfront - Fatal编程技术网

Node.js CloudFront-NodeJS签名Cookie设置

Node.js CloudFront-NodeJS签名Cookie设置,node.js,amazon-web-services,express,amazon-s3,amazon-cloudfront,Node.js,Amazon Web Services,Express,Amazon S3,Amazon Cloudfront,我一直在阅读各种CloudFront文档,了解如何将签名cookie设置为允许访问我的s3存储桶的方法。我已完成以下步骤: 使用CNAME为files.mysite.com的自定义SSL证书创建分发 调整行为以限制查看者访问=是且受信任的签名者=自我 创建了CloudFront密钥对 我现在正处于实现代码的阶段,以便能够在后端对代码进行签名以访问数据,但我不确定如何使用我当前使用的s3签名进行设置 现在,我已经设置了一个路由,对我的s3数据进行签名,以便能够上载和获取我的bucket文件,同时在

我一直在阅读各种CloudFront文档,了解如何将签名cookie设置为允许访问我的s3存储桶的方法。我已完成以下步骤:

  • 使用CNAME为files.mysite.com的自定义SSL证书创建分发

  • 调整行为以限制查看者访问=是且受信任的签名者=自我

  • 创建了CloudFront密钥对

  • 我现在正处于实现代码的阶段,以便能够在后端对代码进行签名以访问数据,但我不确定如何使用我当前使用的s3签名进行设置

    现在,我已经设置了一个路由,对我的s3数据进行签名,以便能够上载和获取我的bucket文件,同时在我的db中保存指向该s3文件路径的链接,用作我站点上指向该文件的链接:

    appRoutes.get('/sign', function(req, res){
        aws.config.update({accessKeyId: process.env.AWS_ACCESS_KEY, secretAccessKey: process.env.AWS_SECRET_KEY, region: process.env.AWS_REGION});
    
        var uploadDate = new moment().format("YYYY-MM-DD");
        var fileNameFormatted = req.query.file_name.replace(/\s+/g, '-').toLowerCase();
    
        var s3 = new aws.S3()
        var options = {
          Bucket: process.env.AWS_BUCKET,
          Key: req.user.organizationId + '/' + uploadDate + '/' + fileNameFormatted,
          Expires: 60,
          ContentType: req.query.file_type,
          ACL: 'public-read'
        }
    
        s3.getSignedUrl('putObject', options, function(err, data){
          if(err) return res.send('Error with S3')
    
            console.log('This is the data: ' + data);
          res.json({
            signed_request: data,
            url: 'https://' + process.env.AWS_BUCKET + '.s3.amazonaws.com/' + req.user.organizationId + '/' + uploadDate + '/' + fileNameFormatted
          });
        });
    });
    
    在我看来,客户端的javascript在上传文件时会触发此路由:

    function upload(file, signed_request, url, done) {
        var xhr = new XMLHttpRequest();
        xhr.open("PUT", signed_request);
        xhr.setRequestHeader('x-amz-acl', 'public-read');
        xhr.onload = function() {
            if (xhr.status === 200) {
                done();
            };
        };
        xhr.send(file);
    };
    
    function sign_request(file, done) {
        var xhr = new XMLHttpRequest();
        console.log(xhr);
        console.log(file);
        xhr.open("GET", "/app/sign?file_name=" + file.name + "&file_type=" + file.type);
        xhr.onreadystatechange = function() {
            if(xhr.readyState === 4 && xhr.status === 200) {
                console.log(xhr.responseText);
                var response = JSON.parse(xhr.responseText);
                console.log(response);
                done(response);
            }
        };
        xhr.send();
    };
    
    document.getElementById("file-input").onchange = function() {
        var file = document.getElementById("file-input").files[0];
        if (!file) return
            sign_request(file, function(response) {
                console.log('sign_request client file: ' + file);
                console.log('sign_request client response: ' + response);
                upload(file, response.signed_request, response.url, function() {
                    document.getElementById("preview").src = response.url;
                    document.getElementById("file-output").value = response.url;
                });
            });
    };
    

    在这种设置下,我不确定CloudFront签名的Cookie是否适合我的客户端或服务器端代码。我知道其目的是通过
    files.mysite.com/s3文件路径访问这些文件,而不是
    s3bucket.aws.com
    ,但是,我是否会配置其他客户端代码,以使用我的cloudfront签名cookie触发另一个签名路由,然后保存该链接?

    cloudfront签名cookie的要点是,为了使授权用户能够下载文件,您不需要对URL进行签名。CloudFront将根据cookie而不是URL对请求进行身份验证。@Michael sqlbot感谢您的评论。那么,你是说我应该将数据库中存储的s3链接更改为cloudfront访问点,然后在用户身份验证时签署cookie,而不是在上载文件的过程中?我想就你的观点而言,cookie应该在什么时候设置为允许授权用户访问与其凭据相关的文件?签名cookie似乎最适合用于授权下载。。。我会犹豫是否支持使用签名cookies来授权上传的想法。我认为这是可能的,但直觉上感觉这不是一个好主意,而且可能还有其他的复杂情况,这取决于涉及哪个S3区域。我注意到您正在使用
    public read
    ACL上载文件,这通常使任何人都可以下载文件,因此。。。也许你应该澄清你真正想要完成的是什么,而不是把注意力集中在你是如何试图完成它的上。@Michael sqlbot很抱歉在我的评论中有点混乱。我不打算使用cloudfront来上传文件。我只是计划在用户试图访问(获取请求)位于s3中的文件时将其与url一起使用。因此,我是否应该在与我最初的问题完全不同的过程中签署cookies?当用户获得授权时,是否可以将已签名的cookie附加到用户?