简体   繁体   English

如何将Firebase存储图像与Google Cloud Vision API一起使用?

[英]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.

相关问题 来自Google Vision API的空响应,其中包含存储在Google云端存储中的图像 - Empty response from Google Vision API with an image stored in Google Cloud Storage 无法验证 google cloud vision api 。 如何验证它以进一步使用它 - Not able to autheticate google cloud vision api . How to autheticate it to use it further 如何将映像从Expressjs更新到Google Cloud Storage / Firebase Storage - How update an image from Expressjs to Google Cloud Storage / Firebase Storage 在Firebase Cloud Functions中使用Google Vision API时发生TypeError - TypeError when using Google Vision API in Firebase Cloud Functions 节点JS | 谷歌视觉 API | 将 base64 发送到 Google Cloud Vision 时出现“错误的图像数据” - Node JS | Google Vision API | "Bad image data" when sending base64 to Google Cloud Vision 如何使用 API 存储在 Google 中的密钥 Firebase 云函数 - How to use API Keys stored in Google Firebase Cloud Functions 如何将图像从云功能请求发送到视觉API? - How to send image from cloud function request to vision API? 如何使用 google-cloud 和 node js 从 firebase 存储中检索图像并将其显示在网页上 - how to retrieve image from firebase storage and display it on a webpage using google-cloud and node js Google Cloud Vision API返回不良结果? - Google Cloud Vision API returning bad result? Google Cloud Vision REST API请求错误 - Google Cloud Vision REST API Request Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM