Node.js node-gcloud 同步调用

标签 node.js promise bluebird gcloud-node

我正在使用node-gcloud https://github.com/GoogleCloudPlatform/gcloud-node与 Google Cloud Storage 交互。

我正在开发一个 Node.js 服务器(我的第一个 Node.js 项目)来向客户端提供一小组 API。基本上,当用户上传文件时,API 调用会返回签名的 url 以显示该文件。

getSignedUrl 函数是异步的 https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.8.1/storage?method=getSignedUrl我找不到从另一个函数返回该结果的方法。

我已经开始考虑 Bluebird 的 promise ,但我无法捕获重点。这是我的代码:

var _signedUrl = function(bucket,url,options) {
  new Promise(function (resolve, reject) {
    var signed_url
    bucket.getSignedUrl(options, function(err, url) {
      signed_url = err || url;
      console.log("This is defined: " + signed_url)

      return signed_url   
    })
  })
}


var _getSignedUrl = function(url) {
  new Promise(function(resolve) {
    var   options = config.gs
      ,   expires = Math.round(Date.now() / 1000) + (60 * 60 * 24 * 14)
      ,   bucket  = project.storage.bucket({bucketName: config.gs.bucket, credentials: config.gs })
      ,   signed_url = null

    options.action  = 'read'
    options.expires =  expires// 2 weeks.
    options.resource= url
    signed_url = resolve(_signedUrl(bucket,url,options))

    console.log("This is undefined: " + signed_url)

    return JSON.stringify( {url: signed_url, expires: expires} );

  });
}

我认为我缺少它应该如何工作的基础知识,因此任何提示将不胜感激。

编辑:

我已经针对第一条评论重新设计了我的解决方案:

getSignedUrl: function() {
    var   options = config.gs
      ,   expires = Math.round(Date.now() / 1000) + (60 * 60 * 24 * 14)
      ,   bucket  = project.storage.bucket({bucketName: config.gs.bucket, credentials: config.gs })
      ,   signed_url = null

    options.action  = 'read'
    options.expires =  expires// 2 weeks.
    options.resource= this.url

    Promise.promisifyAll(bucket);

    return bucket.getSignedUrlAsync(options).catch(function(err) {
        return url; // ignore errors and use the url instead
    }).then(function(signed_url) {
        return JSON.stringify( {url: signed_url, expires: expires} );
    });
}

我不清楚双返回应该如何工作,但如果我保留 返回桶

我得到的是这个输出:

{ 网址: { _bitField: 0, _fulfillmentHandler0:未定义, _rejectionHandler0:未定义, _promise0:未定义, _receiver0:未定义, _settledValue:未定义, _boundTo:未定义} }

,如果删除它并保留

return JSON.stringify( {url: signed_url, expires: expires} );

我像以前一样未定义。我错过了什么?

最佳答案

几点:

  • new Promise(function(res, rej){ … }) 内解析器回调,您实际上需要调用 resolve()reject() (异步),而不是 return任何东西。
  • resolve不返回任何内容。您似乎将它用作返回 promise 结果的“等待”操作,但这是不可能的。 Promise 仍然是异步的。
  • 实际上,您永远不需要调用 new Promise根本不。使用Promisification相反。

你的代码应该看起来像

var gcloud = require('gcloud');
Promise.promisifyAll(gcloud); // if that doesn't work, call it once on a
                              // specific bucket instead

function getSignedUrl(url) {
    var options = config.gs,
        expires = Math.round(Date.now() / 1000) + (60 * 60 * 24 * 14),
        bucket  = project.storage.bucket({bucketName: config.gs.bucket, credentials: config.gs });

    options.action  = 'read';
    options.expires =  expires; // 2 weeks.
    options.resource = url;
    return bucket.getSignedUrlAsync(options).catch(function(err) {
        return url; // ignore errors and use the url instead
    }).then(function(signed_url) {
        console.log("This is now defined: " + signed_url);
        return JSON.stringify( {url: signed_url, expires: expires} );
    });
}

关于Node.js node-gcloud 同步调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26357837/

相关文章:

javascript - 如何检查对象是否是 Promise?

node.js - 将回调转换为 thunk

node.js - react native : Cannot read property 'RNFSFileTypeRegular' of undefined

ios - 从 iPhone 应用程序 Expressjs/Nodejs 后端发送 JSON 发布数据

JavaScript Promise 函数返回得太早

javascript - 如何在 Javascript/Jquery 中对失败的 ajax 请求返回一个新的 promise 而不是错误?

promise - Firestore 事务产生控制台错误 : FAILED_PRECONDITION: the stored version does not match the required base version

node.js - 如何启动expressjs(nodejs)作为生产模式

javascript - 将字符串连接到 Promise

javascript - Bluebird promise 和别名