将多个文件上载到S3节点

时间:2017-02-11 08:32:48

标签: node.js amazon-s3 promise sails.js

我正在尝试从我的S3应用程序将多个文件上传到Sails.js。我有一个控制器来处理请求,如果多个控制器想要使用该功能,则需要上传Service。控制器中的承诺无法解决。而且,我一直收到此错误未处理拒绝TimeoutError:配置中缺少凭据

控制器

multiSizeCreate: (req, res) => {

    UploadService.uploadFile(req).then((data, failure) => {
        if (failure) return [null];
        return [Promise.resolve(AWSService.s3awsupload(data))];<-- <--The promise doesn't get resolved
    })
    .spread(uploadedData => {
        if (!uploadedData) return ResponseService.json(403, res, 'File upload failed');
        console.log(uploadedData); <--It logs an unresolved promise
        return ResponseService.json(201, res, 'Size(s) created successfully');
    })
    .catch(err => ValidationService.jsonResolveError(err, Size, res))
}

AWSService

const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const fs = require('fs');
const key = sails.config.settings.aws.key;
const secret = sails.config.settings.aws.secret;
const bucket = sails.config.settings.aws.bucket;
_ = require('lodash');
AWS.config.update({accessKeyId: key, secretAccessKey: secret});
const Promise = require('bluebird');

module.exports = {

    s3awsupload: (files) => {

        let toUpload = []
        if (files.length > 0) {
            _.forEach(files, file => {
                toUpload.push(awsmainUpload(file));
            });
            return [toUpload];
        } else {
            return null;
        }
    }
}

const awsmainUpload = (fileDirectory) => {

    const uploadImage = fs.createReadStream(fileDirectory.fd);
    const fileName = Math.floor(Date.now() / 1000);
    const params = {Bucket: bucket, Key: fileName + " " + fileDirectory.fileName, Body: uploadImage};

    return new Promise((resolve, reject) => {
        s3.upload(params, (error, outpout) => {
            return error ? reject(error) : resolve(outpout);
        })
    })
}

0 个答案:

没有答案