Uploading a file I downloaded from Sharepoint to S3 Bucket


I am attempting to download a file from SharePoint through their Rest API, and then upload that file to my S3 Bucket. I can "successfully" download the file and upload to S3. The issue is when I then go to my bucket and look at the file it's corrupted.

(This code is in Apex, since I'm attempting to do this in Salesforce)

My code:

public static void uploadFile() {
        AccessTokenResponse accessToken = getAccessToken();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('<Sharepoint path>/_api/Web/GetFileByServerRelativePath(decodedurl=\'/sites/<channelname>/Shared%20Documents/General/TestFile.docx\')//OpenBinaryStream()');
        req.setHeader('Accept', 'application/json;odata=verbose');
        req.setHeader('Authorization', 'Bearer ' + accessToken.access_token);
        Http h = new Http();
        HTTPResponse res = h.send(req);

public static void uploadToAWS(String body) {
       Blob bodyBlob = Blob.valueOf(body);
        String attachmentBody = EncodingUtil.base64Encode(bodyBlob);
        String formattedDateString = Datetime.now().formatGMT('EEE, dd MMM yyyy HH:mm:ss z');
        String key = '<key>';
        String secret = '<secret>';
        String bucketname = '<bucket name>';
        String host = 's3.us-east-1.amazonaws.com';
        String method = 'PUT';
        String filename = 'TestFile.docx';

        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://' + bucketname + '.' + host + '/' + filename);
        req.setHeader('Host', bucketname + '.' + host);
        req.setHeader('Content-Length', String.valueOf(attachmentBody.length()));
        req.setHeader('Content-Encoding', 'UTF-8');
        req.setHeader('Content-type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        req.setHeader('Connection', 'keep-alive');
        req.setHeader('Date', formattedDateString);
        req.setHeader('ACL', 'public-read-write');

        String stringToSign = 'PUT\n\napplication/vnd.openxmlformats-officedocument.wordprocessingml.document\n' + formattedDateString + '\n' + '/' + bucketname + '/' + filename;
        String encodedStringToSign = EncodingUtil.urlEncode(stringToSign, 'UTF-8');
        Blob mac = Crypto.generateMac('HMACSHA1', blob.valueof(stringToSign),blob.valueof(secret));
        String signedKey  = EncodingUtil.base64Encode(mac);
        String authHeader = 'AWS' + ' ' + key + ':' + signedKey ;
        req.setHeader('Authorization', authHeader);

        Http http = new Http();
        HTTPResponse res = http.send(req);

I can go to my bucket in S3 and see that a file was uploaded by that name. When I download the file and try to open it though it says its corrupted. I'm not sure how to debug anymore.

I've tried with both a docx and a pdf, with the same corrupted result.

Any idea on why my file is corrupted, or either what I should be doing to fix it or how I can debug more to understand where things are failing?

asked 2 years ago647 views
1 Answer
Accepted Answer

This was answered on another forum, but for anyone looking at the answer: In the last line of my uploadFile method, I call res.getBody, which converts the response to UTF-8. If I instead call res.getBodyAsBlob and send that to my function to upload to AWS, then it works correctly.

answered 2 years ago
profile picture
reviewed 25 days ago
profile picture
reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions