3

How to upload an image to Amazon Simple Storage Service, also known as AWS S3, using Apex? Is it possible to emulate working Java code, using libraries, into Apex?

  1. Our AWS service doesn't accept HTTP POST, otherwise it would have been simple to convert this HTML example into Apex.

    Edit/clarification: Our vendor has intentionally disabled HTTP POST on the AWS service for reasons. Therefore, implementing the simple HTML POST form doesn't work, let alone implementing as a Visuaforce page or Apex HttpRequest. Here is the error message when implementing the simple HTML page.

    <Error> <Code>AccessDenied</Code> <Message>Access Denied</Message> <RequestId>encrypted code</RequestId> <HostId> encrypted code </HostId> </Error>

  2. I don't currently see the use of Force.com for AWS which I have installed.

Our vendor has recommended to take cue from the Java example.

package com.trax.aws.s3;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.apache.commons.io.IOUtils;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
public class S3Test {
    private static final String AWS_KEY = "AWS_KEY";
    private static final String AWS_SECRET_KEY = "AWS_SECRET_KEY";
    private static final String S3_BUCKET = "S3_BUCKET";
    private static final String S3_BASE_FOLDER = "S3_BASE_FOLDER";
    private static final String TEMP_FILE_NAME = "test_s3.txt";

    public static void main(String[] args) {
        AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials(AWS_KEY, AWS_SECRET_KEY));
        s3.setRegion(Region.getRegion(Regions.US_EAST_1));
        try {
            s3.putObject(S3_BUCKET, S3_BASE_FOLDER + "/" + TEMP_FILE_NAME, createSampleFile());
            S3Object object = s3.getObject(S3_BUCKET, S3_BASE_FOLDER + "/" + TEMP_FILE_NAME);
            S3ObjectInputStream objectContent = object.getObjectContent();
            IOUtils.copy(objectContent, System.out);
            IOUtils.closeQuietly(objectContent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static File createSampleFile() throws IOException {
        File file = new File(TEMP_FILE_NAME);
        file.deleteOnExit();

        Writer writer = new OutputStreamWriter(new FileOutputStream(file));
        writer.write("abcdefghijklmnopqrstuvwxyz\n");
        writer.write("01234567890112345678901234\n");
        writer.write("!@#$%^&*()-=[]{};':',.<>/?\n");
        writer.write("01234567890112345678901234\n");
        writer.write("abcdefghijklmnopqrstuvwxyz\n");
        writer.close();

        return file;
    }

}

Is it even possible to emulate this Java code into Apex?

3
  • 1
    Any idea why 1st option is not allowed for you ?why not build a VF for your task that does same as mentioned in your link? May 14, 2015 at 16:41
  • Thanks @MohithShrivastava. It's disabled by the vendor for their reasons. Therefore, even a simple HTML form POST won't work, let alone Visualforce page. I have edited my question to include this for future readers. I'll answer my own question if I find a solution. May 15, 2015 at 15:03
  • Please go through this link: developer.salesforce.com/page/…
    – ajinkyah
    May 15, 2015 at 16:03

1 Answer 1

0

I'm writing this as the correct answer for now: No, it's not sensible to emulate working Java code, using libraries, into Apex. I have recommended the vendor to allow HTML HTTP POST on the AWS bucket.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.