2014-10-14 2 views
1

노드 -gcloud https://github.com/GoogleCloudPlatform/gcloud-node을 사용하여 Google Cloud Storage와 상호 작용합니다.Node.js node-gcloud 동시 호출

클라이언트에 작은 API 세트를 제공하기 위해 node.js 서버 (내 첫 번째 node.js 프로젝트)를 개발 중입니다. 기본적으로 사용자가 파일을 업로드하면 API 호출은 서명 된 URL을 반환하여 해당 파일을 표시합니다.

getSignedUrl 함수는 비동기식 https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.8.1/storage?method=getSignedUrl이며 다른 함수에서 결과를 반환 할 수있는 방법을 찾을 수 없습니다.

블루 버드 (Bluebird) 약속으로 게임을 시작했지만 그 지점까지 갈 수 없습니다. 여기 내 코드입니다 :

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 : {_bitField : 0,,536

는 내가 무엇을 얻을하면이 출력됩니다_fulfillmentHandler0 : 정의되지 않은, _rejectionHandler0 : 정의되지 않은, _promise0 : 정의되지 않은, _receiver0 : 정의되지 않은, _settledValue : 정의되지 않은, _boundTo : 정의되지 않은} }

, 그것을 제거하고

return JSON.stringify({url: signed_url, expires: expires}); 
를 유지하는 경우

이전처럼 정의되지 않습니다. 내가 뭘 놓치고 있니?

답변

2

어떤 점하십시오 new Promise(function(res, rej){ … }) 해결 콜백 내부

  • , 당신은 실제로resolve() 또는 reject() (비동기) 호출하지 return 아무것도 필요합니다.
  • resolve 아무 것도 반환하지 않습니다. 약속의 결과를 반환하는 "대기"작업처럼 사용하는 것처럼 보였지만 불가능합니다. 약속은 여전히 ​​비동기입니다.
  • 실제로는 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}); 
    }); 
} 
과 같아야합니다