use of org.jets3t.service.model.S3Object in project hadoop by apache.
the class Jets3tNativeFileSystemStore method storeFile.
@Override
public void storeFile(String key, File file, byte[] md5Hash) throws IOException {
if (multipartEnabled && file.length() >= multipartBlockSize) {
storeLargeFile(key, file, md5Hash);
return;
}
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(file));
S3Object object = new S3Object(key);
object.setDataInputStream(in);
object.setContentType("binary/octet-stream");
object.setContentLength(file.length());
object.setServerSideEncryptionAlgorithm(serverSideEncryptionAlgorithm);
if (md5Hash != null) {
object.setMd5Hash(md5Hash);
}
s3Service.putObject(bucket, object);
} catch (ServiceException e) {
handleException(e, key);
} finally {
IOUtils.closeStream(in);
}
}
use of org.jets3t.service.model.S3Object in project twitter-2-weibo by rjyo.
the class S3BackupTask method restore.
public void restore(String from) {
try {
AWSCredentials awsCredentials = new AWSCredentials(awsAccessKey, awsSecretAccessKey);
S3Service s3Service = new RestS3Service(awsCredentials);
S3Object object = s3Service.getObject("h2weibo.backup", from + ".json");
InputStream inputStream = object.getDataInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
String data = "";
while ((line = reader.readLine()) != null) {
data += line;
}
reader.close();
getHelper().restore(data);
} catch (Exception e) {
log.error("Failed to restore from S3.");
log.error(e);
}
}
use of org.jets3t.service.model.S3Object in project twitter-2-weibo by rjyo.
the class S3BackupTask method run.
public void run() {
String dump = getHelper().dump();
try {
AWSCredentials awsCredentials = new AWSCredentials(awsAccessKey, awsSecretAccessKey);
S3Service s3Service = new RestS3Service(awsCredentials);
S3Bucket backup = s3Service.getBucket("h2weibo.backup");
String dateString = DateFormatUtils.format(new Date(), "yyyy-MM-dd_HH");
S3Object object = new S3Object(dateString + ".json", dump);
object = s3Service.putObject(backup, object);
System.out.println("S3Object after upload: " + object);
} catch (Exception e) {
log.error("Failed to upload to S3.");
log.error(e);
}
}
use of org.jets3t.service.model.S3Object in project tutorials by eugenp.
the class JetS3tLiveTest method givenStringUploaded_StringIsDownloaded.
@Test
public void givenStringUploaded_StringIsDownloaded() throws Exception {
// Get a bucket
S3Bucket bucket = createBucket();
assertNotNull(bucket);
uploadStringData();
// Download
S3Object stringObject = s3Service.getObject(BucketName, TestStringName);
// Process stream into a string
String downloadedString = new BufferedReader(new InputStreamReader(stringObject.getDataInputStream())).lines().collect(Collectors.joining("\n"));
// Verify
assertTrue(TestString.equals(downloadedString));
// Clean up for next test
deleteObject(TestStringName);
deleteBucket();
}
use of org.jets3t.service.model.S3Object in project tutorials by eugenp.
the class JetS3tLiveTest method givenBinaryFileUploaded_FileIsDownloaded.
@Test
public void givenBinaryFileUploaded_FileIsDownloaded() throws Exception {
// get a bucket
S3Bucket bucket = createBucket();
assertNotNull(bucket);
// Put a binary file
S3Object fileObject = new S3Object(new File("src/test/resources/test.jpg"));
s3Service.putObject(BucketName, fileObject);
// Print info about type and name
log.info("Content type:" + fileObject.getContentType());
log.info("File object name is " + fileObject.getName());
// Download
S3Object newFileObject = s3Service.getObject(BucketName, "test.jpg");
// Save to a different name
File newFile = new File("src/test/resources/newtest.jpg");
Files.copy(newFileObject.getDataInputStream(), newFile.toPath(), REPLACE_EXISTING);
// Get hashes and compare
String origMD5 = getFileMD5("src/test/resources/test.jpg");
String newMD5 = getFileMD5("src/test/resources/newtest.jpg");
assertTrue(origMD5.equals(newMD5));
// Clean up
deleteObject("test.jpg");
deleteBucket();
}
Aggregations