【问题标题】:AWS S3 & NodeJS - xhr errorAWS S3 和 Node JS - xhr 错误
【发布时间】:2016-10-13 07:40:07
【问题描述】:

我正在尝试按照本教程使用 NodeJS 将文件上传到 Amazon S3,并且已经被代码客户端发生的错误挂断了,但我很确定这是我路线的一部分。出现的 404 错误是在 sign_request 函数中对服务器的 xhr.send() 请求。我在 GET 中记录的文件中的信息是正确的file:138 GET http://localhost:3000/sign?file_name=gtm-value.png&file_type=image/png 404 (Not Found),这让我认为这与我的路线设置有关。

查看:

    <!DOCTYPE html>
    <head>
        {{> app/app-head}}
    </head>
    <body>
        {{> app/app-navigation}}
        <div class="container">
            <div class="col-md-12">
            <form action="/create" method="post">
                <input type="file" name="fileAttachment" id="image"> 
                    <img id="preview">
<button type="submit" id="button">Create Image</button>
            </form>
        </div>
        <script type="text/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();
            xhr.open("GET", "/sign?file_name=" + file.name + "&file_type=" + file.type);
            xhr.onreadystatechange = function() {
                if(xhr.readyState === 4 && xhr.status === 200) {
                    var response = JSON.parse(xhr.responseText);
                    done(response);
                }
            };
            xhr.send();
        };

        document.getElementById("image").onchange = function() {
            var file = document.getElementById("image").files[0]
            if (!file) return
                sign_request(file, function(response) {
                    upload(file, response.signed_request, response.url, function() {
                        document.getElementById("preview").src = response.url
                    });
                });
        };
        </script>

路线:

var aws = require('aws-sdk');

appRoutes.get('/sign', function(req, res){
    aws.config.update({accessKeyId: config.awsAccessKeyId, secretAccessKey: config.awsSecretAccessKey});

    var s3 = new aws.S3();
    var options = {
        Bucket: config.awsBucket,
        Key: req.query.file_name,
        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')

        res.json({
            signed_request: data,
            url: 'https://s3.amazonaws.com/' + config.awsBucket + '/' + req.query.file_name
        });
    });
});

【问题讨论】:

  • 我认为在你从s3.getSignedUrl 方法发回的 json 中,url 应该包含你的存储桶,所以它应该类似于:url : 'https://[bucket].s3.amazonaws.com/ + ...
  • 嘿@fitims 我改变了我的存储桶,但仍然收到相同的请求和错误
  • 实际上确定问题出在使用 auth 密钥应用于用户的策略上。与代码无关。

标签: node.js amazon-s3 xmlhttprequest


【解决方案1】:

该问题与代码无关,并且与使用 auth 密钥提供给用户的权限有关。在为用户提供管理访问策略后,问题得到解决。

【讨论】:

    猜你喜欢
    • 2017-09-04
    • 2018-12-13
    • 1970-01-01
    • 2015-01-12
    • 2020-04-13
    • 2011-10-30
    • 2016-08-23
    • 2015-08-03
    • 1970-01-01
    相关资源
    最近更新 更多