无法解决Cloud Function中的getSignedUrl承诺 [英] Can't resolve getSignedUrl Promise in Cloud Function

查看:55
本文介绍了无法解决Cloud Function中的getSignedUrl承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试调试Google Cloud Function,该函数应返回一个signedUrl,用于将文件上传到Google Cloud Storage.

I have been trying to debug a Google Cloud Function that should return a signedUrl for uploading a file to Google Cloud Storage.

我有一个https云功能:

I have an https cloud function:

exports.gcpSecureURL = functions.https.onCall((data, res) => {

    const bucketName = data.bucket;
    const fileName = data.filename;

    let signedUrl = generateV4UploadSignedUrl(bucketName, fileName)
        .then(value => {
            console.log("VALUE: ", value);
            return value
        })

    res.send(signedUrl) 
        
});

永远不会达到值,并且signedUrl始终是一个承诺.

Value is never reached and signedUrl is always a Promise.

在generateV4UploadSignedUrl函数中,我具有:

In the generateV4UploadSignedUrl function I have:

async function generateV4UploadSignedUrl(bucketName, fileName) {
    // These options will allow temporary uploading of the file with outgoing
    // Content-Type: application/octet-stream header.
    const options = {
      version: 'v4',
      action: 'write',
      expires: Date.now() + 15 * 60 * 1000, // 15 minutes
      contentType: 'application/octet-stream',
    };
  
    // Get a v4 signed URL for uploading file
    const [url] = await storage
      .bucket(bucketName)
      .file(fileName)
      .getSignedUrl(options)
      
    return url;
  };

param bucketName是正确的,而fileName只是我要上传的文件的名称,因此在生成url的阶段应该没关系吗?

The param bucketName is correct and fileName is just the name of the file I want to upload so at the stage where I generate the url should not matter?

我不明白为什么signatureUrl()方法根本不返回任何东西.我将我的存储桶上的权限更改为AllUsers的StorageAdmin,并且我的功能设置为允许未经身份验证.

I don't get why the signedUrl() method is not returning anything at all. I changed the permission on my bucket to StorageAdmin for AllUsers and my function is set to allow unauthenticated.

在云功能日志中,generateV4UploadSignedUrl中存在TypeError:

There is a TypeError in generateV4UploadSignedUrl in the cloud function logs:

TypeError: (intermediate value) is not iterable
at generateV4UploadSignedUrl (/workspace/index.js:51:19)

我不知道发生了什么,功能日志似乎非常不透明.请问我在做什么错?

I have no idea what is happening and the function logs seem pretty opaque. What am I doing wrong please?

推荐答案

您在这里有两个问题.

首先,您正在使用可调用类型函数,但是您似乎就像编写 HTTP函数一样使用它.如果您想编写一个可调用的函数,请务必查看该文档.它们的工作方式不同于HTTP函数.使用可调用函数,您必须返回一个带有数据的promise,以发送给调用方.使用HTTP函数,您可以在所有异步工作完成后调用 res.send()或等效方法来发送响应.确保您了解两者之间的区别.

First, you're using a callable type function, but you appear to be using it as if you are writing an HTTP function. If you want to write a callable function, please be sure to review the documentation for that. They work differently than HTTP functions. With callable functions, you must return a promise with the data to send to the caller. With HTTP functions, you invoke res.send() or equivalent to send a response after all the async work is complete. Be sure you understand the difference between the two.

永远不会达到值,并且signedUrl始终是一个承诺.

Value is never reached and signedUrl is always a Promise.

signedUrl 始终是一个承诺,因为 generateV4UploadSignedUrl async ,并且所有 async 函数始终返回一个诺言.您必须等待诺言,才能从诺言中获得已解决的价值.

signedUrl is always a promise because generateV4UploadSignedUrl is async, and all async functions always return a promise. You will have to await the promise to get the resolved value from the promise.

您的函数应使用async/await,并且看起来像这样:

Your function should use async/await, and look more like this:

exports.gcpSecureURL = functions.https.onCall(async (data, context) => {

    const bucketName = data.bucket;
    const fileName = data.filename;

    let signedUrl = await generateV4UploadSignedUrl(bucketName, fileName)
    console.log("VALUE: ", signedUrl);
    return signedUrl
        
});

尽管这可能无法完全满足您的要求(请确保 signedUrl 的全部内容就是您要发送给客户端的内容),但它应该使您更接近所需的内容.

While this might not do exactly what you want (be sure that the entire contents of signedUrl is what you want to send to the client), it should get you closer to what you want.

这篇关于无法解决Cloud Function中的getSignedUrl承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆