【问题标题】:enabling CORS Google Cloud Function (Python)启用 CORS 谷歌云函数 (Python)
【发布时间】:2021-01-12 04:15:17
【问题描述】:

可以在 Google Cloud Functions 中使用您的 flask_cors 吗?

app = Flask(__name__)
cors = CORS(app)

这个flask_cors 包在本地可以工作,但在部署到 Cloud Functions 时却不能。

我尝试了很多不同的方法,正如 GCP 建议的那样https://cloud.google.com/functions/docs/writing/http 但我仍然收到那个 CORS 错误:

对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。

【问题讨论】:

    标签: python google-cloud-platform google-cloud-functions cors flask-cors


    【解决方案1】:

    不,app 变量在 Cloud Functions 中不可用。

    相反,您可以手动处理 CORS:

    def cors_enabled_function(request):
        # For more information about CORS and CORS preflight requests, see
        # https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
        # for more information.
    
        # Set CORS headers for the preflight request
        if request.method == 'OPTIONS':
            # Allows GET requests from any origin with the Content-Type
            # header and caches preflight response for an 3600s
            headers = {
                'Access-Control-Allow-Origin': '*',
                'Access-Control-Allow-Methods': 'GET',
                'Access-Control-Allow-Headers': 'Content-Type',
                'Access-Control-Max-Age': '3600'
            }
    
            return ('', 204, headers)
    
        # Set CORS headers for the main request
        headers = {
            'Access-Control-Allow-Origin': '*'
        }
    
        return ('Hello World!', 200, headers)
    

    更多详情请见https://cloud.google.com/functions/docs/writing/http#handling_cors_requests

    【讨论】:

    • 谢谢!不过,我将如何将其实现到我的云功能中?我是否在我实际想要运行的函数中调用此函数? def predict(request): cors_enabled_function(request) # do the rest of my function
    • 这里的sn-p 一个云函数(注意它需要一个request参数)。您应该在函数顶部添加这些行。
    • @DustinIngram,如果该功能需要身份验证,此解决方案是否有效?我的意思是,OPTIONS 请求是否必须添加安全标头?
    【解决方案2】:

    如果您已经在使用flask,那么最简单的方法是使用flask-cors

    https://github.com/corydolphin/flask-cors

    像下面这样装饰你的云功能,你就完成了。

    from flask_cors import cross_origin
    
    @cross_origin()
    @json
    def fun_function(request):
        # enter code here
    

    或者您可以根据需要添加任意数量的功能,如下所示。

    from flask_cors import cross_origin
    
    @cross_origin(allowed_methods=['POST'])
    @json
    def fun_function(request):
        # enter code here
    

    【讨论】:

      【解决方案3】:

      @mdev,我遇到了类似的问题,并通过在我的 cors_enabled_function 开头添加预检请求的 CORS 标头来解决它(正如 Dustin Ingram 建议的那样);我在函数结束时留下的主要请求的 CORS 标头(这种方式包括返回语句中的响应,可以是 JSON、文本等)。换句话说,我将我的主要功能代码放在预检和主要 CORS 请求之间。

      【讨论】:

      • 有时英雄无功而返。这对我有用!应该在所有文档中更清楚地解释。
      【解决方案4】:

      云函数中没有APP。您可以按照google cloud documentations 中的说明设置 CORS 标头,并按照您在 Flask 中的编写方式返回您的 JSON。

      下面的示例函数名为hello_world,用于发布请求。它返回CORS 的状态和标题。

      from flask import jsonify
      
      def hello_world(request):
          request_json = request.get_json()
          # Set CORS headers for the preflight request
          if request.method == 'OPTIONS':
              # Allows GET requests from any origin with the Content-Type
              # header and caches preflight response for an 3600s
              headers = {
                  'Access-Control-Allow-Origin': '*',
                  'Access-Control-Allow-Methods': 'POST',
                  'Access-Control-Allow-Headers': 'Content-Type',
                  'Access-Control-Max-Age': '3600'
              }
      
              return ('', 204, headers)
      
          # Set CORS headers for the main request
          headers = {
              'Access-Control-Allow-Methods': 'POST',
              'Access-Control-Allow-Origin': '*'
          }
      
         if request_json and 'labels' in request_json:
              # THIS IS THE PLACE YOU WRITE YOUR CODE.
              # AWLAYS RETURN WITH THE HEADERS AND STATUS
              return (jsonify({"ok": "Great Day 2"}), 200, headers)
      

      【讨论】:

        猜你喜欢
        • 2020-11-21
        • 2019-02-15
        • 2021-05-09
        • 2021-09-02
        • 1970-01-01
        • 2021-03-04
        • 1970-01-01
        • 2018-12-07
        • 2018-10-02
        相关资源
        最近更新 更多