程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了无法从 react.js 获取谷歌云存储桶图像大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决无法从 react.js 获取谷歌云存储桶图像?

开发过程中遇到无法从 react.js 获取谷歌云存储桶图像的问题如何解决?下面主要结合日常开发的经验,给出你关于无法从 react.js 获取谷歌云存储桶图像的解决方法建议,希望对你解决无法从 react.js 获取谷歌云存储桶图像有所启发或帮助;
    componentDIDMount(){
        let default_image = localstorage.getItem("default_image");
        const url = 'https://storage.GoogleAPIs.com/smart-staging-bucket/tryon/images/default/2021-03-14/face.jpg'
        const filename = 'myfile.jpg'
        fetch(url)
        .then(async response => {
            const ContentType = response.headers.get('content-type')
            const blob = await response.blob()
            const file = new file([blob],filename,{ ContentType })
            this.setState({
            default_image: file
            })
        })
    }

在这里,我想从谷歌云存储桶中获取图像。 但是,我遇到了以下错误。有什么办法可以解决吗?

    Access to fetch at 'https://storage.GoogleAPIs.com/smart-staging-bucket/tryon/images/default/2021-03-14/face.jpg' from origin
    'http://localhost:3000' 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.

解决方法

同源政策阻止 fetch 查询图像。您需要为您的云存储存储桶设置跨域资源共享 (CORS) - 请参阅 docs。

例如

echo '[{ "origin": ["https://your-site.com"],"method": ["GET"] }]' >cors.json
gsutil cors set cors.json gs://your-bucket

大佬总结

以上是大佬教程为你收集整理的无法从 react.js 获取谷歌云存储桶图像全部内容,希望文章能够帮你解决无法从 react.js 获取谷歌云存储桶图像所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: