CORS问题尝试使用Filepond编辑文件时发生。

huangapple go评论42阅读模式
英文:

CORS issue when try to edit files with Filepond

问题

以下是代码部分的翻译:

codesandbox链接: https://codesandbox.io/s/muddy-firefly-vfxmib?file=/src/App.tsx

我能够通过https://github.com/pqina/filepond使用预签名URL将图像上传到S3。然而,当我尝试使用Filepond编辑图像时,浏览器会给我CORS错误。S3存储桶是私有的,并位于Amazon CloudFront之后。

在上面的codesandbox中,您可以看到它在以下情况下可以正常工作:

export default function App() {
  const fileLinks = [
    "https://cdn.shopify.com/s/files/1/0466/8784/6560/products/HALFCHAINCHARMRING_1_160x.jpg"
  ];
  return (
    <div className="App">
      <FormikEditFilesField name="productImageUrls" fileLinks={fileLinks} />
    </div>
  );
}

但是,如果您将上面的文件链接替换为我的图像链接"https://alpha.assets.bimelody.com/images/movintnyc/VD33744WHITE_4_1000x.webp",将会出现CORS错误。您可以在浏览器中打开此链接而没有问题。

还要提到的是,当我在本地运行React应用程序时,显示图像正常工作。只是尝试使用Filepond加载图像不起作用。这一部分让我感到困惑,因为如果是CORS问题,那么为什么在本地主机上可以显示图像,但在Filepond编辑时却不起作用?我是否在API或存储桶中漏掉了一些配置?

更新

看起来我漏掉了在CORS配置中允许HEAD头。

更新后,它可以正常工作。我的最终存储桶CORS配置如下:

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET",
            "POST",
            "PUT",
            "HEAD",
            "DELETE"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": []
    }
]

如果您遇到相同的问题,请根据以下链接检查您的CloudFront和S3配置:

此外,Chrome存在可能会导致仅在Chrome上出现CORS问题的缓存问题,可以使用以下方式解决:

fetch(myRequest, {
  method: 'GET',
  mode: 'cors',
  cache: 'no-store',
})
英文:

codesandbox link: https://codesandbox.io/s/muddy-firefly-vfxmib?file=/src/App.tsx

I'm able to upload images to S3 through presighed URL with https://github.com/pqina/filepond. However, when I try to edit images with Filepond, the browser gave me CORS error. The S3 bucket is private and behind Amazon CloudFront.

In above codesandbox, you can see that it works fine with:

export default function App() {
  const fileLinks = [
    &quot;https://cdn.shopify.com/s/files/1/0466/8784/6560/products/HALFCHAINCHARMRING_1_160x.jpg&quot;
  ];
  return (
    &lt;div className=&quot;App&quot;&gt;
      &lt;FormikEditFilesField name=&quot;productImageUrls&quot; fileLinks={fileLinks} /&gt;
    &lt;/div&gt;
  );
}

However, if you replace above file link with my image link "https://alpha.assets.bimelody.com/images/movintnyc/VD33744WHITE_4_1000x.webp", an CORS error will get thrown. You can open this link in browser with no problem.

Also want to mention that when I run the React app locally, displaying the images work fine. It is just try to load the images with Filepond doesn't work. This part confuses me as if it is a CORS problem, then why it works in localhost for displaying the images, but not for Filepond edit ?

Do I miss some configuration in my API or bucket?

Update

Looks like I missed allowing HEAD header in the CORS configuration.

After update, it works. And my final bucket cors is:

[
    {
        &quot;AllowedHeaders&quot;: [
            &quot;*&quot;
        ],
        &quot;AllowedMethods&quot;: [
            &quot;GET&quot;,
            &quot;POST&quot;,
            &quot;PUT&quot;,
            &quot;HEAD&quot;,
            &quot;DELETE&quot;
        ],
        &quot;AllowedOrigins&quot;: [
            &quot;*&quot;
        ],
        &quot;ExposeHeaders&quot;: []
    }
]

If you encountered same problem, check your cloudfront and S3 configuration based on these links:

Also, Chrome has caching issue that may cause Chrome-only CORS issue, which can be addressed with:

fetch(myRequest, {
  method: &#39;GET&#39;,
  mode: &#39;cors&#39;,
  cache: &#39;no-store&#39;, 
})

答案1

得分: 1

S3配置错误。此问题已经解决。

请查看 https://stackoverflow.com/a/70780987/6323902

英文:

Wrong S3 configuration. This problem is already resolved.

Please check https://stackoverflow.com/a/70780987/6323902

huangapple
  • 本文由 发表于 2023年2月16日 05:56:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定