我正在尝试在我的网站上设置一个小的上传部分,供用户上传个人资料图片。我正在使用Google Cloud的Slingshot,并从本地主机进行测试,但是我遇到了以下错误:
OPTIONS https://mybucket.storage.googleapis.com/ net::ERR_INSECURE_RESPONSE
我猜这个错误是由于我的CORS配置引起的,所以我尝试了各种不同的设置,但都没有效果。
这是我最近的CORS设置:
[
{
"origin": ["http://localhost:3000/"],
"responseHeader": ["Content-Type"],
"method": ["GET", "HEAD", "DELETE"],
"maxAgeSeconds": 3600
}
]
我也尝试过这样做:
[
{
"origin": ["*"],
"responseHeader": ["*"],
"method": ["GET", "HEAD", "DELETE"],
"maxAgeSeconds": 3600
}
]
仍然没有进展。错误与之前相同。
这是我为Slingshot编写的服务器代码:
if(Meteor.isServer){
// Initiate file upload restrictions
Slingshot.fileRestrictions("userLogoUpload", {
//Only images are allowed
allowedFileTypes: ["image/png", "image/jpeg", "image/gif"],
//Maximum file size:
maxSize: 2 * 1024 * 1024 // 2 MB (null for unlimited)
});
// Google Cloud Directives
Slingshot.createDirective("userLogoUpload", Slingshot.GoogleCloud, {
bucket: Meteor.settings.public.GoogleCloudBucket,
GoogleAccessId: Meteor.settings.private.GoogleAccessId,
GoogleSecretKey: Assets.getText("xxxxxxxxxx.pem"),
// Uploaded files are publicly readable
acl: "public-read",
authorize: function(){
if(!Meteor.userId()){
throw new Meteor.error("Login Required", "Please log in to upload files");
}
return true;
},
key: function(file){
let user = Meteor.users.findOne(Meteor.userId());
return user.profile.username + "/" + file.name;
}
});
}
这是客户端上传的启动方式:
let uploader = new Slingshot.Upload("userLogoUpload");
uploader.send(document.getElementById("upload").files[0], function(error, downloadUrl){
if(!error){
console.log(downloadUrl);
} else{
console.error('Error uploading', uploader.xhr.response);
console.log(error);
}
所有变量都没有问题。我的pem文件也没有问题,能够正常工作。所以要么是Google云出了问题,要么是我设置CORS文件的方式有误。
感谢任何帮助。