上传后无法获得签名的网址

时间:2019-07-11 05:38:00

标签: node.js firebase google-cloud-functions google-cloud-storage

上传文件后,该函数将无法获取已签名的URL。

当file.getSignedUrl()被注释掉时,该功能起作用。

fstream.on('finish', function () {
    var bucket = admin.storage().bucket();
    // Upload a local file to a new file to be created in your bucket.
    bucket.upload(tempFilePath,{ destination: filePath+fileName} ,function (err, file) {
    if (!err) {
    console.log("Uploaded: " + fileName);
    fs.unlinkSync(tempFilePath);
    return file.getSignedUrl({
        action: 'read',
        expires: '03-17-2500'
        }, function(error, url) {
            if (error) {
            console.error(error);
            return callback(new Error('could not get url: ', error))
            }
        console.log("Uploaded url : " + url);
        return callback(null, url)
        })

        //return callback(null, "happy")

        }else{
            console.error("err: " + err);
            return callback(new Error('could not upload'))
        }
    });
});

1 个答案:

答案 0 :(得分:0)

为了了解签名的URL,请检查Google Cloud Storage Documentation

对于具有Node.js getSignedUrl的云功能,您可以use as a reference this example

签名Blob所需的先决条件:

  1. Google Identity and Access Management (IAM) API has to be enabled in the project
  2. 服务帐户需要权限:iam.serviceAccounts。{getAccessToken,signBlob} Cloud functions service account (cloudfunctions.serviceAgent) has this permission