Uploading a file I downloaded from Sharepoint to S3 Bucket

0

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.setMethod('GET');
        req.setHeader('Accept', 'application/json;odata=verbose');
        req.setHeader('Authorization', 'Bearer ' + accessToken.access_token);
        Http h = new Http();
        HTTPResponse res = h.send(req);
        uploadToAWS(res.getBody());
}

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.setMethod(method);
        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');
        req.setBodyAsBlob(EncodingUtil.base64Decode(attachmentBody));

        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?

posta 2 anni fa619 visualizzazioni
1 Risposta
1
Risposta accettata

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.

con risposta 2 anni fa
profile picture
ESPERTO
verificato 11 giorni fa
profile picture
ESPERTO
verificato un anno fa

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande