Skip to content

dxdc/aws-sdk-google-apps

Repository files navigation

aws-sdk-google-apps

mit license Donate

Native support for the entire AWS SDK for JavaScript in Google Apps Script.

Working examples for Simple Email Service (SES), S3, Lambda, and EC2. This project can easily accommodate all other AWS services, e.g.,

npm run sdk --sdk=ses,s3,ec2,lambda,dynamodb && npm run build

Library deployment

  1. Add the existing Google Apps Script project as a Library
  • Script ID 1J6iN9mJE-NK6LGTlZcngsflJEx59tE3ZOW4-2cdHbgw0So2MmEcRZxKG
  • Choose an identifier, e.g., AWSLIB
  • Versions of the Google Apps Script project map to tags on this Git repository
  1. Initialize your AWS config settings and implement one of this library's S3, Lambda, SES, or EC2 functions. Examples.js shows some working examples.
const AWS_CONFIG = {
  accessKey: 'AK0ZXZD0KGNG4KG6REBP', // use your own AWS key
  secretKey: 'EXrPgHC41HEW2YownLUnJLgh6bMsrmW1uva1ic24', // use your own AWS key
  region: 'us-east-1',
};

// example function to retrieve S3 object
async function getS3ObjectTest() {
  AWSLIB.initConfig(AWS_CONFIG);
  var result = await AWSLIB.getS3Object('myBucket', 'folder1/file.jpg');
  if (result === false) {
    return false;
  }

  var blob = Utilities.newBlob(result.Body, result.ContentType);
  // Logger.log(blob.getDataAsString());
  return blob;
}
  1. Methods for common S3, Lambda, SES, and EC2 services have been implemented. However, direct access to library AWS SDK methods is also available via the AWS property on your chosen library identifier, e.g.:
// Create a new service object
var s3 = new AWSLIB.AWS.S3({
  apiVersion: '2006-03-01',
  params: { Bucket: albumBucketName },
});

Advanced deployment

  1. Customize the AWS SDK if additional services are needed

  2. Copy & paste all the files from dist/ into your project.

  • Examples.js and Config.js are placeholders, which should be adapted with your code.

Customized AWS SDK

The AWS SDK can be customized for specific API versions and/or services.

This project defaults to the following services: ses,s3,lambda,ec2.

To customize the codebase for your project:

$ cd aws-sdk-js
$ npm install
$ cd ..
$ npm install
$ npm run sdk --sdk=all
# can also be customized, e.g.
# npm run sdk --sdk=ses,ec2,dynamodb-2011-12-05,dynamodb-2012-08-10
$ npm run build

Services can also be customized using a comma-delimited list of services. AWS has a full list of identifiers and api versions available.

Create your own library

  1. Create a new project in Google Scripts.

  2. Copy & paste all the files from dist/ into your project file and save it.

  3. Go File → Manage versions and click Save new version.

  4. You can Share and make it public.

  5. Copy your library Script ID from File → Project properties → Script ID

  6. Reference this Script ID as a library in other projects.

Background

Several other projects exist for interfacing between the AWS API and Google Apps Script. However, these projects have very limited support for the full suite of AWS services offered. This is the first project which invokes the AWS SDK directly.

SDK Core modifications

Several key changes to the AWS SDK core were required to make it compatible with the Google Apps Script framework.

Namely, Google Apps Script does not have support for window, XMLHttpRequest, and DOMParser - instead, it requires the use of UrlFetchApp and XmlService. These patch files can be found in src-sdk.

Note, the final patched build remains compatible in the browser, e.g.,

-AWS.HttpClient.prototype = AWS.XHRClient.prototype;
+AWS.HttpClient.prototype = typeof XMLHttpRequest === 'undefined' && typeof UrlFetchApp !== 'undefined' ? AWS.XHRGoogleClient.prototype : AWS.XHRClient.prototype;

How to contribute

Have an idea? Found a bug? Contributions and pull requests are welcome.

Credits

Acknowledgements to @lsegal for initial research, @sk16 for modifying AWS.XHRClient for use with fetch, as well as the authors of existing repositories (e.g., aws-apps-scripts, apps-script-aws-request), for insight in how to solve this problem.

Support this project

I try to reply to everyone needing help using these projects. Obviously, this takes time. However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it:

  • Starring and sharing the projects you like 🚀
  • PayPal PayPal— You can make one-time donations to dxdc via PayPal.
  • Venmo— You can make one-time donations via Venmo. Venmo QR Code
  • Bitcoin— You can send me Bitcoin at this address: 33sT6xw3tZWAdP2oL4ygbH5TVpVMfk9VW7