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 –

相關問題