0

我正在上传配置文件图像到谷歌云存储(这工作正常),并试图使所有的图像公开可用,但是我一直从返回403错误IMG SRC或者只是在浏览器中粘贴它的内部使用URL时,返回的URL看起来如下:Nodejs Google云存储在访问公开网址时返回403

https://storage.googleapis.com/my_bucket_name/12370691_1205162746164520_2152367844899316112_o.jpg 

,它提供了以下错误:

Anonymous users does not have storage.objects.get access to object my_bucket_name/15991995_384418798568801_734448169_o.jpg. 

note: my_bucket_name is not the real bucket name, it's just a placeholder for this post

代码BEL流显示了如何设置谷歌云储存起来:

const gcs = require('@google-cloud/storage')({ 
    projectId: ‘my_project_id’, 
    keyFilename: './routes/to/my/keyfile.json' 
}); 

const bucket = gcs.bucket(‘my_bucket_name’); 

const options = { 
    entity: 'allUsers', 
    role: gcs.acl.READER_ROLE 
}; 

bucket.acl.add(options, (err, aclObject) => { 
    console.log(err); //null 
    console.log(aclObject); //{ entity: 'allUsers', role: 'READER' } 
}); 

function getPublicUrl(filename) { 
    return `https://storage.googleapis.com/${bucketName}/${filename}`; 
} 

... code for uploading the image below, (works fine) 

回答

0

更改bucket.acl.addbucket.acl.update修复该问题通过使斗公开可读,运行此代码一旦足以改变默认的桶的ACL权限,你可以删除代码算账:

const options = { 
    entity: 'allUsers', 
    role: gcs.acl.READER_ROLE 
}; 

bucket.acl.update(options, (err, aclObject) => { 
    console.log(err); //null 
    console.log(aclObject); //{ entity: 'allUsers', role: 'READER' } 
}); 

注:同样可利用来完成:

gsutil defacl set public-read gs://[YOUR-BUCKET-NAME] 

如果你不想为了使整个桶可读,即url/bucket_name,并且只想使桶内的文件可读,那么你需要在文件写入时将该文件配置为可由匿名用户读取,如下所示:

const remoteWriteStream = file.createWriteStream({ 
    metadata: { 
     contentType: req.file.mimetype 
    }, 
    predefinedAcl: "publicRead" 
    }); 
相关问题