1

My objective is to successfully do a simple GET request from Amazon S3 with our Salesforce Instance.

This is my apex class.

public class AWSWebService {

    Public  static AWS_Keys1__c awsKeySet= AWS_Keys1__c.getInstance('AWSUPLOAD');
    String Content_Type;
    String acessType;
    String sigver;
    String formattedexpire;

    public AWSWebService() {
        Content_Type ='multipart/form-data';
        acessType ='public-read-write';

        Datetime expire = system.now().addDays(1);
        formattedexpire = expire.formatGmt('yyyy-MM-dd')+'T'+ expire.formatGmt('HH:mm:ss')+'.'+expire.formatGMT('SSS')+'Z';        
    }

    string policy { 

        get {

            return 
                '{ "expiration": "'+formattedexpire+'","conditions": [ {"bucket": "AAAAA" } ,{ "acl": "'+acessType+'" },'+
                '{"content-type":"'+Content_Type+'"},'+
                '{"success_action_redirect": ""},' +
                '["starts-with", "$key",""] ]}';               
        }
    }

    public String getPolicy() {
        return EncodingUtil.base64Encode(Blob.valueOf(policy));
    }

    public String getSignedPolicy() {    
        return make_sig(EncodingUtil.base64Encode(Blob.valueOf(policy)));        
    }

    private String make_sig(string canonicalBuffer) {

        if(canonicalBuffer == null || awsKeySet == null) {

            System.debug('~~~ came here evaluated null');
            System.debug('~~~ canonicalbuffer = '+ canonicalBuffer);
            System.debug('~~~ awsKeySet : '+ awsKeySet);
            return null;
        }

        String macUrl ;

        Blob mac = Crypto.generateMac('HmacSHA1', blob.valueof(canonicalBuffer),blob.valueof(awsKeySet.AWS_Secret_Key__c));
        macUrl = EncodingUtil.base64Encode(mac);                
        return macUrl;

    }

    public void invokeWS() {
        String endpoint = 'https://s3.amazonaws.com';
        String bucket = 'AAAAA';
        String AWSAccessKeyId=awsKeySet.AWS_AccessKey_Id__c ;
        String timestamp = datetime.now().formatGmt('EEE, d MMM yyyy HH:mm:ss Z');

        Http http=new Http();
        HttpRequest request=new HttpRequest();
        request.setEndpoint(endpoint);
        request.setMethod('GET');

        request.setHeader('Host',bucket+'.'+'s3.amazonaws.com');
        request.setHeader('Date',timestamp);
        request.setHeader('Authorization','AWS '+AWSAccessKeyId+':'+getSignedPolicy());

        HttpResponse res = http.send(request);
        system.debug('Body is'+res.getBody());
    }

}

When I did an anon apex like below

AWSWebService aws = new AWSWebService();
aws.invokeWS();

I receive the following response.

19:43:47:000 USER_DEBUG
 <Error>
<Code>TemporaryRedirect</Code>
<Message>Please re-send this request to the specified temporary endpoint. Continue to use the original request endpoint for future requests.</Message>
<Bucket>AAAAA</Bucket>
<Endpoint>AAAAA.s3-ap-southeast-2.amazonaws.com</Endpoint>
<RequestId>EF76D1B4CA2845CC</RequestId>
<HostId>0CY824msBbu7a48FE8wAAAAADWPBP9YCndSMYihyA+uDtSD3N6GrfPYbMA133sg/jZRwftLJST4=</HostId>
</Error>

What does this error mean ?. What is meant by temporary redirect ?.

Can someone explain and help me out ?

Your Answer

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

Browse other questions tagged or ask your own question.