Hello readers, In this tutorial, I am going to show you how to Upload Files on Amazon s3 bucket through Salesforce Apex. An Amazon S3 bucket is a public cloud storage resource available in Amazon Web Services.
What is Amazon s3 bucket?
An Amazon S3 bucket is a public cloud storage resource available in Amazon Web Services’ (AWS) Simple Storage Service (S3), an object storage offering. Amazon S3 buckets, which are similar to file folders, store objects, which consist of data and its descriptive metadata.
With the help of the following code, you can upload file on Amazon S3 through Apex
Prerequisites
- Need account on AWS.
- Create a bucket on AWS.
- Bucket Name, Secret key, key, and the region is used for authentication
1- Create an Account:
Create an Account with the required details and Login to the Account.
2- Create a bucket on AWS.
A- After login to your AWS account. Goto Services and choose s3 as shown in the image.
B- Click on the Create bucket button as shown in the image.
C- Enter a Unique name, choose Reason and then click on the Next button.
D- The following screen will appear. Click on the Next button.
E-Click on the Next button.
F- The next screen that will appear will look like this.
Choose Create bucket button.
You can see your bucket is created.
Where is Access Key and Secret Key?
You can find your Access Key and Secret Key by going to the following location as shown in the below image.
Expand the Access keys (access key ID and secret access key) panel and copy Access and Secret Key.
Now Login to your Salesforce Account and write the following code into your apex class.
Attachment attach = [select Id,Body, ContentType, Name from Attachment limit 1 ]; String attachmentBody = EncodingUtil.base64Encode(attach.Body); String formattedDateString = Datetime.now().formatGMT('EEE, dd MMM yyyy HH:mm:ss z'); String key = '*******************'; String secret = '**********************************'; String bucketname = 'test-s3-service'; String host = 's3-us-west-1.amazonaws.com'; String method = 'PUT'; String filename = attach.Id + '-' + attach.Name; HttpRequest req = new HttpRequest(); req.setMethod(method); req.setEndpoint('https://' + bucketname + '.' + host + '/' + bucketname + '/' + filename); req.setHeader('Host', bucketname + '.' + host); req.setHeader('Content-Length', String.valueOf(attachmentBody.length())); req.setHeader('Content-Encoding', 'UTF-8'); req.setHeader('Content-type', attach.ContentType); req.setHeader('Connection', 'keep-alive'); req.setHeader('Date', formattedDateString); req.setHeader('ACL', 'public-read'); req.setBody(attachmentBody); String stringToSign = 'PUT\n\n' + attach.ContentType + '\n' + formattedDateString + '\n' + '/' + bucketname + '/' + bucketname + '/' + filename; String encodedStringToSign = EncodingUtil.urlEncode(stringToSign, 'UTF-8'); Blob mac = Crypto.generateMac('HMACSHA1', blob.valueof(stringToSign),blob.valueof(secret)); String signed = EncodingUtil.base64Encode(mac); String authHeader = 'AWS' + ' ' + key + ':' + signed; req.setHeader('Authorization',authHeader); String decoded = EncodingUtil.urlDecode(encodedStringToSign , 'UTF-8'); Http http = new Http(); HTTPResponse res = http.send(req); System.debug('*Resp:' + String.ValueOF(res.getBody())); System.debug('RESPONSE STRING: ' + res.toString()); System.debug('RESPONSE STATUS: ' + res.getStatus()); System.debug('STATUS_CODE: ' + res.getStatusCode());
Run the following code and verify by going to your aws account.
View More:
Conclusion:
I hope you understand how to upload files from Salesforce to Amazon s3 bucket. Please don’t hesitate to comment for any technical help.
Thank You.
canadian pharcharmy drug store near me the pharmacy
best drugstore eyeliner on line pharmacy ed meds
pharmacy today pharmacy drugstore online mens ed pills
24 hours pharmacy online drugstore drugstore
best ed medication on line pharmacy best erectile dysfunction pills
legit online pharmacy the peoples pharmacy rx pharmacy
You’re so awesome! I do not believe I’ve truly read something like this before. So nice to find someone with some unique thoughts on this subject. Seriously.. thank you for starting this up. This web site is one thing that is required on the internet, someone with a little originality!|
I used to be recommended this blog by means of my cousin. I’m now not sure whether this submit is written by means of him as nobody else recognise such particular about my difficulty. You are amazing! Thanks!|