如何使用谷歌脚本将文件从 gdrive 复制到 s3 存储桶? [英] How to copy files from gdrive to s3 bucket using google scripts?

查看:21
本文介绍了如何使用谷歌脚本将文件从 gdrive 复制到 s3 存储桶?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有链接的 Google 电子表格的 Google 表单.我希望每次有人提交表单时,电子表格都会被复制到 AWS 中的 s3 存储桶中.为此,我刚开始使用 Google Scripts.我设法让触发部分在表单提交上工作,但我很难理解这个 GitHub 项目 上传到 s3.

I created a Google Form with a linked Google Spreadsheet. I would like that everytime someone submits the form, the spreadsheet is copied to an s3 bucket in AWS. To do so, I just got started with Google Scripts. I managed to get the trigger part working on form submit but I am struggling to understand the readme of this GitHub project to upload to s3.

function setUpTrigger() {
  ScriptApp.newTrigger('copyDataS3')
  .forForm('1SK-2Ow63vs_TaoF54UjSgn35FL7F8_ANHDTOOiTabMM')
  .onFormSubmit()
  .create();
}

function copyDataS3() {
  // https://github.com/viuinsight/google-apps-script-for-aws
  // I do not understand where should I place aws.js and util.js. 
  // Should I do File -> New -> Script file and copy paste the contents? Should the file be .js or .gs?
  S3.init("MY_ACCESS_KEY", "MY_SECRET_KEY"); 
  // if I wanwt to copy an spreadsheet with the following id, what should go into "object" below?
  var ssID = "SPREADSHEET_ID";
  S3.putObject(bucketName, objectName, object, region)
}

推荐答案

我相信你的目标如下.

  • 您想使用 Google Apps 脚本将 Google 电子表格作为 CSV 数据发送到 s3 存储桶.
  • 当我看到 google-apps-script-for-aws 您正在使用的库,我注意到数据是作为字符串请求的.我认为在这种情况下,您的 CSV 数据可能可以直接发送.但是例如,当您要发送二进制数据时,就会发生错误.所以在这个答案中,我想提出 2 个模式的修改脚本.
  • 我认为情况可能类似于这个线程.但是我注意到您正在使用与线程不同的库.所以我发布了这个答案.
  • When I saw google-apps-script-for-aws of the library you are using, I noticed that the data is requested as the string. I thought that in this case, your CSV data might be able to be directly sent. But for example, when you want to sent a binary data, it will occur an error. So in this answer, I would like to propose the modified script of 2 patterns.
  • I thought that the situation might similar to this thread. But I noticed that you are using the different library from the thread. So I post this answer.

在这个模式中,它假设只发送文本数据.这就像您回复中的 CSV 数据.在这种情况下,我认为不需要修改库.

In this pattern, it supposes that only the text data is sent. It's like the CSV data in your replying. In this case, I think that it is not required to modify the library.

S3.init("MY_ACCESS_KEY", "MY_SECRET_KEY");  // Please set this.
var spreadsheetId = "###";  // Please set the Spreadsheet ID.
var sheetName = "Sheet1";  // Please set the sheet name.
var region = "###"; //  Please set this.

var csv = SpreadsheetApp
  .openById(spreadsheetId)
  .getSheetByName(sheetName)
  .getDataRange()
  .getValues()  //  or .getDisplayValues()
  .map(r => r.join(","))
  .join("\n");
var blob = Utilities.newBlob(csv, MimeType.CSV, sheetName + ".csv");
S3.putObject("bucketName", "test.csv", blob, region);

模式 2:

在这个模式中,它假设文本数据和二进制数据都被发送.在这种情况下,还需要修改库端.

Pattern 2:

In this pattern, it supposes that both the text data and binary data are sent. In this case, it is required to also modify the library side.

请修改第110行在 s3.js 中如下.

Please modify the line 110 in s3.js as follows.

var content = object.getDataAsString();

到:

var content = object.getBytes();

并且,请修改s3.js 中的第 146 行如下.

And, please modify the line 146 in s3.js as follows.

Utilities.DigestAlgorithm.MD5, content, Utilities.Charset.UTF_8));

到:

Utilities.DigestAlgorithm.MD5, content));

对于 Google Apps 脚本:

在这种情况下,请将 blob 提供给 S3.putObject,如下所示.

S3.init("MY_ACCESS_KEY", "MY_SECRET_KEY");  // Please set this.
var fileId = "###";  // Please set the file ID.
var region = "###"; //  Please set this.

var blob = DriveApp.getFileById(fileId).getBlob();
S3.putObject("bucketName", blob.getName(), blob, region);

参考:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆