linux - 使用文件转换将 Google Drive 备份到 .zip 文件

标签 linux google-drive-api applescript google-docs

我在这个话题上一直在兜圈子,找不到适用于 Google Drive 上海量数据的自动化方法。这是我希望实现的目标:

我的公司使用无限制的 Google Drive 来存储共享文档,我们希望自动备份内容。但是我们不能将数据存储在像“.gdoc”和“.gsheet”这样的谷歌文档的备份中......我们需要以 Microsoft/Open-Office 格式(“.docx”和“.xlsx").

我们目前使用 Google 的 Takeout 页面来压缩驱动器的所有内容并将其保存在我们的 Linux 服务器(具有冗余存储)上。它会将文件压缩并导出为正确的格式。 此处:[https://takeout.google.com/settings/takeout][1]

现在可以了……但需要我们进行一些手动操作。照看 zip、下载和上传过程变得很浪费。我已经搜索并阅读到外卖的谷歌 API 无法通过 gscript 使用。所以,这似乎是不可能的。

使用 Google 脚本,我已经能够转换单个文件....但不能,例如,将“.gsheet”文件的文件夹转换为“.xlsx”格式。也许可以将所有谷歌文件复制并转换到驱动器上的新文件夹中。访问驱动器和转换后的“备份”后,我们可以通过服务器备份转换后的文件集合...

所以这就是全部内容:

  1. 您能否将所有 google 驱动器和/或驱动器上的特定文件夹从“.gdoc”批量转换为“.docx”,并将“.gsheet”转换为“.xlsx”。这可以用 gscript 完成吗?

  2. 如果无法通过问题一的方法,是否有人熟悉可以执行此类目录转换的 Linux 或 Mac 应用程序? (不要相信,因为谷歌的专有文件类型)

我陷入了困境,对这个问题的任何见解都会有所帮助。我真的希望 Google 允许用户通过脚本选择来转换和导出驱动器文件夹。

最佳答案

#1) 你能否将所有谷歌驱动器和/或驱动器上的特定文件夹从“.gdoc”批量转换为“.docx”,并将“.gsheet”转换为“.xlsx”。这可以用 gscript 完成吗?

你可以试试这个:

How To Automaticlly Convert files in Google App Script

将 Google App Script 中的文件转换为 blob

var documentId = DocumentApp.getActiveDocument().getId();        

function getBlob(documentId) {
 var file = Drive.Files.get(documentId);
 var url = file.exportLinks['application/vnd.openxmlformats-officedocument.wordprocessingml.document'];
 var oauthToken = ScriptApp.getOAuthToken();
 var response = UrlFetchApp.fetch(url, {
   headers: {
     'Authorization': 'Bearer ' + oauthToken
   }
 });
 return response.getBlob();
}

在云端硬盘中将文件保存为 docx

function saveFile(blob) {
   var file = {
    title: 'Converted_into_MS_Word.docx',
    mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
  };
  file = Drive.Files.insert(file, blob);
  Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
  return file;
}

Time-driven triggers

A time-driven trigger (also called a clock trigger) is similar to a cron job in Unix. Time-driven triggers let scripts execute at a particular time or on a recurring interval, as frequently as every minute or as infrequently as once per month. (Note that an add-on can use a time-driven trigger once per hour at most.) The time may be slightly randomized — for example, if you create a recurring 9 a.m. trigger, Apps Script chooses a time between 9 a.m. and 10 a.m., then keeps that timing consistent from day to day so that 24 hours elapse before the trigger fires again.

function createTimeDrivenTriggers() {
  // Trigger every 6 hours.
  ScriptApp.newTrigger('myFunction')
      .timeBased()
      .everyHours(6)
      .create();

  // Trigger every Monday at 09:00.
  ScriptApp.newTrigger('myFunction')
      .timeBased()
      .onWeekDay(ScriptApp.WeekDay.MONDAY)
      .atHour(9)
      .create();
}

过程:

  1. List all files id inside a folder
  2. 转换文件
  3. 将代码插入时间驱动的触发器

2) 如果无法通过问题一的方法,是否有人熟悉可以进行此类目录转换的 Linux 或 Mac 应用程序? (不要相信,因为googles专有文件类型)

如果您想将其保存在本地,请尝试设置一个 cronjob 并使用 Download Files

The Drive API allows you to download files that are stored in Google Drive. Also, you can download exported versions of Google Documents (Documents, Spreadsheets, Presentations, etc.) in formats that your app can handle. Drive also supports providing users direct access to a file via the URL in the webViewLink property.

Depending on the type of download you'd like to perform — a file, a Google Document, or a content link — you'll use one of the following URLs:

  1. Download a file — files.get with alt=media file resource
  2. Download and export a Google Doc — files.export
  3. Link a user to a file — webContentLink from the file resource

示例代码:

$fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
$content = $driveService->files->get($fileId, array(
  'alt' => 'media' ));

希望这对您有所帮助并回答了您所有的问题

关于linux - 使用文件转换将 Google Drive 备份到 .zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37842755/

相关文章:

r - 如何使用 httr 发布多部分/相关内容(适用于 Google Drive API)

android - 尝试在 Android 应用程序中访问 Google Drive API 时出现 403 错误

objective-c - 通过 AppleScript 在 Objective-C 中编辑 Mac OS X 登录项

bluetooth - 在 Mavericks 中使用 applescript 禁用蓝牙

linux - 如何根据文件名模式删除文件?

linux - 在终端中搜索文本文件

google-sheets - Google OAuth 2.0 的长期访问 token

applescript - 从纯文本源文件编译applescript

linux - 我如何连接到一个简单的 linux 控制台来执行一些任何人都可以使用 linux 操作系统的基本命令?

linux - 如何在mailx命令中添加收件人姓名