代码之家  ›  专栏  ›  技术社区  ›  Ivan Tarskich

从google app engine切换到firebase托管会导致google文件存储的cors错误

  •  0
  • Ivan Tarskich  · 技术社区  · 6 年前

    从app engine切换到firebasehosting后,对于本地主机和生产环境中app engine版本上载的每个文件,我都会收到以下错误:

    Access to fetch at 'https://storage.googleapis.com/BUCKETNAME/uploads%2Fcompress%40_1566757062038_IMG201906.webp?GoogleAccessId=PROJECTID%40appspot.gserviceaccount.com&Expires=16730323200&Signature=SIGNATURE' from origin 'https://DOMAINNAME' has been blocked by CORS policy: 
    No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
    

    bucked有以下cors设置:

    [{"maxAgeSeconds": 3600, "method": ["GET", "HEAD", "DELETE"], "origin": ["*"]}]
    

    所有文件都是通过在后端生成的签名url请求的,如下所示:

    const config = {
       action: 'read',
       expires: '03-01-2500',
    };
    const [thumbFileUrl] = await bucket.file(PATH).getSignedUrl(config);
    

    前端的请求由

    <img [src]="upload.DownloadUrl" crossorigin="anonymous" >
    

    新的上传工作正常。只是开关前面的那些会导致错误。

    也可以在firebase主机配置中设置

    "hosting": {
    "public": "dist/browser",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ],
    "headers": [
      {
        "source": "**/*.@(eot|otf|ttf|ttc|woff|font.css)",
        "headers": [
          {
            "key": "Access-Control-Allow-Origin",
            "value": "*"
          }
        ]
      },
      {
        "source": "**/*.@(bmp|cur|gif|ico|jpe?g|png|svgz?|webp)",
        "headers": [
          {
            "key": "Access-Control-Allow-Origin",
            "value": "*"
          }
        ]
      }
    ]
    }
    
    0 回复  |  直到 6 年前