Search in sources :

Example 16 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project jackrabbit-oak by apache.

the class S3Backend method read.

@Override
public InputStream read(DataIdentifier identifier) throws DataStoreException {
    long start = System.currentTimeMillis();
    String key = getKeyName(identifier);
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        S3Object object = s3service.getObject(bucket, key);
        InputStream in = object.getObjectContent();
        LOG.debug("[{}] read took [{}]ms", identifier, (System.currentTimeMillis() - start));
        return in;
    } catch (AmazonServiceException e) {
        throw new DataStoreException("Object not found: " + key, e);
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AmazonServiceException(com.amazonaws.AmazonServiceException) S3Object(com.amazonaws.services.s3.model.S3Object)

Example 17 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project herd by FINRAOS.

the class S3DaoTest method testGetPropertiesHandleGenericException.

@Test
public void testGetPropertiesHandleGenericException() throws Exception {
    S3Operations originalS3Operations = (S3Operations) ReflectionTestUtils.getField(s3Dao, "s3Operations");
    S3Operations mockS3Operations = mock(S3Operations.class);
    ReflectionTestUtils.setField(s3Dao, "s3Operations", mockS3Operations);
    JavaPropertiesHelper originalJavaPropertiesHelper = (JavaPropertiesHelper) ReflectionTestUtils.getField(s3Dao, "javaPropertiesHelper");
    JavaPropertiesHelper mockJavaPropertiesHelper = mock(JavaPropertiesHelper.class);
    ReflectionTestUtils.setField(s3Dao, "javaPropertiesHelper", mockJavaPropertiesHelper);
    try {
        String bucketName = "bucketName";
        String key = "key";
        S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
        S3Object s3Object = new S3Object();
        s3Object.setObjectContent(new ByteArrayInputStream(new byte[] { 0 }));
        when(mockS3Operations.getS3Object(any(), any())).thenReturn(s3Object);
        when(mockJavaPropertiesHelper.getProperties(any(InputStream.class))).thenThrow(new RuntimeException("message"));
        try {
            s3Dao.getProperties(bucketName, key, s3FileTransferRequestParamsDto);
            fail();
        } catch (Exception e) {
            assertEquals(RuntimeException.class, e.getClass());
            assertEquals("message", e.getMessage());
        }
    } finally {
        ReflectionTestUtils.setField(s3Dao, "s3Operations", originalS3Operations);
        ReflectionTestUtils.setField(s3Dao, "javaPropertiesHelper", originalJavaPropertiesHelper);
    }
}
Also used : JavaPropertiesHelper(org.finra.herd.dao.helper.JavaPropertiesHelper) S3FileTransferRequestParamsDto(org.finra.herd.model.dto.S3FileTransferRequestParamsDto) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) S3Object(com.amazonaws.services.s3.model.S3Object) MultiObjectDeleteException(com.amazonaws.services.s3.model.MultiObjectDeleteException) ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) IOException(java.io.IOException) Test(org.junit.Test)

Example 18 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project tutorials by eugenp.

the class S3Application method main.

public static void main(String[] args) throws IOException {
    // set-up the client
    AmazonS3 s3client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials)).withRegion(Regions.US_EAST_2).build();
    AWSS3Service awsService = new AWSS3Service(s3client);
    bucketName = "baeldung-bucket";
    // creating a bucket
    if (awsService.doesBucketExist(bucketName)) {
        System.out.println("Bucket name is not available." + " Try again with a different Bucket name.");
        return;
    }
    awsService.createBucket(bucketName);
    // list all the buckets
    for (Bucket s : awsService.listBuckets()) {
        System.out.println(s.getName());
    }
    // deleting bucket
    awsService.deleteBucket("baeldung-bucket-test2");
    // uploading object
    awsService.putObject(bucketName, "Document/hello.txt", new File("/Users/user/Document/hello.txt"));
    // listing objects
    ObjectListing objectListing = awsService.listObjects(bucketName);
    for (S3ObjectSummary os : objectListing.getObjectSummaries()) {
        System.out.println(os.getKey());
    }
    // downloading an object
    S3Object s3object = awsService.getObject(bucketName, "Document/hello.txt");
    S3ObjectInputStream inputStream = s3object.getObjectContent();
    FileUtils.copyInputStreamToFile(inputStream, new File("/Users/user/Desktop/hello.txt"));
    // copying an object
    awsService.copyObject("baeldung-bucket", "picture/pic.png", "baeldung-bucket2", "Document/picture.png");
    // deleting an object
    awsService.deleteObject(bucketName, "Document/hello.txt");
    // deleting multiple objects
    String[] objkeyArr = { "Document/hello2.txt", "Document/picture.png" };
    DeleteObjectsRequest delObjReq = new DeleteObjectsRequest("baeldung-bucket").withKeys(objkeyArr);
    awsService.deleteObjects(delObjReq);
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) Bucket(com.amazonaws.services.s3.model.Bucket) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) S3ObjectInputStream(com.amazonaws.services.s3.model.S3ObjectInputStream) S3Object(com.amazonaws.services.s3.model.S3Object) File(java.io.File) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest)

Example 19 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project spring-integration-aws by spring-projects.

the class S3Session method read.

@Override
public void read(String source, OutputStream outputStream) throws IOException {
    String[] bucketKey = splitPathToBucketAndKey(source, true);
    S3Object s3Object = this.amazonS3.getObject(bucketKey[0], bucketKey[1]);
    S3ObjectInputStream objectContent = s3Object.getObjectContent();
    try {
        StreamUtils.copy(objectContent, outputStream);
    } finally {
        objectContent.close();
    }
}
Also used : S3ObjectInputStream(com.amazonaws.services.s3.model.S3ObjectInputStream) S3Object(com.amazonaws.services.s3.model.S3Object)

Example 20 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project spring-integration-aws by spring-projects.

the class S3Session method readRaw.

@Override
public InputStream readRaw(String source) throws IOException {
    String[] bucketKey = splitPathToBucketAndKey(source, true);
    S3Object s3Object = this.amazonS3.getObject(bucketKey[0], bucketKey[1]);
    return s3Object.getObjectContent();
}
Also used : S3Object(com.amazonaws.services.s3.model.S3Object)

Aggregations

S3Object (com.amazonaws.services.s3.model.S3Object)76 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)22 InputStream (java.io.InputStream)22 IOException (java.io.IOException)21 S3ObjectInputStream (com.amazonaws.services.s3.model.S3ObjectInputStream)16 ByteArrayInputStream (java.io.ByteArrayInputStream)15 Test (org.junit.Test)15 File (java.io.File)12 FileInputStream (java.io.FileInputStream)12 AmazonS3 (com.amazonaws.services.s3.AmazonS3)11 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)11 AmazonServiceException (com.amazonaws.AmazonServiceException)10 Date (java.util.Date)9 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)8 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)8 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)7 S3Object (software.amazon.awssdk.services.s3.model.S3Object)7 AmazonClientException (com.amazonaws.AmazonClientException)6 FileOutputStream (java.io.FileOutputStream)6 Test (org.testng.annotations.Test)6