跨源策略错误

Cross Origin Policy error

本文关键字:错误 策略      更新时间:2023-09-26

我正在从S3读取图像,并且正在使用angularjs,当我打开页面时出现错误

Image from origin 'https://feazt-static.s3.amazonaws.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8000' is therefore not allowed access. 

当我再次重新加载页面时,图像正在正确加载。请帮助我。

我实现的代码:

$timeout(function() {
        var canvas = document.createElement("canvas");
        var context = canvas.getContext("2d");
        var img_obj = document.getElementById("img_file_"+index);
        img_obj.addEventListener('load', function() {
            canvas.width = this.width*2;
            canvas.height = this.height*2;
            context.drawImage(this, 0, 0, canvas.width, canvas.height);
            this.setAttribute("src",canvas.toDataURL()); 
        });
        img_obj.setAttribute('crossOrigin', 'anonymous');
        img_obj.setAttribute("src",arg);
    }, 2000);

您需要在存储桶上启用它,然后一切都很好。

遵循亚马逊官方指南:

http://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html