preSigned URLS Promise TypeError:res.json不是NodeJS响应错误的函数

时间:2019-11-05 19:25:19

标签: javascript node.js amazon-s3 pre-signed-url

我正在尝试为存储在我的私有s3存储桶中的文件创建一个预签名的url数组,但是由于res.json函数错误,我似乎无法返回它们。尽管存在相同问题的线程数量众多,但我无法弄清楚。我的代码有什么问题?

const express = require('express');
const AWS = require('aws-sdk');
let config = require('./config');
let app = express();

var s3 = new AWS.S3();
s3.config.update({
    accessKeyId: config.accessKeyId,
    secretAccessKey: config.secretAccessKey,
    region: 'us-east-2'
});



// Create an array that houses pre-signed Urls
// Loop through x amt of times (depending on number of documents)
// Push each url into an array called 'preSignedUrls'
// Exit loop and reset array to re-generate new urls
var router = express.Router();


function getSignedURL(res) {
    Key = ['file1.pdf', 'file2.pdf'];
    let promises = [];
    let i = 0;
    while(i < Key.length){

        var params = {
            Bucket: 'bucketname',
            Key: Key[i],
            Expires: 36000
        };

        promises.push(s3.getSignedUrlPromise('getObject', params));
        console.log(promises)
        i++;
    }
    Promise.all(promises)
    .then( response => {
        res.json(response);
    }, function(err) { 
    console.log(err);
    });

}


router.route('/get_signed_url')
.get(getSignedURL);
//                 if (err) {
    // console.log(err);
    // return next(err);

router.route('/get_signed_url')
app.use('/v1', router);

var PORT = process.env.PORT || 3001;
var HOST = process.env.HOST || '11.123.346.789';

console.log("Listening on", HOST, PORT);
app.listen(PORT, HOST);

这是我得到的: (node:6717) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 5): TypeError: res.json is not a function

1 个答案:

答案 0 :(得分:0)

请求控制器的签名是def generate(rows): for row in rows: yield json.dumps(dict(row), default=lambda x: str(x)) async def get(url, row, dag_run_id, bucket): async with aiohttp.ClientSession() as session: async with session.post(url, params={'bucket': bucket, 'dag_run_id': dag_run_id}, json=row) as response: return await response.read() rows = bq.query(f'select * from table') gen = generate(rows) loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.gather(*[get(url, row, dag_run_id, staging_bucket) for row in gen])) ,响应对象是第二个,而不是第一个。