Monday, 19 January 2015

Java Example to upload file to Amazon S3 Bucket


This example needs android sdk jars to be included in libs 
Following link sdk is available

http://aws.amazon.com/mobile/sdk/

import java.io.File;
import java.io.IOException;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.PutObjectRequest;

public class UploadObjectSingleOperation {
//This should be already created in the Amazon S3
    private static String bucketName = "sample";
    private static String keyName = "Photo.JPEG";
// windows file path
//    private static String uploadFileName = "D://Pics//mypic.JPG";
//Mac file path
    private static String uploadFileName = "/Users/xxx/Pictures/Photo.jpg";
    public static void main(String[] args) throws IOException {
       
        AmazonS3 s3client = new AmazonS3Client(new BasicAWSCredentials("accesskey", "secretkey"));
        try {
            System.out.println("Uploading a new object to S3 from a file\n");
            File file = new File(uploadFileName);
            s3client.putObject(new PutObjectRequest(bucketName, keyName, file));
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, which "
                    + "means your request made it "
                    + "to Amazon S3, but was rejected with an error response"
                    + " for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, which "
                    + "means the client encountered "
                    + "an internal error while trying to "
                    + "communicate with S3, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());
        }
    }
}


SourceCode

1 comment:

  1. Appium training in Chennai
    We provide best appium training in Chennai with real time scenarios. For real time training reach us 8122241286 and become experts in appium.
    Appium training in chennai

    ReplyDelete

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...