-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3-example.js
220 lines (194 loc) · 6.57 KB
/
s3-example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* An implementation of the AWS request v4 for retrieving files from S3, for use
* in AdWords Scripts.
*
* Based on :
* https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html
*/
function s3example() {
// Basic example, grabbing a CSV file from Amazon and writing to a Google Sheet
// Input CSV URL on Amazon S3
var inputCsvUrl = 'https://s3.us-east-2.amazonaws.com/example_bucket/test.csv';
// Output spreadsheet in Google Sheets, for ingestion by other Google Apps and services.
var outputSpreadsheetUrl ='https://docs.google.com/spreadsheets/d/...../edit';
// Create S3 client, using credentials.
var s3 = new S3(
'us-east-2', 'ACCESS_KEY_ID',
'ACCESS_SECRET');
var response = s3.fetch(inputCsvUrl);
if (response.getResponseCode() === 200) {
var csv = Utilities.parseCsv(response.getContentText());
var ss = SpreadsheetApp.openByUrl(outputSpreadsheetUrl);
var s = ss.getActiveSheet();
s.getRange(1, 1, csv.length, csv[0].length).setValues(csv);
} else {
// ... Do something with error
Logger.log(response);
}
}
/**
* Creates a client for making authenticated requests to the Amazon S3 service.
*
* @param {string} region The AWS region, e.g. "us-east-1".
* @param {string} accessKeyId
* @param {string} accessSecret
* @return {!Object} the client object
*/
function S3(region, accessKeyId, accessSecret) {
this.region = region;
this.accessKeyId = accessKeyId;
this.accessSecret = accessSecret;
this.versionString = 'aws4_request';
this.service = 's3';
this.date = new Date();
this.signingKey = this._createSigningKey();
this.credential = this._getCredential();
this.headers = {
'x-amz-content-sha256': this._getHexSha256Hash(''),
'x-amz-date': this._getTimeStampISO8601Format()
};
this.uriRegex = /^http(?:s|):\/\/[^\/]+([^\?]*)/;
this.hostRegex = /^http(?:s|):\/\/([^\/]+)/;
}
/**
* Retrieves a URL using a GET request.
*
* @param {string} url The URL of the resource.
* @return {!HttpResponse} The response {@see https://developers.google.com/apps-script/reference/url-fetch/http-response)
*/
S3.prototype.fetch = function(url) {
var canonicalRequest = this._createCanonicalRequest(url);
var stringToSign = this._getStringToSign(canonicalRequest);
var signature = this._getHexHmacSha256(this.signingKey, stringToSign);
var authorization = this._getAuthorizationHeader(signature);
var headers = {Authorization: authorization};
var headerKeys = Object.keys(this.headers);
headerKeys.forEach(function(k) {
headers[k] = this.headers[k];
}.bind(this));
var params = {headers: headers, muteHttpExceptions: true};
return UrlFetchApp.fetch(url, params);
};
S3.prototype._createCanonicalRequest = function(url) {
return [
'GET', this._getCanonicalUri(url), this._getCanonicalQueryString(url),
this._getCanonicalHeaders(url), this._getSignedHeaders(),
this._getHexSha256Hash('')
].join('\n');
};
S3.prototype._getCanonicalUri = function(url) {
var matches = this.uriRegex.exec(url);
return matches[1] || '/';
};
S3.prototype._getCanonicalQueryString = function(url) {
var paramsIndex = url.indexOf('?');
if (paramsIndex === -1 || paramsIndex === url.length - 1) {
return '';
}
var paramsString = url.substring(paramsIndex + 1);
var params = paramsString.split('&').map(function(p) {
var parts = p.split('=');
return [parts[0], parts[1] || ''];
});
params.sort(this._pairKeySort);
return params
.map(function(p) {
return encodeURIComponent(p[0]) + '=' + encodeURIComponent(p[1]);
})
.join('&');
};
S3.prototype._getCanonicalHeaders = function(url) {
var matches = this.hostRegex.exec(url);
var host = matches[1];
var canonical = [['Host', host]];
var keys = Object.keys(this.headers);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
canonical.push([key, this.headers[key]]);
}
var list = canonical.map(function(p) {
return p[0].toLowerCase() + ':' + p[1].trim() + '\n';
});
list.sort();
return list.join('');
};
S3.prototype._getSignedHeaders = function() {
var headers = Object.keys(this.headers);
var lcHdrs = headers.map(function(h) {
return h.toLowerCase();
});
lcHdrs.push('host');
lcHdrs.sort();
return lcHdrs.join(';');
};
S3.prototype._getStringToSign = function(canonicalRequest) {
return [
'AWS4-HMAC-SHA256', this._getTimeStampISO8601Format(), this._getScope(),
this._getHexSha256Hash(canonicalRequest)
].join('\n');
};
S3.prototype._getScope = function() {
return [
this._getYearDate(), this.region, this.service, this.versionString
].join('/');
};
S3.prototype._getAuthorizationHeader = function(signature) {
return Utilities.formatString(
'AWS4-HMAC-SHA256 Credential=%s,SignedHeaders=%s,Signature=%s',
this._getCredential(), this._getSignedHeaders(), signature);
};
S3.prototype._pairKeySort = function(a, b) {
if (a[0] < b[0]) {
return -1;
} else if (a[0] > b[0]) {
return 1;
}
return 0;
};
S3.prototype._createSigningKey = function() {
var dateKey = Utilities.computeHmacSha256Signature(
this._getYearDate(), 'AWS4' + this.accessSecret);
var dateRegionKey =
Utilities.computeHmacSha256Signature(this._toBytes(this.region), dateKey);
var dateRegionServiceKey = Utilities.computeHmacSha256Signature(
this._toBytes(this.service), dateRegionKey);
return Utilities.computeHmacSha256Signature(
this._toBytes(this.versionString), dateRegionServiceKey);
};
S3.prototype._getCredential = function() {
var date = this._getYearDate();
return [
this.accessKeyId, date, this.region, this.service, this.versionString
].join('/');
};
S3.prototype._getTimeStampISO8601Format = function() {
return Utilities.formatDate(this.date, 'UTC', 'yyyyMMdd\'T\'HHmmSS\'Z\'');
};
S3.prototype._getYearDate = function() {
return Utilities.formatDate(this.date, 'UTC', 'yyyyMMdd');
};
S3.prototype._toBytes = function(str) {
return Utilities.newBlob(str).getBytes();
};
S3.prototype._getHexSha256Hash = function(payload) {
var bytes =
Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, payload);
return this._bytesToHex(bytes);
};
S3.prototype._getHexHmacSha256 = function(key, value) {
if (typeof value === 'string') {
var data = this._toBytes(value);
} else {
var data = value;
}
var bytes = Utilities.computeHmacSha256Signature(data, key);
return this._bytesToHex(bytes);
};
S3.prototype._bytesToHex = function(bytes) {
return bytes
.map(function(b) {
var c = (b < 0 ? b + 256 : b).toString(16);
return c.length === 1 ? '0' + c : c;
})
.join('');
};