S3-for-Google-Apps-Script icon indicating copy to clipboard operation
S3-for-Google-Apps-Script copied to clipboard

Upload Image (PNG file) from Google App Script to S3 Bucket

Open sajibcse68 opened this issue 5 years ago • 5 comments

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:

Screenshot 2020-04-29 at 5 18 18 AM

If I download the image and open getting the error:

It may be damaged or use a file format that Preview doesn’t recognize.

sajibcse68 avatar Apr 28 '20 23:04 sajibcse68

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()); to request.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()));
}

hizkifw avatar Mar 11 '21 17:03 hizkifw

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()); to request.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.

krizten avatar Mar 23 '22 09:03 krizten

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()); to request.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

f2016862 avatar Aug 10 '22 18:08 f2016862

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()); to request.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!!

HumbertoTello avatar Sep 29 '23 22:09 HumbertoTello

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 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()); to request.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()));
}

chipcop106 avatar Jul 12 '24 03:07 chipcop106