Last active
August 7, 2023 14:37
-
-
Save katylava/083076495e0f467e1ee6afb8842b2172 to your computer and use it in GitHub Desktop.
Google Apps Script to import a CSV, stored securely on S3, to a Google Spreadsheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AWS_KEY = '<your key>'; | |
var AWS_SECRET = '<your secret>'; | |
function generateS3Url(bucket, path) { | |
var expiresDt = Math.floor(Date.now() / 1000) + (60 * 60 * 24); // can be up to 7 days from now | |
var stringToSign = 'GET\n\n\n' + expiresDt + '\n/' + bucket + '/' + encodeURIComponent(path); | |
var hmac = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_1, stringToSign, AWS_SECRET, Utilities.Charset.UTF_8); | |
var signed = encodeURIComponent(Utilities.base64Encode(hmac)); | |
return 'https://' + bucket + '.s3.amazonaws.com/' + path + '?AWSAccessKeyId=' + AWS_KEY + '&Expires=' + expiresDt + '&Signature=' + signed; | |
} | |
// Use "Resources" > "Current project's triggers" to run this on a schedule | |
function updateSheet() { | |
var csvUrl = generateS3Url('my-bucket', 'my-file.csv'); | |
SpreadsheetApp.openById('<spreadsheet id>').getSheets()[0].getRange('A1').setFormula('=IMPORTDATA("' + csvUrl + '")'); | |
} |
Sounds like AWS will no longer accept HMAC-SHA1 but require HMAC-SHA256 for URL signing.
@katylava @swoodford @akhileshmosale I've successfully created function to generate these URLs! You can find it here: https://github.com/kmotrebski/gas-s3-url-generator
It has same functionality as the one here in this gist but it works for current way of signing URLs at S3.
Under the hood it's quite complex compared to this one but AFAIK the signing algorithm has changed. Currently supported version is V4. V2 is no longer supported for S3 - I hope so as I've spent some time to create this function :) My code directly follows exemplary case from AWS documentation here. All variable names are identical.
I hope it is some help!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the error i am getting
"The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256."
What is the solution?
I also tried using "var hmac = Utilities.computeHmacSha256Signature(stringToSign, AWS_SECRET, Utilities.Charset.UTF_8);" but still got the same error