0

我正在尝试创建一个使用 minio 服务器的 openwhisk 操作。为此,我必须将我的操作打包为 nodeJs 模块,因为 openwhisk 不支持 minio。我的 index.js 文件如下:

function myAction(){
    const minio = require("minio")
    const minioClient = new minio.Client({
        endPoint: 'myIP',
        port: 9000,
        secure: false,
        accessKey: '###########',
        secretKey: '###########'
    });

    minioClient.listBuckets(function(err, buckets) {
        if (err) return console.log(err)
        return {payload: buckets}
    })

}

exports.main = myAction;

当我调用此操作时,我得到 {}。你有什么想法为什么会发生这种情况?关于如何解决它的任何建议?

4

1 回答 1

2

如果您正在异步执行某项操作,OpenWhisk 操作期望您返回一个 Promise 。

在您的情况下,您需要构造一个在listBuckets方法完成后解决的承诺(通常这意味着:您需要在回调中解决它)。

function myAction(){
    const minio = require("minio")
    const minioClient = new minio.Client({
        endPoint: 'myIP',
        port: 9000,
        secure: false,
        accessKey: '###########',
        secretKey: '###########'
    });

    return new Promise(function(resolve, reject) {
        minioClient.listBuckets(function(err, buckets) {
            if (err) {
                reject(err);
            } else {
                resolve({payload: buckets});
            }
        });
    });
}

exports.main = myAction;

(未经测试的代码)。

于 2018-03-08T09:58:53.357 回答