簡體   English   中英

如何將Firebase存儲圖像與Google Cloud Vision API一起使用?

[英]How to use Firebase Storage image with Google Cloud Vision API?

我有一個webapp,用戶通過Firebase Auth匿名進行身份驗證。 我將用戶的圖像存儲在Firebase存儲中,后台版本由Google雲存儲存儲桶支持。 當我嘗試使用客戶端javascript中的Google Cloud Vision API獲取圖片屬性時,我收到了權限錯誤。

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

如果我將圖像公開,一切正常。 但這是用戶數據,不能公開。 我怎么解決這個問題?

我調用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);
  });

我將代碼上傳到存儲的代碼:

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

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

您真的希望通過Google Cloud Functions之類的東西在服務器端執行此操作( 請參閱此處的示例 )。

客戶端將要求您的API密鑰具有足夠的權限來訪問照片,這將基本上使私有照片公開(因為一旦API密鑰有權讀取它們,什么阻止惡意客戶端這樣做?)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM