Firebase函数(node.js)从存储中返回图像的下载URL(通过文件名)

时间:2018-06-05 18:08:36

标签: firebase google-cloud-functions firebase-storage

如何编写Firebase http函数,该函数返回可以下载图像的网址(纯文本格式)?

我使用的是最新版本的Firebase Admin SDK。 我在默认存储桶中有一个名为exports.getImageUrl = functions.https.onRequest((request, response) => { return firebase.storage().bucket().file("images/thumbnail.png").getSignedUrl({ action: 'read', expires: '03-09-2491' }) .then(signedUrls => { return response.send(signedUrls[0]) }) .catch(error => { return response.status(500).send(error) }) }) 的文件,我想通过HTTP调用返回URL,我想出了这个代码:

callback is not a function
    at /user_code/node_modules/firebase-admin/node_modules/@google-cloud/storage/src/file.js:1784:7
    at Request._callback (/user_code/node_modules/firebase-admin/node_modules/google-auto-auth/index.js:362:9)

但是失败了:

exports.getImage = functions.https.onRequest((request, response) => {
  console.log("start")
  const storage = firebase.storage()
  console.log(storage)
  const bucket = storage.bucket()
  console.log(bucket)
  const file = bucket.file("buildInExercises/bb_4zigzagcushionblue_thumbnail.png")
  console.log(file)
  return file.getSignedUrl({
    action: 'read',
    expires: '03-09-2491'    
  })
  .then(urls => {
    return response.send(urls)
  })
  .catch(error => {
    return response.status(500).send(error)
  })
})

我做错了什么?

修改

我在这里详细添加了日志记录:

{
  "message": "Permission iam.serviceAccounts.signBlob is required to perform this operation on service account projects/snookercoachapp-dev/serviceAccounts/snookercoachapp-dev@appspot.gserviceaccount.com."
}

我现在得到了正确的答复:

function arb_encode($num, $basestr) {
    $base = strlen($basestr);
    $rep = '';
    while($num > 0) {
        $rem = $num % $base;
        $rep = $basestr[$rem] . $rep;
        $num = ($num - $rem) / $base;
    }
    return $rep;
}

function arb_decode($num, $basestr) {
    $base = strlen($basestr);
    $dec = 0;
    $num_arr = str_split((string)$num);
    $cnt = strlen($num);
    for($i=0; $i < $cnt; $i++) {
        $pos = strpos($basestr, $num_arr[$i]);
        if( $pos === false ) {
            Throw new Exception(sprintf('Unknown character %s at offset %d', $num_arr[$i], $i));
        }
        $dec = ($dec * $base) + $pos;
    }
    return $dec;
}

$alpha = implode('', range('A', 'Z'));
var_dump(
    str_pad( arb_encode(1234, $alpha), 4, 'A', STR_PAD_LEFT),
    arb_decode('BUTT', $alpha)
);

接缝我有权限问题吗?

1 个答案:

答案 0 :(得分:2)

错误消息是getSignedUrl()仅在使用服务帐户初始化管理员SDK时才有效的面部症状。这意味着您不能依赖于在没有参数的情况下调用admin.initializeApp()时使用的默认凭据。