简体   繁体   English

尝试将文件流式传输到node.js中的Amazon S3存储桶时“在配置中缺少凭据”

[英]“Missing credentials in config” while trying to stream a file to Amazon S3 bucket in node.js

I tried to setup a basic file stream to Amazon S3. 我尝试为Amazon S3设置基本文件流。 I copied and pasted the example on https://www.npmjs.com/package/s3-upload-stream 我复制并粘贴了https://www.npmjs.com/package/s3-upload-stream上的示例

And modified it slightly (as attached). 并略微修改(如附件)。

   var AWS = require('aws-sdk'),
   zlib = require('zlib'),
   s = require('fs');
   s3Stream = require('s3-upload-stream')(new AWS.S3()),

        // Set the client to be used for the upload.
        AWS.config.loadFromPath('./config.json');


    // Create the streams
    var read = fs.createReadStream('path to a file');
    var compress = zlib.createGzip();
    var upload = s3Stream.upload({
        "Bucket": "bucketName",
        "Key": "file.txt"
    });

    module.exports = function (router) {
        router.get('/', function (req, res) {
            res.render('upload')
        })
        router.post('/', function (req, res) {
            // Handle errors.
            upload.on('error', function (error) {
                console.log(error);
            });


            /* Handle progress. Example details object:
             { ETag: '"f9ef956c83756a80ad62f54ae5e7d34b"',
             PartNumber: 5,
             receivedSize: 29671068,
             uploadedSize: 29671068 }
             */
            upload.on('part', function (details) {
                console.log(details);
            });


            /* Handle upload completion. Example details object:
             { Location: 'https://bucketName.s3.amazonaws.com/filename.ext',
             Bucket: 'bucketName',
             Key: 'filename.ext',
             ETag: '"bf2acbedf84207d696c8da7dbb205b9f-5"' }
             */
            upload.on('uploaded', function (details) {
                console.log(details);
            });

            // Pipe the incoming filestream through compression, and up to S3.
            read.pipe(compress).pipe(upload);

        })

    }

My config file is setup like so: 我的配置文件设置如下:

{
 "accessKeyId": "key",
 "secretAccessKey":"secret",
 "region": "us-east-1e"
}

The code returns the following error: 该代码返回以下错误:

    Failed to create a multipart upload on S3 : 
        {
            "message":"Missing credentials in config",
            "code":"CredentialsError",
            "time":"2015-07-28T22:59:10.763Z",
            "originalError":
                {
                    "message":"Could not load credentials from any providers",
                    "code":"CredentialsError",
                    "time":"2015-07-28T22:59:10.763Z",
                    "originalError":
                        {
                            "message": "Connection timed out after 1000ms",
                            "code":"TimeoutError",
                            "time":"2015-07-28T22:59:10.762Z"
                        }
                }
        }

To solve this I tried hard-coding the credentials using 为了解决这个问题,我尝试使用硬编码凭证

AWS.config.update

Which doesn't seem to work as well. 哪个似乎不起作用。

What could the reason be for the error? 错误的原因是什么?

Thanks all! 谢谢大家!

Just incase this affects anyone else, setting the config before initialising AWS.S3() fixed it for me. 只是因为这会影响其他人,在初始化AWS.S3()之前设置配置为我修复了它。 AWS.config.loadFromPath('./config.json'); var s3Stream = require('s3-upload-stream')(new AWS.S3());

I resolved mine by changing the user in my aws config file from a specific user to [default]. 我通过将aws配置文件中的用户从特定用户更改为[default]来解决了我的问题。

$nano .aws/credentials

[default]
aws_access_key_id = xyz
aws_secret_access_key = xyz

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

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