我正在運行節點,從谷歌云存盤下載銷售報告。 我得到了 credentials.json 檔案。現在的問題是,每次我運行我的應用程式時,我都會得到 "[email protected] "沒有存盤.objects.get對谷歌云存盤物件的訪問權"。
是的,這個電子郵件沒有在谷歌云存盤上注冊,也沒有被賦予權限,但它應該僅憑憑據就能作業,不是嗎? 該憑證直接來自谷歌云存盤,并有以下資訊。 client_secret,project_id,redirect_uri,client_id...
我的示例代碼:
//Imports the Google Cloud client library.
const {Storage} = require('@google-cloud/storage');
const projectId = 'xxxxxxxxxxxxxxxxx'.
const key = 'credentials.json'.
const bucketName = 'pubsite.......'
const destFileName = './test'.
const fileName = 'salesreport_2020.zip'。
// 創建一個客戶端
const storage = new Storage({projectId, key});
async函式downloadFile() {
const options = {
destination: destFileName,
};
// 下載檔案
await storage.bucket(bucketName).file(fileName).download(options)。
console.log(
`gs://${bucketName}/${fileName}下載到${destFileName}.`。
);
}
downloadFile().catch(console.error)。
uj5u.com熱心網友回復:
由于您看到的是隨機的gmail地址,這很可能意味著存盤客戶端正在使用應用程式默認憑證而不是您打算使用的憑證。有兩條路可以走:
<接受應用程式的默認憑據。洗掉您傳遞給 Storage 建構式的選項,而將 GOOGLE_APPLICATION_CREDENTIALS 環境變數設定為您的 json 服務帳戶檔案。
修復Storage建構式,以正確傳遞憑證。這個問題可能很簡單,因為你需要傳遞憑證檔案的完整路徑(即 /a/b/c/credentials.json)。可能是存盤選項沒有得到正確的處理,試著明確一些,如
const storage = new Storage({projectId: 'your-project-id', keyFilename: '/path/to/keyfile.json'})。)
uj5u.com熱心網友回復:
你使用了錯誤的憑證檔案型別。
你的代碼被寫成使用服務帳戶的JSON密鑰檔案。你提到憑證檔案包含client_secret。這意味著你正試圖使用 OAuth 2.0 客戶端 ID。
在 credentials.json 檔案中查看。它應該包含 "型別"。"service_account"。如果你在檔案的開頭看到{"installed":或{"web":,那么你的憑證是錯誤的。
另外,你在行中指定的引數是錯誤的:
const storage = new Storage({projectId, key})。
替換為:
const storage = new Storage({projectId: projectId, keyFilename: key})。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/309492.html
標籤:
