You can use the following sample code snippets as a reference:
-
Read VCAP_SERVICES
@Value("${vcap.services.objectstore.credentials.access_key_id}") String accessKeyId; @Value("${vcap.services.objectstore.credentials.secret_access_key}") String secretAccessKey; @Value("${vcap.services.objectstore.credentials.bucket}") String bucketName; @Value("${vcap.services.objectstore.credentials.host}") String endPoint; @Value("${vcap.services.objectstore.credentials.region}") String bucketRegion;
-
Get an Amazon S3 client
//Using setEndPoint method AWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretAccessKey); AmazonS3 s3client = null; s3client = new AmazonS3Client(credentials); s3client.setEndPoint(endPoint); //Using region method AWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretAccessKey); AmazonS3 s3client = null; s3client = new AmazonS3Client(credentials); s3client.setRegion(bucketRegion);
-
Upload an object
s3client.putObject(new PutObjectRequest(bucketName, objectName, file));
-
Download an object
s3client.getObject(new GetObjectRequest(bucketName, objectName));
-
Delete an object
s3client.deleteObject(bucketName, objectName);