【问题标题】:Node.js node-gcloud synchronous callNode.js node-gcloud 同步调用
【发布时间】:2014-10-14 09:55:17
【问题描述】:

我正在使用 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} );

我和以前一样不确定。我错过了什么?

【问题讨论】:

    标签: node.js promise bluebird gcloud-node


    【解决方案1】:

    几点:

    • new Promise(function(res, rej){ … }) 解析器回调中,您实际上需要调用 resolve()reject()(异步),而不是return 任何东西。
    • resolve 不返回任何内容。您似乎将它用作返回承诺结果的“等待”操作,但这是不可能的。 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} );
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-06
      • 2013-10-16
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 2014-06-13
      • 1970-01-01
      • 2013-05-11
      相关资源
      最近更新 更多