"Missing credentials in configuration" when trying to stream a file to Amazon S3 bucket in node.js

I tried to set up a basic file stream on Amazon S3. I copied and pasted the example at https://www.npmjs.com/package/s3-upload-stream

And changed it a bit (as indicated).

   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 set up like this:

{
 "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 problem, I tried to hardcode the credentials using

AWS.config.update

      

Which doesn't seem to work.

What is the reason for this error?

Thanks everyone!

+3


source to share


2 answers


It just might affect anyone else by setting the config before initializing AWS.S3 (), fixing it for me. AWS.config.loadFromPath('./config.json'); var s3Stream = require('s3-upload-stream')(new AWS.S3());



+1


source


I resolved mine by changing the user in my aws config file from specific user to [default].



$nano .aws/credentials

[default]
aws_access_key_id = xyz
aws_secret_access_key = xyz

      

0


source







All Articles