【问题标题】:"Missing credentials in config" while trying to stream a file to Amazon S3 bucket in node.js尝试将文件流式传输到 node.js 中的 Amazon S3 存储桶时出现“配置中缺少凭据”
【发布时间】:2017-02-06 23:23:36
【问题描述】:

我尝试将基本文件流设置到 Amazon S3。 我复制并粘贴了示例 https://www.npmjs.com/package/s3-upload-stream

并稍作修改(如附件)。

   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);

        })

    }

我的配置文件是这样设置的:

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

代码返回如下错误:

    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"
                        }
                }
        }

为了解决这个问题,我尝试使用对凭据进行硬编码

AWS.config.update

这似乎也不起作用。

错误的原因可能是什么?

谢谢大家!

【问题讨论】:

  • 可能是 s3-upload-stream 中的错误,或者它与您使用的 aws-sdk 版本不兼容?对 s3-upload-stream 项目的最后一次更改似乎是 8 个月前的事情,因此您可能希望通过从该日期下载并使用相关版本的 aws-sdk 进行验证,例如 github.com/aws/aws-sdk-js/releases/tag/v2.0.23
  • 好的!对我来说很有意义!您建议我使用什么将文件流式传输到支持更新版本的 S3?
  • 我会首先尝试让该流库正常工作并通过 GitHub 报告任何错误,因为它可能很容易修复(假设它实际上是一个错误)。我还想看看 aws-sdk 有什么流媒体功能,例如,我在docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-examples.html 看到了一个文件上传流媒体示例(搜索 createReadStream)。
  • 实际上那里的例子似乎非常有用,但它们似乎无法让您定义要使用的夹头尺寸。
  • 您可以使用托管上传器配置各种选项,请参阅blogs.aws.amazon.com/javascript/post/Tx3EQZP53BODXWF/… 上的“缓冲 10 兆字节块并将并发减少到 2”示例。不是 100% 确定这适用于流媒体,但值得研究。

标签: node.js amazon-web-services amazon-s3


【解决方案1】:

以防这会影响其他任何人,在初始化 AWS.S3() 之前设置配置为我修复了它。 AWS.config.loadFromPath('./config.json'); var s3Stream = require('s3-upload-stream')(new AWS.S3());

【讨论】:

    【解决方案2】:

    我通过将我的 aws 配置文件中的用户从特定用户更改为 [默认] 来解决我的问题。

    $nano .aws/credentials
    
    [default]
    aws_access_key_id = xyz
    aws_secret_access_key = xyz
    

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 2019-10-14
      • 1970-01-01
      • 2015-05-05
      • 2014-12-04
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 2018-04-04
      相关资源
      最近更新 更多