Node.js Node gcloud同步调用
我正在使用node gcloud与谷歌云存储交互 我正在开发一个node.js服务器(我的第一个node.js项目),为客户端提供一组小的API。基本上,当用户上传文件时,API调用返回签名的url以显示该文件 getSignedUrl函数是异步的,我找不到从另一个函数返回结果的方法 我已经开始玩弄蓝鸟的承诺,但我还没说到点子上。这是我的密码:Node.js Node gcloud同步调用,node.js,promise,bluebird,gcloud-node,Node.js,Promise,Bluebird,Gcloud Node,我正在使用node gcloud与谷歌云存储交互 我正在开发一个node.js服务器(我的第一个node.js项目),为客户端提供一组小的API。基本上,当用户上传文件时,API调用返回签名的url以显示该文件 getSignedUrl函数是异步的,我找不到从另一个函数返回结果的方法 我已经开始玩弄蓝鸟的承诺,但我还没说到点子上。这是我的密码: var _signedUrl = function(bucket,url,options) { new Promise(function (reso
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} );
});
}
我不清楚双倍回报应该如何运作,但如果我保持
返回铲斗
我得到的是这个输出:
{url:
{u位字段:0,
_fulfillmentHandler0:未定义,
_rejectionHandler0:未定义,
_承诺人0:未定义,
_接收人0:未定义,
_结算值:未定义,
_boundTo:未定义}
}
,如果需要,请将其移除并保留
return JSON.stringify( {url: signed_url, expires: expires} );
我和以前一样没有定义。我遗漏了什么?一些要点:
- 在
resolver回调中,实际上需要调用newpromise(函数(res,rej){…})
或resolve()
(异步),而不是reject()
返回任何内容
不返回任何内容。您似乎将其用作返回承诺结果的“等待”操作,但这是不可能的。承诺仍然是异步的resolve
- 实际上,您根本不需要调用
。改用newpromise
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} );
});
}