CSEG Samples for Amazon S3
FileUpload
This sample shows how to upload file to AWS (Amazon Web Server) S3 server using CSEG WebServices. This sample shows how to use FileUpload using Http Client.
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(inputs.get("url"));
try {
HttpEntity reqEntity = createEntity(inputs); httppost.setEntity(reqEntity);
HttpResponse response = httpClient.execute(httppost); HttpEntity resEntity = response.getEntity();
System.out.println(EntityUtils.toString(resEntity));
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {e.printStackTrace();
} finally {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static HttpEntity createEntity(Map inputs) {HttpEntity reqEntity = null;
if (inputs.get("certPassword") != null && inputs.get("alias") != null) {
reqEntity = MultipartEntityBuilder.create().addTextBody("accessKey", inputs.get("awsKey"))
// AWS access key
.addTextBody("secretKey", inputs.get("awsSecretKey"))
// AWS secret key
.addTextBody("bucketName", inputs.get("bucket"))
// bucket on the AWS
.addTextBody("region", inputs.get("region"))
// AWS region
.addTextBody("ksUserName", inputs.get("user"))
// KS user name
.addTextBody("ksPassword", inputs.get("password"))
// KS password
.addTextBody("keyName", inputs.get("key"))
// Key name on the KS
.addTextBody("name", inputs.get("fileName"))
// file will be stored on S3 as per this name
.addBinaryBody("file", new File(inputs.get("filepath")),
// file to be upload
ContentType.MULTIPART_FORM_DATA, inputs.get("filepath"))
//true if client side encryption or false for server side operation
.addTextBody("isClientSide", inputs.get("isClientSide") == null ? "true" : inputs.get("isClientSide"))
//for client side transformation either AES or RSA
.addTextBody("transformation", inputs.get("transformation") == null ? "AES" : inputs.get("transformation"))
.addTextBody("certAlias", inputs.get("alias"))
// cert alias for
// SSL connection towards KS
.addTextBody("certPassword", inputs.get("certPassword"))
.addTextBody("canKeyRotate", inputs.get("canKeyRotate") == null ? "true" : inputs.get("canKeyRotate"))
.build();
} else {
reqEntity = MultipartEntityBuilder.create()
.addTextBody("accessKey", inputs.get("awsKey"))
// AWS access key
.addTextBody("secretKey", inputs.get("awsSecretKey"))
// AWS secret key
.addTextBody("bucketName", inputs.get("bucket"))
// bucket on the AWS
.addTextBody("region", inputs.get("region"))
// AWS region
.addTextBody("ksUserName", inputs.get("user"))
// KS user name
.addTextBody("ksPassword", inputs.get("password"))
// KS password
.addTextBody("keyName", inputs.get("key"))
// Key name on the KS
.addTextBody("name", inputs.get("fileName"))
// file will be stored on S3 as per this name
.addBinaryBody("file", new File(inputs.get("filepath")),
// file to be upload
ContentType.MULTIPART_FORM_DATA, inputs.get("filepath"))
//true if client side encryption or false for server side operation
.addTextBody("isClientSide", inputs.get("isClientSide") == null ? "true" : inputs.get("isClientSide"))
//for client side transformation either AES or RSA
.addTextBody("transformation", inputs.get("transformation") == null ? "AES" : inputs.get("transformation"))
.addTextBody("canKeyRotate", inputs.get("canKeyRotate") == null ? "true" : inputs.get("canKeyRotate"))
.build();
}
return reqEntity;
}
private static Map readArguments(String[] args) {
Map inputs = new HashMap();
for (String arg : args) {
String[] temp = arg.split("=");
System.out.println(Arrays.toString(temp));
inputs.put(temp[0], temp[1]);
}
return inputs;
}
private static void validateProperties(
Map input) {
StringBuffer buffer = new StringBuffer();
if (input.get("url") == null) {
buffer.append("url");
buffer.append(", ");
}
if (input.get("awsKey") == null) {
buffer.append("awsKey");
buffer.append(", ");
}
if (input.get("awsSecretKey") == null) {
buffer.append("awsSecretKey");
buffer.append(", ");
}
if (input.get("bucket") == null) {
buffer.append("bucket");
buffer.append(", ");
}
if (input.get("region") == null) {
buffer.append("region");
buffer.append(", ");
}
if (input.get("user") == null) {
buffer.append("user");
buffer.append(", ");
}
if (input.get("password") == null) {
buffer.append("password");
buffer.append(", ");
}
if (input.get("key") == null) {
buffer.append("key");
buffer.append(", ");
}
if (input.get("fileName") == null) {
buffer.append("fileName");
buffer.append(", ");
}
if (input.get("filepath") == null) {
buffer.append("filepath");
buffer.append(", ");
}
if (buffer.length() != 0) {
System.err.println("java FileUpload url=webserviceurl awsKey=accessKey awsSecretKey=secretKey"
+ " bucket=bucketName region=region user=ksUserName password=ksPassword"
+ " key=keyName fileName=filename filepath=filepath [isClientSide=isClientSide] [transformation=transformation] [alias=certalias]"
+ " [certPassword=certPassword] [canKeyRotate=canKeyRotate]");
System.exit(-1);
}
}
}
FileDownload
This sample shows how to download a file from AWS (Amazon Web Server) S3 server using CSEG WebServices. This sample shows how to use FileDownload using Http Client.
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(inputs.get("url"));
String destinationFile = null;
try {
URI uri = null;
uri = new URIBuilder(httpget.getURI())
.setParameter("accessKey", inputs.get("awsKey"))
// AWS access key
.setParameter("secretKey", inputs.get("awsSecretKey"))
// AWS secret key
.setParameter("bucketName", inputs.get("bucket"))
// bucket on the
// AWS
.setParameter("region", inputs.get("region"))
// AWS region
.setParameter("ksUserName", inputs.get("user"))
// KS user name
.setParameter("ksPassword", inputs.get("password"))
// KS password
.setParameter("keyName", inputs.get("key"))
// Key name on the KS
.setParameter("name", inputs.get("fileName"))
// name of the file to download
// true if client side encryption or false for server
// side operation
.setParameter("isClientSide", inputs.get("clientSide"))
// for client side transformation either AES or RSA
.setParameter("transformation",
inputs.get("transformation"))
.setParameter("certAlias", inputs.get("alias"))
// cert alias for
// SSL connection towards KS
.setParameter("certPassword", inputs.get("certPassword"))
.setParameter("version", inputs.get("version")).build();
// absolute path of destination including file name (ex:
// "C://user//downloads//text.txt") destinationFile = inputs.get("destination");
((HttpRequestBase) httpget).setURI(uri); HttpResponse response = httpClient.execute(httpget);
HttpEntity resEntity = response.getEntity();
if (response.getStatusLine().getStatusCode() == 200) {
FileOutputStream out = new FileOutputStream(new File(
destinationFile));
resEntity.writeTo(out);
out.close();
System.out.println("File downloaded successfully at location: "
+ destinationFile);
} else {
System.out.println("File not downloaded due to "
+ EntityUtils.toString(resEntity));
}
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static Map readArguments(String[] args) {
Map inputs = new HashMap();
for (String arg : args) {
String[] temp = arg.split("=");
System.out.println(Arrays.toString(temp));
inputs.put(temp[0], temp[1]);
}
return inputs;
}
private static void validateProperties(
Map input) {
StringBuffer buffer = new StringBuffer();
if (input.get("url") == null) {
buffer.append("url");
buffer.append(", ");
}
if (input.get("awsKey") == null) {
buffer.append("awsKey");
buffer.append(", ");
}
if (input.get("awsSecretKey") == null) {
buffer.append("awsSecretKey");
buffer.append(", ");
}
if (input.get("bucket") == null) {
buffer.append("bucket");
buffer.append(", ");
}
if (input.get("region") == null) {
buffer.append("region");
buffer.append(", ");
}
if (input.get("user") == null) {
buffer.append("user");
buffer.append(", ");
}
if (input.get("password") == null) {
buffer.append("password");
buffer.append(", ");
}
if (input.get("key") == null) {
buffer.append("key");
buffer.append(", ");
}
if (input.get("fileName") == null) {
buffer.append("fileName");
buffer.append(", ");
}
if (input.get("destination") == null) {
buffer.append("destination");
buffer.append(", ");
}
if (buffer.length() != 0) {
System.err.println("java FileDownload url=webserviceurl awsKey=accessKey awsSecretKey=secretKey"
+ " bucket=bucketName region=region user=ksUserName password=ksPassword"
+ " key=keyName fileName=filename destination=destinationFile [clientSide=isClientSide] [transformation=transformation] [alias=certalias]"
+ " [certPassword=certPassword] [version=version]");
System.exit(-1);
}
}
}