據我所知,Google Cloud 的一種最佳做法是盡可能不使用服務帳戶密鑰。
在這里,我有一個谷歌云功能(nodejs)通過驅動器 API(googleapis 包 v95.0.0)訪問驅動器,使用服務帳戶密鑰(通過控制臺生成的檔案)。
什么有效:
const settings = require('./config/driveSA.json');
const options = {
googleKey: settings,
user: '[email protected]' //user impersonification
}
const auth = gdriveUtils.prepareJwt(google, options);
const drive = google.drive({ version: 'v3', auth: auth });
const file = await drive.files.get({
fileId: fileId,
alt: 'media',
supportsTeamDrives: true,
});
我想要達到的目標:
我想改進這個功能,讓它直接使用Cloud Function本身繼承的默認服務帳號(與key相同)。
這種方法在使用“@google-cloud”相關包時總是有效的。就像存盤一樣,我們可以簡單地:
const storage = new Storage();
const bucket = storage.bucket(bucketName);
我嘗試了什么:
不指定任何身份驗證 obj:
const drive = google.drive({ version: 'v3' });
const file = await drive.files.get({
fileId: fileId,
alt: 'media',
supportsTeamDrives: true,
});
使用未真正記錄的方法:getApplicationDefault
import { GoogleApis, Auth } from 'googleapis';
const adcResponse = await google.auth.getApplicationDefault();
const auth: Auth.GoogleAuth = new google.auth.GoogleAuth(adcResponse);
const drive = google.drive({ version: 'v3', auth: auth });
const file = await drive.files.get({
fileId: fileId,
alt: 'media',
supportsTeamDrives: true,
});
不幸的是,包的檔案對身份驗證部分總是含糊其辭,并且總是在示例中使用密鑰。這是可行的嗎?或者在這種情況下我是否被迫使用鑰匙?
謝謝!
uj5u.com熱心網友回復:
正如用戶@DalmTo 所提到的,您很可能混合了這些帳戶。請記住,使用 GCP 服務和 Google API 服務時涉及 2 個服務帳戶: GCP 服務帳戶:呼叫函式時正在使用哪些云函式(很可能是 App Engine 默認服務帳戶)。Google API 服務帳戶:它有權在您的驅動器上執行各種操作。
如果您希望能夠在驅動器中執行所有這些活動,請確保您在函式中使用 Google API 服務帳戶,這很容易通過您提到的帳戶密鑰完成。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/427974.html
