1

I had uploaded a .txt file to AWS S3 bucket. on opening file, it showed data like

aHR0cHM6Ly90ZXN0LnNhbGVzZm9yY2UuY29tLw==

If we pass this string data aHR0cHM6Ly90ZXN0LnNhbGVzZm9yY2UuY29tLw== based on the util provided by https://gist.github.com/nxtr/bca4984cd3753261995c421711729629 displayed actual correct data present in the txt file as "https://test.salesforce.com/".

So how can we upload file .txt into AWS S3 bucket, that actually displays correct data "https://test.salesforce.com/" instead of aHR0cHM6Ly90ZXN0LnNhbGVzZm9yY2UuY29tLw==

Can anyone please help. thanks,

Thanks for reply @sfdcfox these are my observations 1) file uploaded successfully, but with above issues, this is the request

String url = 'https://' + this.bucketName + '.s3.amazonaws.com/' + this.folderName + '/' + this.fileName;   
        HttpRequest req = new HttpRequest();   
        req.setEndpoint(url);
        req.setMethod('PUT');
        req.setHeader('X-Amz-Content-SHA256',  this.getXAmzContent());
        req.setHeader('X-Amz-Date', this.timestamp);
        req.setHeader('Authorization', this.getPutAuthHeader());
        req.setBody(body);

2) Then tried your code,

String url = 'https://' + this.bucketName + '.s3.amazonaws.com/' + this.folderName + '/' + this.fileName;   
        HttpRequest req = new HttpRequest();   
        req.setEndpoint(url);
        req.setMethod('PUT');
        req.setHeader('X-Amz-Content-SHA256',  this.getXAmzContent());
        req.setHeader('X-Amz-Date', this.timestamp);
        req.setHeader('Authorization', this.getPutAuthHeader());
        Blob testBlob = EncodingUtil.base64Decode(body);
        if(testBlob.size() > 0) {
        req.setHeader('Content-Length', String.valueOf(testBlob.size()));
        req.setBodyAsBlob(testBlob);
       }

getting this error

<Code>XAmzContentSHA256Mismatch</Code><Message>The provided 'x-amz-content-sha256' header does not match what was computed.</Message><ClientComputedContentSHA256>

What mistake I making ?

0

You need to use HttpRequest.setBodyAsBlob. Presumably, you're currently using EncodingUtil.bsae64Encode followed by HttpRequest.setBody, which will result in the encoding that you see. In my AWS code, that part of the code looks like:

    if(payload.size() > 0) {
        setHeader('Content-Length', String.valueOf(payload.size()));
        request.setBodyAsBlob(payload);
    }
| improve this answer | |
  • thanks @sfdcfox, tried your code, but getting header errors for XAmzContentSHA256Mismatch – Sunrise487 Jan 10 at 4:46
  • @Sunrise487 Then you're calculating it incorrectly. Did you take a look at my code? Also, I'm using v4 of the API, make sure you're not trying to use v2. – sfdcfox Jan 10 at 4:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.