[英]How to use Firebase Storage image with Google Cloud Vision API?
I have a webapp where users are authenticated anonymously with Firebase Auth. 我有一个webapp,用户通过Firebase Auth匿名进行身份验证。 I store images from users in Firebase Storage, which behind the scenes is backed by a Google Cloud Storage bucket.
我将用户的图像存储在Firebase存储中,后台版本由Google云存储存储桶支持。 When I try to use the Google Cloud Vision API from client-side javascript to get the image properties I get a permission error.
当我尝试使用客户端javascript中的Google Cloud Vision API获取图片属性时,我收到了权限错误。
image-annotator::User lacks permission.: Can not open file: gs://MYAPP.appspot.com/photos/IMG_12345.jpg
If I make the image public, everything works. 如果我将图像公开,一切正常。 But this is user data and can't be public.
但这是用户数据,不能公开。 How can I solve this?
我怎么解决这个问题?
My code for calling the vision api: 我调用vision api的代码:
gapi.client.init({
'apiKey': MY_API_KEY,
'discoveryDocs': ['https://vision.googleapis.com/$discovery/rest'],
// clientId and scope are optional if auth is not required.
'clientId': MY_APP_ID + '.apps.googleusercontent.com',
'scope': 'profile',
}).then(function() {
// 3. Initialize and make the API request.
return gapi.client.vision.images.annotate({
"requests":
[
{
"features":
[
{
"type": "LABEL_DETECTION"
},
{
"type": "IMAGE_PROPERTIES"
}
],
"image":
{
"source":
{
"gcsImageUri": "gs://MY_APP.appspot.com/photos/" + "IMG_12345.jpg"
}
}
}
]
});
}).then(function(response) {
console.log(response.result);
}, function(reason) {
console.log('Error: ' + reason.result.error.message);
});
My code for uploading images to storage: 我将代码上传到存储的代码:
var file = e.target.files[0];
var metadata = {
contentType: file.type
};
console.log(file);
var uploadTask = photosStorageRef.child(file.name).put(file, metadata);
You really want to do this on the server side via something like Google Cloud Functions ( see my example here ). 您真的希望通过Google Cloud Functions之类的东西在服务器端执行此操作( 请参阅此处的示例 )。
Client side will require that your API key have sufficient permission to access the photos, which will basically make the private photos public (because once the API key has permission to read them, what's to stop a malicious client from doing so?). 客户端将要求您的API密钥具有足够的权限来访问照片,这将基本上使私有照片公开(因为一旦API密钥有权读取它们,什么阻止恶意客户端这样做?)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.