use of com.amazonaws.services.s3.model.ObjectMetadata 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.ObjectMetadata in project h2o-3 by h2oai.
the class PersistS3 method uriToKey.
@Override
public Key uriToKey(URI uri) throws IOException {
AmazonS3 s3 = getClient();
// Decompose URI into bucket, key
String[] parts = decodePath(uri.toString());
try {
ObjectMetadata om = s3.getObjectMetadata(parts[0], parts[1]);
// Voila: create S3 specific key pointing to the file
return S3FileVec.make(encodePath(parts[0], parts[1]), om.getContentLength());
} catch (AmazonServiceException e) {
if (e.getErrorCode().contains("404")) {
throw new IOException(e);
} else {
Log.err("AWS failed for " + Arrays.toString(parts) + ": " + e.getMessage());
throw e;
}
}
}
use of com.amazonaws.services.s3.model.ObjectMetadata in project gradle by gradle.
the class S3Client method put.
public void put(InputStream inputStream, Long contentLength, URI destination) {
checkRequiredJigsawModuleIsOnPath();
try {
S3RegionalResource s3RegionalResource = new S3RegionalResource(destination);
String bucketName = s3RegionalResource.getBucketName();
String s3BucketKey = s3RegionalResource.getKey();
configureClient(s3RegionalResource);
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(contentLength);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, s3BucketKey, inputStream, objectMetadata);
LOGGER.debug("Attempting to put resource:[{}] into s3 bucket [{}]", s3BucketKey, bucketName);
amazonS3Client.putObject(putObjectRequest);
} catch (AmazonClientException e) {
throw ResourceExceptions.putFailed(destination, e);
}
}
use of com.amazonaws.services.s3.model.ObjectMetadata in project alluxio by Alluxio.
the class S3AUnderFileSystem method createEmptyObject.
@Override
protected boolean createEmptyObject(String key) {
try {
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(0);
meta.setContentMD5(DIR_HASH);
meta.setContentType(Mimetypes.MIMETYPE_OCTET_STREAM);
mClient.putObject(new PutObjectRequest(mBucketName, key, new ByteArrayInputStream(new byte[0]), meta));
return true;
} catch (AmazonClientException e) {
LOG.error("Failed to create object: {}", key, e);
return false;
}
}
use of com.amazonaws.services.s3.model.ObjectMetadata in project alluxio by Alluxio.
the class S3AOutputStream method close.
@Override
public void close() throws IOException {
if (mClosed) {
return;
}
mLocalOutputStream.close();
String path = getUploadPath();
try {
// Generate the object metadata by setting server side encryption, md5 checksum, the file
// length, and encoding as octet stream since no assumptions are made about the file type
ObjectMetadata meta = new ObjectMetadata();
if (SSE_ENABLED) {
meta.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
}
if (mHash != null) {
meta.setContentMD5(new String(Base64.encode(mHash.digest())));
}
meta.setContentLength(mFile.length());
meta.setContentEncoding(Mimetypes.MIMETYPE_OCTET_STREAM);
// Generate the put request and wait for the transfer manager to complete the upload, then
// delete the temporary file on the local machine
PutObjectRequest putReq = new PutObjectRequest(mBucketName, path, mFile).withMetadata(meta);
mManager.upload(putReq).waitForUploadResult();
if (!mFile.delete()) {
LOG.error("Failed to delete temporary file @ {}", mFile.getPath());
}
} catch (Exception e) {
LOG.error("Failed to upload {}. Temporary file @ {}", path, mFile.getPath());
throw new IOException(e);
}
// Set the closed flag, close can be retried until mFile.delete is called successfully
mClosed = true;
}
Aggregations