【问题标题】:Saving Image to gcloud with firebase cloud function使用 firebase cloud 功能将图像保存到 gcloud
【发布时间】:2019-07-27 02:27:12
【问题描述】:

我想将 unsplash 中的选定图像保存在云功能中 - 以便在后台运行。这是我的代码:

export const getUnsplashCron = functions
  .region('europe-west1')
  .runWith({ memory: '512MB', timeoutSeconds: 15 })
  .https.onRequest(async(req, res) => {

    if (!req.query.url) {
      res.status(300).send('no url');
    }

    return request({
      url: req.query.url + '?client_id=' + environment.unsplash.appId,
      encoding: null
    }, (err, response, body) => {
      if (!err && response.statusCode === 200) {

        const stream = require('stream');
        const bufferStream = new stream.PassThrough();

        bufferStream.end(Buffer.from(body, 'base64'));

        const file = admin.storage().bucket().file('name.jpg');

        bufferStream.pipe(
          file.createWriteStream({
            public: true
          }))
          .on('error', function(error) {
            console.log(error);
            return res.status(500).send(error)
          })
          .on('finish', function() {

            const config: GetSignedUrlConfig = {
              action: 'read',
              expires: '01-01-2025'
            };
            file.getSignedUrl(config, function(error, url) {
              console.log(url);
              if (error) {
                return res.status(500).send(error);
              }
              return res.status(500).send(url);
            });

          });


      }
    });

  });

但我得到了一个

错误:未找到 在 Util.parseHttpRespBody (/srv/node_modules/@google-cloud/common/build /src/util.js:172:38) 在 Util.handleResp (/srv/node_modules/@google-cloud/common/build/src/util.js:116:117) 在 retryRequest (/srv/node_modules/@google-cloud/common/build/src/util.js:404:22) 在 onResponse (/srv/node_modules/retry-request/index.js:200:7) 在 /srv/node_modules/teeny-request/build/src/index.js:158:17 在 在 process._tickDomainCallback (internal/process/next_tick.js:229:7) 代码:404, 错误: [ { 域:'global',原因:'notFound',消息:'未找到'}], 响应:未定义, 消息:'未找到'}

我做错了什么?!

【问题讨论】:

    标签: node.js google-cloud-functions gcloud


    【解决方案1】:

    您应该使用 Promise 来处理异步任务(例如对 unsplash 的 HTTP 调用,或对 Cloud Storage 的写入)。默认情况下request 不返回承诺,因此您需要使用接口包装器进行请求,例如request-promise

    因此,您应该如下修改您的代码:

    const rp = require('request-promise');
    .....
    
    export const getUnsplashCron = functions
      .region('europe-west1')
      .runWith({ memory: '512MB', timeoutSeconds: 15 })
      .https.onRequest(async(req, res) => {
    
      .......
    
      const options = {
        uri: req.query.url + '?client_id=' + environment.unsplash.appId
      };
    
      rp(options)
        .then(response => {
          .... //Upload your file
          res.send('Success');
        })
        .catch(err => {
          // API call failed...
          res.status(500).send('Error': err);
        });
    };
    

    您可以观看此官方视频系列了解更多详情:https://firebase.google.com/docs/functions/video-series/(尤其是标题为“Learn JavaScript Promises”的 3 个视频)。

    【讨论】:

    • @Coach 你好,你有时间看这个答案吗?
    猜你喜欢
    • 2020-03-05
    • 2019-09-21
    • 2019-06-23
    • 2021-04-21
    • 2018-06-05
    • 1970-01-01
    • 2021-10-28
    • 2019-01-10
    • 2021-03-14
    相关资源
    最近更新 更多