Skip to content

搞英语 → 看世界

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

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

使用 Google Apps 脚本将文件从 Google Drive 上传到 Google Cloud Storage

Posted on 2023-01-03

本教程介绍如何使用 Google Apps 脚本将文件和文件夹从 Google Drive 上传到 Google Cloud Storage 中的存储桶。您甚至可以设置一个基于时间的触发器,例如 cron 作业,它会监视您 Google Drive 中的文件夹并自动将新传入的文件上传到 Google Cloud Storage。同样的技术也可以用于将文件从 Google Drive 上传到 Firebase Storage。

创建谷歌云存储桶

首先,转到console.cloud.google.com/projectcreate并创建一个新的 Google Cloud 项目。添加项目后,转到console.cloud.google.com/storage/create-bucket并创建一个新存储桶。为您的存储桶起一个唯一的名称,并选择您要存储数据的区域。如果您上传的文件是私有的,并且您不想在以后任何时候公开它们,您可以启用“在此存储桶上实施公共访问预防”选项。

Cloud Storage Service Account

创建存储服务帐户

接下来,转到 IAM & Admin > Service Accounts console.cloud.google.com/iam-admin/serviceaccounts/create并创建一个新的服务帐户。为您的服务帐户命名并选择“Storage Admin”角色。您可能还想向服务帐户添加“服务帐户令牌创建者”角色,因为这是为您上传到 Google Cloud Storage 的文件创建签名 URL 所必需的。

从服务帐户列表中,单击您在上一步中创建的帐户。转到“密钥”选项卡并单击“添加密钥”>“创建新密钥”,然后选择 JSON 选项。这将下载包含服务帐户凭据的 JSON 文件。您需要这些凭据才能将文件上传到 Google Cloud Storage。

编写 Google Apps 脚本代码

转到script.new创建一个新的 Google Apps 脚本项目。单击库并将 OAuth2 库1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF添加到您的项目中。接下来,添加一个新文件service.js ,并使用服务帐户 JSON 文件中的private_key和client_email的值来创建一个新的 OAuth2 服务。

 // service.js // Replace these with your own values const service_account = { private_key : '-----BEGIN PRIVATE KEY-----\n51CjpLsH8A\n-----END PRIVATE KEY-----\n' , client_email : '[email protected]' , } ; const getStorageService = ( ) => OAuth2 . createService ( 'FirestoreStorage' ) . setPrivateKey ( service_account . private_key ) . setIssuer ( service_account . client_email ) . setPropertyStore ( PropertiesService . getUserProperties ( ) ) . setCache ( CacheService . getUserCache ( ) ) . setTokenUrl ( 'https://oauth2.googleapis.com/token' ) . setScope ( 'https://www.googleapis.com/auth/devstorage.read_write' ) ;

上传文件到谷歌云存储

接下来,我们将在 Apps 脚本中编写上传功能。该函数获取您要上传到 Google Cloud Storage 的文件的文件 ID、存储桶的名称以及您要存储文件的路径。

该函数使用上一步中的getStorageService函数来创建新的 OAuth2 服务。 OAuth2 服务的getAccessToken方法用于获取将文件上传到 Google Cloud Storage 所需的访问令牌。

 // Replace these with your own values const DRIVE_FILE_ID = 'abc123' ; const STORAGE_BUCKET = 'labnol.appspot.com' ; const FILE_PATH = 'parentFolder/subFolder' ; const uploadFileToCloudStorage = ( ) => { const file = DriveApp . getFileById ( DRIVE_FILE_ID ) ; const blob = file . getBlob ( ) ; const bytes = blob . getBytes ( ) ; const API = ` https://www.googleapis.com/upload/storage/v1/b ` ; const location = encodeURIComponent ( ` ${ FILE_PATH } / ${ file . getName ( ) } ` ) ; const url = ` ${ API } / ${ STORAGE_BUCKET } /o?uploadType=media&name= ${ location } ` ; const service = getStorageService ( ) ; const accessToken = service . getAccessToken ( ) ; const response = UrlFetchApp . fetch ( url , { method : 'POST' , contentLength : bytes . length , contentType : blob . getContentType ( ) , payload : bytes , headers : { Authorization : ` Bearer ${ accessToken } ` , } , } ) ; const result = JSON . parse ( response . getContentText ( ) ) ; Logger . log ( JSON . stringify ( result , null , 2 ) ) ; } ;

另请参阅: Google Drive 的文件上传表单

原文: https://www.labnol.org/code/20074-upload-files-to-google-cloud-storage

本站文章系自动翻译,站长会周期检查,如果有不当内容,请点此留言,非常感谢。
  • 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