4

Doing this with S3 SDK makes it simple. But want to go with S3 REST API(Read some advantages with this).

I have gone through with S3 API documentation and find difficult to code using it. I am totally new to this type of coding wherein it uses Request Parameters, Request Headers, Response Headers, Authorization, Error Codes, ACL etc. It also provided sample examples but could not find a way how to use those examples and do coding.

Can any one help where to start and end so that I can code for all CRUD operations on S3 using API. An example for uploading image file will helps me in better understanding.

1
  • Dealing with the S3 API is a headache. I recommend using the jets3t library if possible. Commented Jul 30, 2018 at 7:53

2 Answers 2

0

I am putting some basic code snippets below, that you can easily integrate in your code.

Getting s3 client:

private AmazonS3 getS3Client() {
    AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(credentials)
            .withAccelerateModeEnabled(true).withRegion(Regions.US_EAST_1).build();
    return s3Client;
}

Uploading file:

public void processOutput(FileServerDTO fileRequest) {

    try {
        AmazonS3 s3Client = getS3Client();
        s3Client.putObject(fileRequest.getBucketName(), fileRequest.getKey(), fileRequest.getFileContent(), null);
    } catch (Exception e) {
        logger.error("Exception while uploading file" + e.getMessage());
        throw e;
    }
}

Downloading File:

public byte[] downloadFile(FileServerDTO fileRequest) {
    AmazonS3 s3Client = getS3Client();
    S3Object s3object = s3Client.getObject(new GetObjectRequest(fileRequest.getBucketName(), fileRequest.getKey()));
    S3ObjectInputStream inputStream = s3object.getObjectContent();
    try {
        return FileCopyUtils.copyToByteArray(inputStream);
    } catch (Exception e) {
        logger.error("Exception while downloading file" + e.getMessage());
    }
    return null;
}

FileServerDTO contains basic attributes related to file info. You can easily use these util methods in your service.

Sign up to request clarification or add additional context in comments.

1 Comment

sCom: Please refer the comments I provided to @JOE DAI
-1

If you go through S3 services you will get better understanding of how S3 services works here are some example how to create upload delete files form S3 server using S3 servies:-

1) how to use Amazon’s S3 storage with the Java API

2) S3 Docs

There is brief explanation how it works.

1 Comment

Ravindra: Please refer the comments I provided to @JOE DAI

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.