Skip to content

搞英语 → 看世界

翻译英文优质信息和名人推特

Menu
  • 首页
  • 作者列表
  • 独立博客
  • 专业媒体
  • 名人推特
  • 邮件列表
  • 关于本站
Menu

如何使用服务帐户将文件上传到 Google Drive

Posted on 2022-04-10

本分步指南将指导您如何使用Node.js使用服务帐户将文件上传到 Google Drive。对于此示例,我们在您的本地硬盘驱动器上有一个包含多个文件的文件夹,我们需要将这些文件上传到 Google Drive 中的特定文件夹。

1. 创建谷歌云项目

转到cloud.google.com并创建一个新的 Google Cloud 项目。为您的项目命名,更改项目 ID 并单击“ Create ”按钮。

Create Google Cloud Project

2.启用谷歌API

从左侧菜单中选择APIs & Services ,然后单击Enable APIs and Services以启用各种 Google API。如果您打算将文件上传到 Google Drive,则需要启用 Drive API。如果您希望使用 Google Cloud Storage API,则需要启用 Storage API。

Google Drive API

3. 创建服务帐户

在APIs & Services部分,单击Credentials并单击Create credentials以创建服务帐户。

Create Service Account

3a。描述服务帐户

为您的服务帐号命名和服务帐号 ID。这就像一个电子邮件地址,将来将用于识别您的服务帐户。单击Done以完成创建服务帐户。

Service Account Details

3b。创建密钥文件

在 Cloud Console 中,转到 IAM 和管理员 > 服务帐号页面。单击要为其创建密钥的服务帐户的电子邮件地址。单击Keys选项卡。单击Add key下拉菜单,然后选择Create new key 。

选择JSON作为密钥类型,然后单击创建。这将下载一个包含您的私钥的 JSON 文件。不要将此文件提交到 Github 存储库。

Service Account Key

4.共享驱动器文件夹

对于此示例,我们希望将文件从本地文件夹上传到 Google Drive 中的特定文件夹。

转到您的 Google Drive 并创建一个新文件夹。右键单击该文件夹,选择共享并将您在步骤 3 中创建的服务帐户的电子邮件地址添加为该文件夹的编辑器。

因此,您的 Node.js 应用程序将能够访问此文件夹并将文件上传到其中。该应用程序将无法访问您 Google Drive 上的任何其他资源。

Share Google Drive Folder

5. 配置 Node.js 应用

现在已经设置了服务帐户,我们需要设置一个 Node.js 应用程序,它将文件上传到 Google Drive。我们将从命令行运行此应用程序,但您也可以使用 Google Cloud Run 和 Docker 将其转换为 Web 应用程序。

5a。创建授权的 OAuth2 客户端

将service.json替换为您在步骤 3b 中创建的服务帐户密钥文件的名称。

 // service.js const { google } = require ( 'googleapis' ) ; const path = require ( 'path' ) ; const getDriveService = ( ) => { const KEYFILEPATH = path . join ( __dirname , 'service.json' ) ; const SCOPES = [ 'https://www.googleapis.com/auth/drive' ] ; const auth = new google . auth . GoogleAuth ( { keyFile : KEYFILEPATH , scopes : SCOPES , } ) ; const driveService = google . drive ( { version : 'v3' , auth } ) ; return driveService ; } ; module . exports = getDriveService ;

5b。编写文件上传器

将父文件夹替换为您要上传到的 Google Drive 文件夹的文件夹 ID。文件上传后,我们也会将本地文件移至垃圾箱。

 // upload.js const fs = require ( 'fs' ) ; const getInvoiceFolder = require ( './folder' ) ; const drive = require ( './service' ) ; const uploadSingleFile = async ( fileName , filePath ) => { const folderId = 'DRIVE_FOLDER_ID' ; const { data : { id , name } = { } } = await drive . files . create ( { resource : { name : fileName , parents : [ folderId ] , } , media : { mimeType : 'application/pdf' , body : fs . createReadStream ( filePath ) , } , fields : 'id,name' , } ) ; console . log ( 'File Uploaded' , name , id ) ; } ; const scanFolderForFiles = async ( folderPath ) => { const folder = await fs . promises . opendir ( folderPath ) ; for await ( const dirent of folder ) { if ( dirent . isFile ( ) && dirent . name . endsWith ( '.pdf' ) ) { await uploadSingleFile ( dirent . name , path . join ( folderPath , dirent . name ) ) ; await fs . promises . rm ( filePath ) ; } } } ; module . exports = scanFolderForFiles ;

6.运行文件上传器

现在一切都设置好了,创建一个index.js文件并运行node index.js命令将文件上传到 Google Drive。

 // index.js const scanFolderForFiles = require ( './scan' ) ; scanFolderForFiles ( 'local-folder' ) . then ( ( ) => { console . log ( '? All files have been uploaded to Google Drive successfully!' ) ; } ) ;

您可以考虑使用https://www.googleapis.com/auth/drive.file范围,而不是更广泛的https://www.googleapis.com/auth/drive范围。在这种情况下,初始父文件夹也应该使用相同的应用程序创建,否则它将无权写入该文件夹。

来源: https://www.labnol.org/google-api-service-account-220404

本站文章系自动翻译,站长会周期检查,如果有不当内容,请点此留言,非常感谢。
  • Abhinav
  • Abigail Pain
  • Adam Fortuna
  • Alberto Gallego
  • Alex Wlchan
  • Answer.AI
  • Arne Bahlo
  • Ben Carlson
  • Ben Kuhn
  • Bert Hubert
  • Bits about Money
  • Brian Krebs
  • ByteByteGo
  • Chip Huyen
  • Chips and Cheese
  • Christopher Butler
  • Colin Percival
  • Cool Infographics
  • Dan Sinker
  • David Walsh
  • Dmitry Dolzhenko
  • Dustin Curtis
  • Elad Gil
  • Ellie Huxtable
  • Ethan Marcotte
  • Exponential View
  • FAIL Blog
  • Founder Weekly
  • Geoffrey Huntley
  • Geoffrey Litt
  • Greg Mankiw
  • Henrique Dias
  • Hypercritical
  • IEEE Spectrum
  • Investment Talk
  • Jaz
  • Jeff Geerling
  • Jonas Hietala
  • Josh Comeau
  • Lenny Rachitsky
  • Liz Danzico
  • Lou Plummer
  • Luke Wroblewski
  • Matt Baer
  • Matt Stoller
  • Matthias Endler
  • Mert Bulan
  • Mostly metrics
  • News Letter
  • NextDraft
  • Non_Interactive
  • Not Boring
  • One Useful Thing
  • Phil Eaton
  • Product Market Fit
  • Readwise
  • ReedyBear
  • Robert Heaton
  • Ruben Schade
  • Sage Economics
  • Sam Altman
  • Sam Rose
  • selfh.st
  • Shtetl-Optimized
  • Simon schreibt
  • Slashdot
  • Small Good Things
  • Taylor Troesh
  • Telegram Blog
  • The Macro Compass
  • The Pomp Letter
  • thesephist
  • Thinking Deep & Wide
  • Tim Kellogg
  • Understanding AI
  • 英文媒体
  • 英文推特
  • 英文独立博客
©2025 搞英语 → 看世界 | Design: Newspaperly WordPress Theme