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.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?
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.
Relevant questions
Uploading a file I downloaded from Sharepoint to S3 Bucket
Accepted Answerasked a month agoS3 Access denied on pdf file type only
asked 3 months agoWhere can I found the uri of my Bucket to connect my Symfony application with S3 and upload files?
asked 3 months agoHow do we unzip a file in S3 bucket using C#.NET
asked 2 months agoIs there an AWS service that can codelessly, serverlessly download an external file via HTTPS to an S3 bucket?
asked 2 months agoError: During file upload to S3 from Angular app
asked 3 years agoUploading a Dataframe to AWS S3 Bucket from SageMaker
Accepted Answerasked 3 years agoCan't download file from S3 bucket in another account
asked 2 years agoQuickSight: how to use data hosted on SharePoint?
asked 6 months agoHow to exacute virus scan before uploading the file to the S3 bucket
asked 40 minutes ago