Upload Image (PNG file) from Google App Script to S3 Bucket
Trying to upload a png file:
// get the image blob
const imgBlob = UrlFetchApp.fetch('imageUrl').getBlob();
// init S3 instance
const s3 = S3.getInstance(awsAccessKeyId, awsSecretKey);
// upload the image to S3 bucket
s3.putObject(bucketName, 'test.png', imgBlob, { logRequests:true });
File is uploading in S3 but not in perfect way! It's look like:

If I download the image and open getting the error:
It may be damaged or use a file format that Preview doesn’t recognize.
I also had the same issue, I think it's because the Blob gets converted to a string before being sent. I managed to get it working by making the following changes:
- S3.gs line 142: change
request.setContent(object.getDataAsString());torequest.setContent(object); - S3Request.gs line 60: remove the line
if (typeof content != 'string') throw 'content must be passed as a string' - S3Request.gs line 393: replace the function with the following:
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()));
}
I also had the same issue, I think it's because the
Blobgets converted to astringbefore being sent. I managed to get it working by making the following changes:
- S3.gs line 142: change
request.setContent(object.getDataAsString());torequest.setContent(object);- S3Request.gs line 60: remove the line
if (typeof content != 'string') throw 'content must be passed as a string'- S3Request.gs line 393: replace the function with the following:
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())); }
Thanks! This is a lifesaver! I have been experiencing the issue of corrupted files for 2 days now.
I also had the same issue, I think it's because the
Blobgets converted to astringbefore being sent. I managed to get it working by making the following changes:
- S3.gs line 142: change
request.setContent(object.getDataAsString());torequest.setContent(object);- S3Request.gs line 60: remove the line
if (typeof content != 'string') throw 'content must be passed as a string'- S3Request.gs line 393: replace the function with the following:
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())); }
Thanks it saved my life
I also had the same issue, I think it's because the
Blobgets converted to astringbefore being sent. I managed to get it working by making the following changes:
- S3.gs line 142: change
request.setContent(object.getDataAsString());torequest.setContent(object);- S3Request.gs line 60: remove the line
if (typeof content != 'string') throw 'content must be passed as a string'- S3Request.gs line 393: replace the function with the following:
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())); }
Thank you so much for this!!
For anyone still looking for a solution, I have published S3Patches, which include hotfix. You can install this library using App Script ID:
1dVPQ-c-2Clh_rQ0JS3xiMx5eAWSeKVqiKy4U-oI0yuwjg8AljIH4b3VL
I also had the same issue, I think it's because the
Blobgets converted to astringbefore being sent. I managed to get it working by making the following changes:
- S3.gs line 142: change
request.setContent(object.getDataAsString());torequest.setContent(object);- S3Request.gs line 60: remove the line
if (typeof content != 'string') throw 'content must be passed as a string'- S3Request.gs line 393: replace the function with the following:
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())); }