Search in sources :

Example 31 with S3Object

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);
    }
}
Also used : ServiceException(org.jets3t.service.ServiceException) S3ServiceException(org.jets3t.service.S3ServiceException) BufferedInputStream(java.io.BufferedInputStream) S3Object(org.jets3t.service.model.S3Object) FileInputStream(java.io.FileInputStream)

Example 32 with S3Object

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) RestS3Service(org.jets3t.service.impl.rest.httpclient.RestS3Service) S3Object(org.jets3t.service.model.S3Object) AWSCredentials(org.jets3t.service.security.AWSCredentials) RestS3Service(org.jets3t.service.impl.rest.httpclient.RestS3Service) S3Service(org.jets3t.service.S3Service)

Example 33 with S3Object

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);
    }
}
Also used : S3Bucket(org.jets3t.service.model.S3Bucket) RestS3Service(org.jets3t.service.impl.rest.httpclient.RestS3Service) S3Object(org.jets3t.service.model.S3Object) AWSCredentials(org.jets3t.service.security.AWSCredentials) RestS3Service(org.jets3t.service.impl.rest.httpclient.RestS3Service) S3Service(org.jets3t.service.S3Service) Date(java.util.Date)

Example 34 with S3Object

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();
}
Also used : S3Bucket(org.jets3t.service.model.S3Bucket) S3Object(org.jets3t.service.model.S3Object) Test(org.junit.Test)

Example 35 with S3Object

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();
}
Also used : S3Bucket(org.jets3t.service.model.S3Bucket) S3Object(org.jets3t.service.model.S3Object) Test(org.junit.Test)

Aggregations

S3Object (org.jets3t.service.model.S3Object)39 Test (org.junit.Test)16 S3ServiceException (org.jets3t.service.S3ServiceException)11 ServiceException (org.jets3t.service.ServiceException)11 RestS3Service (org.jets3t.service.impl.rest.httpclient.RestS3Service)11 S3Bucket (org.jets3t.service.model.S3Bucket)9 Date (java.util.Date)8 File (java.io.File)6 IOException (java.io.IOException)6 DataSegment (io.druid.timeline.DataSegment)5 URI (java.net.URI)5 StorageObject (org.jets3t.service.model.StorageObject)5 InputStream (java.io.InputStream)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 Pattern (java.util.regex.Pattern)3 S3Service (org.jets3t.service.S3Service)3 AWSCredentials (org.jets3t.service.security.AWSCredentials)3 FileUtils (io.druid.java.util.common.FileUtils)2 BufferedReader (java.io.BufferedReader)2