Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sajibcse68 opened this issue Apr 28, 2020 · 4 comments
Open

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

sajibcse68 opened this issue Apr 28, 2020 · 4 comments

Comments

@sajibcse68
Copy link

sajibcse68 commented Apr 28, 2020

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.

@hizkifw
Copy link

hizkifw commented Mar 11, 2021

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

tomkeysers added a commit to tomkeysers/S3-for-Google-Apps-Script that referenced this issue Mar 18, 2022
upload images instead of string (@link eschultink#8 (comment))
tomkeysers added a commit to tomkeysers/S3-for-Google-Apps-Script that referenced this issue Mar 18, 2022
edit to upload images instead of string (@link eschultink#8 (comment))
@krizten
Copy link

krizten commented Mar 23, 2022

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.

@f2016862
Copy link

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

@HumbertoTello
Copy link

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!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants