Introduction
This guide explains how to retrieve and update objects such as text files and images stored in AWS S3 using Google Apps Script (GAS). The structure of the GAS setup is outlined below.

Let’s dive in and start building!
S3-for-Google-Apps-Script
When using AWS S3 from GAS, you need to write authentication-related code, which can be quite complex and time-consuming. To simplify this process, we found a useful module!
S3-for-Google-Apps-Script - GitHub
From the GitHub repository mentioned above, bring the following two files directly into your GAS project:
- S3.gs
- S3Request.gs
After importing these files, you will need to make some minor modifications.
Note: If you only need to work with text files, the following steps are not necessary.
Modifications
1.In the S3Request.gs
file, set the region name in the S3Request
function (line 12) to "ap-northeast-1"
.
※ For the hands-on session, we will proceed with "ap-northeast-1" for now. If you want to use a different region, please replace it accordingly and follow along.
function S3Request(service) {
this.service = service;
this.httpMethod = "GET";
this.contentType = "";
this.content = ""; //content of the HTTP request
this.bucket = ""; //gets turned into host (bucketName.s3.amazonaws.com)
this.objectName = "";
this.headers = {};
this.date = new Date();
this.serviceName = 's3';
this.region = 'ap-northeast-1';
this.expiresHeader = 'presigned-expires';
this.extQueryString = '';
}
2.Comment out the setContentType
function on line 34 of S3Request.gs
.
S3Request.prototype.setContentType = function (contentType) {
// if (typeof contentType != 'string') throw 'contentType must be passed as a string';
this.contentType = contentType;
return this;
};
3.Modify the content of the hexEncodedHash
function on line 391 of S3Request.gs
as shown below.
S3Request.prototype.hexEncodedHash = function(string) {
if(typeof string === "string")
return this.hex(Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, string, Utilities.Charset.UTF_8));
else
return this.hex(Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, string.getBytes()));
}
Next, let's create an S3 bucket!
Creating an S3 Bucket
Log in to the AWS Console, select the S3 service, and click the "Create bucket" button.
(The image is in Japanese. Apologies in advance.)

Enter a bucket name of your choice, select "ap-northeast-1" as the AWS region, and click the "Create bucket" button at the bottom of the screen. At this point, make a note of the bucket name and AWS region.
※ In this exercise, the settings other than the bucket name are left at their default values. Please configure the values as needed for your specific project.

As follows, it's OK once the bucket is created.

Additionally, let's upload a file (test.txt
) containing only the word "test" into the bucket!

Next, we'll create an IAM user to manage the S3 bucket.
Creating an IAM User
Log in to the AWS console, select the IAM service, and click the Add User button.

Enter a user name of your choice and click the Next button at the bottom of the screen.

Assign the policy AmazonS3FullAccess to the user as shown below, and complete the user creation process.

Navigate to the Security Credentials tab of the created user and click Create Access Key.

Select Application running outside AWS, and proceed to the next step.

Copy the generated Access Key and Secret Key.

This completes the preparation!
Now, let's move on to working with S3 objects.
S3 Object Operations
Create a new script in your GAS (Google Apps Script) project and paste the following code to execute. The code below demonstrates operations with a text file.
Object Retrieval
// ++++++++++++++++++++++++++++++
// Retrieve Object
// ++++++++++++++++++++++++++++++
function getObjectFromS3() {
// init S3 instance
const s3 = getInstance('<access key>', '<secret key>');
// get the object from S3 bucket
const object = s3.getObject('<bucket name>', 'test.txt');
console.log(object.getDataAsString());
}

The content of test.txt
has been retrieved!
Object Upload
// ++++++++++++++++++++++++++++++
// Upload Object
// ++++++++++++++++++++++++++++++
function uploadObjectToS3() {
// init S3 instance
const s3 = getInstance('<access key>', '<secret key>');
// upload the object to S3 bucket
s3.putObject('<bucket name>', 'upload.txt', 'アップロードテスト');
}

upload.txt
has been uploaded!
Object Deletion
// ++++++++++++++++++++++++++++++
// Delete Object
// ++++++++++++++++++++++++++++++
function deleteObjectInS3() {
// init S3 instance
const s3 = getInstance('<access key>', '<secret key>');
// delete the object In S3 bucket
s3.deleteObject('<bucket name>', 'upload.txt');
}

upload.txt
has been deleted!
Summary
What do you think?
I hope this helps those who are struggling with how to manage AWS S3 objects using GAS!