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
- 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
- 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;
}
- 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 },
});
-
Customize the AWS SDK if additional services are needed
-
Copy & paste all the files from
dist/
into your project.
Examples.js
andConfig.js
are placeholders, which should be adapted with your code.
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 a new project in Google Scripts.
-
Copy & paste all the files from
dist/
into your project file and save it. -
Go
File → Manage versions
and clickSave new version
. -
You can
Share
and make it public. -
Copy your library Script ID from
File → Project properties → Script ID
-
Reference this Script ID as a library in other projects.
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.
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;
Have an idea? Found a bug? Contributions and pull requests are welcome.
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.
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: