4

我有一个Web应用程序,用户通过Firebase身份验证进行匿名身份验证。我将来自用户的图片存储在Firebase存储中,后台由Google云端存储存储分区提供支持。当我尝试从客户端JavaScript使用Google Cloud Vision API获取图像属性时,我收到了权限错误。如何将Firebase存储图像与Google Cloud Vision API配合使用?

image-annotator::User lacks permission.: Can not open file: gs://MYAPP.appspot.com/photos/IMG_12345.jpg 

如果我公开图像,一切正常。但这是用户数据,不能公开。我该如何解决这个问题?

我调用视觉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); 
    }); 

我上传图像到存储代码:

var file = e.target.files[0]; 
var metadata = { 
    contentType: file.type 
}; 
console.log(file); 

var uploadTask = photosStorageRef.child(file.name).put(file, metadata); 

回答

1

你真的想通过类似Google Cloud Functions为此在服务器端(see my example here )。

客户端将要求您的API密钥有足够的权限访问照片,这将基本上公开私人照片(因为一旦API密钥有权读取它们,什么是阻止恶意客户端这样做? )。

+0

有没有任何方法让照片的每个用户身份验证?通过firebase存储进行访问时,请参阅https://firebase.google.com/docs/storage/,特别是“Firebase存储与Firebase身份验证无缝集成以识别用户,并提供声明式安全语言,允许您设置访问权限控制单个文件或文件组,因此您可以根据需要将文件制作为公共或私人文件。“ – astromme

+0

另外,谢谢。我已请求访问云端函数alpha。我想现在我等着? – astromme

+0

在Firebase版块中,请参阅:https://firebase.google.com/docs/storage/security/user-security –

相关问题