use of com.amazonaws.services.s3.model.SSECustomerKey in project YCSB by brianfrankcooper.
the class S3Client method readFromStorage.
/**
* Download an object from S3.
*
* @param bucket
* The name of the bucket
* @param key
* The file key of the object to upload/update.
* @param result
* The Hash map where data from the object are written
*
*/
protected Status readFromStorage(String bucket, String key, HashMap<String, ByteIterator> result, SSECustomerKey ssecLocal) {
try {
Map.Entry<S3Object, ObjectMetadata> objectAndMetadata = getS3ObjectAndMetadata(bucket, key, ssecLocal);
//consuming the stream
InputStream objectData = objectAndMetadata.getKey().getObjectContent();
// writing the stream to bytes and to results
int sizeOfFile = (int) objectAndMetadata.getValue().getContentLength();
byte[] inputStreamToByte = new byte[sizeOfFile];
objectData.read(inputStreamToByte, 0, sizeOfFile);
result.put(key, new ByteArrayByteIterator(inputStreamToByte));
objectData.close();
objectAndMetadata.getKey().close();
} catch (Exception e) {
System.err.println("Not possible to get the object " + key);
e.printStackTrace();
return Status.ERROR;
}
return Status.OK;
}
use of com.amazonaws.services.s3.model.SSECustomerKey in project YCSB by brianfrankcooper.
the class S3Client method init.
/**
* Initialize any state for the storage.
* Called once per S3 instance; If the client is not null it is re-used.
*/
@Override
public void init() throws DBException {
final int count = INIT_COUNT.incrementAndGet();
synchronized (S3Client.class) {
Properties propsCL = getProperties();
int recordcount = Integer.parseInt(propsCL.getProperty("recordcount"));
int operationcount = Integer.parseInt(propsCL.getProperty("operationcount"));
int numberOfOperations = 0;
if (recordcount > 0) {
if (recordcount > operationcount) {
numberOfOperations = recordcount;
} else {
numberOfOperations = operationcount;
}
} else {
numberOfOperations = operationcount;
}
if (count <= numberOfOperations) {
String accessKeyId = null;
String secretKey = null;
String endPoint = null;
String region = null;
String maxErrorRetry = null;
String maxConnections = null;
String protocol = null;
BasicAWSCredentials s3Credentials;
ClientConfiguration clientConfig;
if (s3Client != null) {
System.out.println("Reusing the same client");
return;
}
try {
InputStream propFile = S3Client.class.getClassLoader().getResourceAsStream("s3.properties");
Properties props = new Properties(System.getProperties());
props.load(propFile);
accessKeyId = props.getProperty("s3.accessKeyId");
if (accessKeyId == null) {
accessKeyId = propsCL.getProperty("s3.accessKeyId");
}
System.out.println(accessKeyId);
secretKey = props.getProperty("s3.secretKey");
if (secretKey == null) {
secretKey = propsCL.getProperty("s3.secretKey");
}
System.out.println(secretKey);
endPoint = props.getProperty("s3.endPoint");
if (endPoint == null) {
endPoint = propsCL.getProperty("s3.endPoint", "s3.amazonaws.com");
}
System.out.println(endPoint);
region = props.getProperty("s3.region");
if (region == null) {
region = propsCL.getProperty("s3.region", "us-east-1");
}
System.out.println(region);
maxErrorRetry = props.getProperty("s3.maxErrorRetry");
if (maxErrorRetry == null) {
maxErrorRetry = propsCL.getProperty("s3.maxErrorRetry", "15");
}
maxConnections = props.getProperty("s3.maxConnections");
if (maxConnections == null) {
maxConnections = propsCL.getProperty("s3.maxConnections");
}
protocol = props.getProperty("s3.protocol");
if (protocol == null) {
protocol = propsCL.getProperty("s3.protocol", "HTTPS");
}
sse = props.getProperty("s3.sse");
if (sse == null) {
sse = propsCL.getProperty("s3.sse", "false");
}
String ssec = props.getProperty("s3.ssec");
if (ssec == null) {
ssec = propsCL.getProperty("s3.ssec", null);
} else {
ssecKey = new SSECustomerKey(ssec);
}
} catch (Exception e) {
System.err.println("The file properties doesn't exist " + e.toString());
e.printStackTrace();
}
try {
System.out.println("Inizializing the S3 connection");
s3Credentials = new BasicAWSCredentials(accessKeyId, secretKey);
clientConfig = new ClientConfiguration();
clientConfig.setMaxErrorRetry(Integer.parseInt(maxErrorRetry));
if (protocol.equals("HTTP")) {
clientConfig.setProtocol(Protocol.HTTP);
} else {
clientConfig.setProtocol(Protocol.HTTPS);
}
if (maxConnections != null) {
clientConfig.setMaxConnections(Integer.parseInt(maxConnections));
}
s3Client = new AmazonS3Client(s3Credentials, clientConfig);
s3Client.setRegion(Region.getRegion(Regions.fromName(region)));
s3Client.setEndpoint(endPoint);
System.out.println("Connection successfully initialized");
} catch (Exception e) {
System.err.println("Could not connect to S3 storage: " + e.toString());
e.printStackTrace();
throw new DBException(e);
}
} else {
System.err.println("The number of threads must be less or equal than the operations");
throw new DBException(new Error("The number of threads must be less or equal than the operations"));
}
}
}
use of com.amazonaws.services.s3.model.SSECustomerKey in project YCSB by brianfrankcooper.
the class S3Client method writeToStorage.
/**
* Upload a new object to S3 or update an object on S3.
*
* @param bucket
* The name of the bucket
* @param key
* The file key of the object to upload/update.
* @param values
* The data to be written on the object
* @param updateMarker
* A boolean value. If true a new object will be uploaded
* to S3. If false an existing object will be re-uploaded
*
*/
protected Status writeToStorage(String bucket, String key, HashMap<String, ByteIterator> values, Boolean updateMarker, String sseLocal, SSECustomerKey ssecLocal) {
int totalSize = 0;
//number of fields to concatenate
int fieldCount = values.size();
// getting the first field in the values
Object keyToSearch = values.keySet().toArray()[0];
// getting the content of just one field
byte[] sourceArray = values.get(keyToSearch).toArray();
//size of each array
int sizeArray = sourceArray.length;
if (updateMarker) {
totalSize = sizeArray * fieldCount;
} else {
try {
Map.Entry<S3Object, ObjectMetadata> objectAndMetadata = getS3ObjectAndMetadata(bucket, key, ssecLocal);
int sizeOfFile = (int) objectAndMetadata.getValue().getContentLength();
fieldCount = sizeOfFile / sizeArray;
totalSize = sizeOfFile;
objectAndMetadata.getKey().close();
} catch (Exception e) {
System.err.println("Not possible to get the object :" + key);
e.printStackTrace();
return Status.ERROR;
}
}
byte[] destinationArray = new byte[totalSize];
int offset = 0;
for (int i = 0; i < fieldCount; i++) {
System.arraycopy(sourceArray, 0, destinationArray, offset, sizeArray);
offset += sizeArray;
}
try (InputStream input = new ByteArrayInputStream(destinationArray)) {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(totalSize);
PutObjectRequest putObjectRequest = null;
if (sseLocal.equals("true")) {
metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
putObjectRequest = new PutObjectRequest(bucket, key, input, metadata);
} else if (ssecLocal != null) {
putObjectRequest = new PutObjectRequest(bucket, key, input, metadata).withSSECustomerKey(ssecLocal);
} else {
putObjectRequest = new PutObjectRequest(bucket, key, input, metadata);
}
try {
PutObjectResult res = s3Client.putObject(putObjectRequest);
if (res.getETag() == null) {
return Status.ERROR;
} else {
if (sseLocal.equals("true")) {
System.out.println("Uploaded object encryption status is " + res.getSSEAlgorithm());
} else if (ssecLocal != null) {
System.out.println("Uploaded object encryption status is " + res.getSSEAlgorithm());
}
}
} catch (Exception e) {
System.err.println("Not possible to write object :" + key);
e.printStackTrace();
return Status.ERROR;
}
} catch (Exception e) {
System.err.println("Error in the creation of the stream :" + e.toString());
e.printStackTrace();
return Status.ERROR;
}
return Status.OK;
}
use of com.amazonaws.services.s3.model.SSECustomerKey in project YCSB by brianfrankcooper.
the class S3Client method scanFromStorage.
/**
* Perform an emulation of a database scan operation on a S3 bucket.
*
* @param bucket
* The name of the bucket
* @param startkey
* The file key of the first file to read.
* @param recordcount
* The number of files to read
* @param fields
* The list of fields to read, or null for all of them
* @param result
* A Vector of HashMaps, where each HashMap is a set field/value
* pairs for one file
*
*/
protected Status scanFromStorage(String bucket, String startkey, int recordcount, Vector<HashMap<String, ByteIterator>> result, SSECustomerKey ssecLocal) {
int counter = 0;
ObjectListing listing = s3Client.listObjects(bucket);
List<S3ObjectSummary> summaries = listing.getObjectSummaries();
List<String> keyList = new ArrayList();
int startkeyNumber = 0;
int numberOfIteration = 0;
// getting the list of files in the bucket
while (listing.isTruncated()) {
listing = s3Client.listNextBatchOfObjects(listing);
summaries.addAll(listing.getObjectSummaries());
}
for (S3ObjectSummary summary : summaries) {
String summaryKey = summary.getKey();
keyList.add(summaryKey);
}
// Sorting the list of files in Alphabetical order
// sorting the list
Collections.sort(keyList);
// Getting the position of the startingfile for the scan
for (String key : keyList) {
if (key.equals(startkey)) {
startkeyNumber = counter;
} else {
counter = counter + 1;
}
}
// if not using the total number of Files
if (recordcount < keyList.size()) {
numberOfIteration = recordcount;
} else {
numberOfIteration = keyList.size();
}
// of the Files or Till the recordcount number
for (int i = startkeyNumber; i < numberOfIteration; i++) {
HashMap<String, ByteIterator> resultTemp = new HashMap<String, ByteIterator>();
readFromStorage(bucket, keyList.get(i), resultTemp, ssecLocal);
result.add(resultTemp);
}
return Status.OK;
}
use of com.amazonaws.services.s3.model.SSECustomerKey in project YCSB by brianfrankcooper.
the class S3Client method getS3ObjectAndMetadata.
private Map.Entry<S3Object, ObjectMetadata> getS3ObjectAndMetadata(String bucket, String key, SSECustomerKey ssecLocal) {
GetObjectRequest getObjectRequest;
GetObjectMetadataRequest getObjectMetadataRequest;
if (ssecLocal != null) {
getObjectRequest = new GetObjectRequest(bucket, key).withSSECustomerKey(ssecLocal);
getObjectMetadataRequest = new GetObjectMetadataRequest(bucket, key).withSSECustomerKey(ssecLocal);
} else {
getObjectRequest = new GetObjectRequest(bucket, key);
getObjectMetadataRequest = new GetObjectMetadataRequest(bucket, key);
}
return new AbstractMap.SimpleEntry<>(s3Client.getObject(getObjectRequest), s3Client.getObjectMetadata(getObjectMetadataRequest));
}
Aggregations