Search in sources :

Example 16 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project BridgeServer2 by Sage-Bionetworks.

the class StudyConsentService method writeBytesToPublicS3.

/**
 * Write the byte array to a bucket at S3. The bucket will be given world read privileges, and the request
 * will be returned with the appropriate content type header for the document's MimeType.
 */
void writeBytesToPublicS3(@Nonnull String bucket, @Nonnull String key, @Nonnull byte[] data, @Nonnull MimeType type) throws IOException {
    try (InputStream dataInputStream = new ByteArrayInputStream(data)) {
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType(type.toString());
        PutObjectRequest request = new PutObjectRequest(bucket, key, dataInputStream, metadata).withCannedAcl(CannedAccessControlList.PublicRead);
        Stopwatch stopwatch = Stopwatch.createStarted();
        s3Client.putObject(request);
        logger.info("Finished writing to bucket " + bucket + " key " + key + " (" + data.length + " bytes) in " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Stopwatch(com.google.common.base.Stopwatch) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 17 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project BridgeServer2 by Sage-Bionetworks.

the class StudyConsentServiceTest method publishConsent.

@Test
public void publishConsent() throws Exception {
    // This is the document with the footer, where all the template variables have been removed
    String transformedDoc = "<doc><p>Document</p><table class=\"bridge-sig-block\"><tbody><tr>" + "<td><div class=\"label\">Name of Adult Participant</div></td><td><img brimg=\"\" " + "alt=\"\" onerror=\"this.style.display='none'\" src=\"cid:consentSignature\" /><div " + "class=\"label\">Signature of Adult Participant</div></td><td><div class=\"label\">" + "Date</div></td></tr><tr><td><div class=\"label\">Email, Phone, or ID</div></td><td>" + "<div class=\"label\">Sharing Option</div></td></tr></tbody></table></doc>";
    StudyConsent consent = StudyConsent.create();
    consent.setCreatedOn(CREATED_ON);
    consent.setSubpopulationGuid(SUBPOP_GUID.getGuid());
    when(mockDao.getConsent(SUBPOP_GUID, CREATED_ON)).thenReturn(consent);
    when(mockS3Helper.readS3FileAsString(CONSENT_BUCKET, consent.getStoragePath())).thenReturn(DOCUMENT);
    App app = App.create();
    Subpopulation subpop = Subpopulation.create();
    subpop.setGuid(SUBPOP_GUID);
    service.setConsentTemplate(new ByteArrayResource("<doc>${consent.body}</doc>".getBytes()));
    StudyConsentView result = service.publishConsent(app, subpop, CREATED_ON);
    assertEquals(result.getDocumentContent(), DOCUMENT + SIGNATURE_BLOCK);
    assertEquals(result.getCreatedOn(), CREATED_ON);
    assertEquals(result.getSubpopulationGuid(), SUBPOP_GUID.getGuid());
    assertEquals(result.getStudyConsent(), consent);
    verify(mockSubpopService).updateSubpopulation(eq(app), subpopCaptor.capture());
    assertEquals(subpopCaptor.getValue().getPublishedConsentCreatedOn(), CREATED_ON);
    verify(mockS3Client, times(2)).putObject(requestCaptor.capture());
    PutObjectRequest request = requestCaptor.getAllValues().get(0);
    assertEquals(request.getBucketName(), PUBLICATION_BUCKET);
    assertEquals(request.getCannedAcl(), PublicRead);
    assertEquals(IOUtils.toString(request.getInputStream(), UTF_8), transformedDoc);
    ObjectMetadata metadata = request.getMetadata();
    assertEquals(metadata.getContentType(), MimeType.HTML.toString());
    request = requestCaptor.getAllValues().get(1);
    assertEquals(request.getBucketName(), PUBLICATION_BUCKET);
    assertEquals(request.getCannedAcl(), PublicRead);
    // The PDF output isn't easily verified...
    metadata = request.getMetadata();
    assertEquals(metadata.getContentType(), MimeType.PDF.toString());
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) StudyConsent(org.sagebionetworks.bridge.models.subpopulations.StudyConsent) Subpopulation(org.sagebionetworks.bridge.models.subpopulations.Subpopulation) StudyConsentView(org.sagebionetworks.bridge.models.subpopulations.StudyConsentView) ByteArrayResource(org.springframework.core.io.ByteArrayResource) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) Test(org.testng.annotations.Test)

Example 18 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project proxima-platform by O2-Czech-Republic.

the class S3BlobPathTest method testRead.

@Test
public void testRead() throws IOException {
    final String message = "one two three four five six seven eight nine ten";
    final S3FileSystem fs = Mockito.mock(S3FileSystem.class);
    final AmazonS3 client = Mockito.mock(AmazonS3.class);
    Mockito.when(fs.client()).thenReturn(client);
    Mockito.when(fs.getSseCustomerKey()).thenReturn(new SSECustomerKey(SSE_KEY));
    final S3Object object = new S3Object();
    final byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
    object.setObjectContent(new ByteArrayInputStream(messageBytes));
    final ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(message.length());
    object.setObjectMetadata(objectMetadata);
    Mockito.when(client.getObject(Mockito.any(GetObjectRequest.class))).thenAnswer((Answer<S3Object>) invocationOnMock -> {
        final GetObjectRequest request = invocationOnMock.getArgument(0, GetObjectRequest.class);
        Assert.assertEquals(SSE_KEY, request.getSSECustomerKey().getKey());
        if (request.getRange() != null) {
            object.setObjectContent(new ByteArrayInputStream(Arrays.copyOfRange(messageBytes, (int) request.getRange()[0], (int) request.getRange()[1])));
        } else {
            object.setObjectContent(new ByteArrayInputStream(messageBytes));
        }
        return object;
    });
    // This is needed for file size lookup.
    Mockito.when(fs.getObject(Mockito.eq("name"))).thenReturn(object);
    final S3BlobPath blobPath = S3BlobPath.of(op.getContext(), fs, "name");
    try (final ReadableByteChannel channel = blobPath.read()) {
        Assert.assertTrue(channel instanceof SeekableByteChannel);
        final SeekableByteChannel cast = (SeekableByteChannel) channel;
        final ByteBuffer readBuffer = ByteBuffer.allocate(4096);
        readAll(cast, readBuffer);
        Assert.assertEquals(message, toString(readBuffer));
        Assert.assertEquals(messageBytes.length, cast.position());
        // Reset read buffer.
        readBuffer.rewind();
        // Seek to 'five' and re-read rest of the message.
        final String remaining = message.substring(message.indexOf("five"));
        cast.position(messageBytes.length - remaining.getBytes(StandardCharsets.UTF_8).length);
        readAll(cast, readBuffer);
        Assert.assertEquals(remaining, toString(readBuffer));
        // Read partial.
        final int oneLength = "one".getBytes(StandardCharsets.UTF_8).length;
        final ByteBuffer smallReadBuffer = ByteBuffer.allocate(oneLength);
        cast.position(0L);
        Assert.assertEquals(oneLength, cast.read(smallReadBuffer));
        // There are still bytes we can read, but there is no free space in receiving buffer left.
        Assert.assertEquals(0, cast.read(smallReadBuffer));
        Assert.assertEquals("one", toString(smallReadBuffer));
        // Common checks.
        Assert.assertTrue(cast.isOpen());
        Assert.assertEquals(messageBytes.length, cast.size());
    }
}
Also used : Context(cz.o2.proxima.direct.core.Context) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) ByteBuffer(java.nio.ByteBuffer) Answer(org.mockito.stubbing.Answer) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) S3Object(com.amazonaws.services.s3.model.S3Object) ConfigFactory(com.typesafe.config.ConfigFactory) AmazonS3(com.amazonaws.services.s3.AmazonS3) URI(java.net.URI) SSECustomerKey(com.amazonaws.services.s3.model.SSECustomerKey) ReadableByteChannel(java.nio.channels.ReadableByteChannel) Repository(cz.o2.proxima.repository.Repository) TestUtils(cz.o2.proxima.util.TestUtils) IOException(java.io.IOException) Test(org.junit.Test) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) SeekableByteChannel(java.nio.channels.SeekableByteChannel) Mockito(org.mockito.Mockito) S3Blob(cz.o2.proxima.direct.s3.S3BlobPath.S3Blob) WritableByteChannel(java.nio.channels.WritableByteChannel) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) AmazonS3(com.amazonaws.services.s3.AmazonS3) ReadableByteChannel(java.nio.channels.ReadableByteChannel) ByteBuffer(java.nio.ByteBuffer) SeekableByteChannel(java.nio.channels.SeekableByteChannel) SSECustomerKey(com.amazonaws.services.s3.model.SSECustomerKey) ByteArrayInputStream(java.io.ByteArrayInputStream) S3Object(com.amazonaws.services.s3.model.S3Object) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) Test(org.junit.Test)

Example 19 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project aws-java-reference-pocs by hardikSinghBehl.

the class StorageService method save.

/**
 * @param file: represents an object to be saved in configured S3 Bucket
 * @return HttpStatus 200 OK if file was saved, HttpStatus 417
 *         EXPECTATION_FAILED if file was not saved.
 */
public HttpStatus save(final MultipartFile file) {
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(file.getSize());
    metadata.setContentType(file.getContentType());
    metadata.setContentDisposition(file.getOriginalFilename());
    try {
        amazonS3.putObject(awsProperties.getS3().getBucketName(), file.getOriginalFilename(), file.getInputStream(), metadata);
    } catch (SdkClientException | IOException e) {
        log.error("UNABLE TO STORE {} IN S3: {} ", file.getOriginalFilename(), LocalDateTime.now());
        return HttpStatus.EXPECTATION_FAILED;
    }
    return HttpStatus.OK;
}
Also used : SdkClientException(com.amazonaws.SdkClientException) IOException(java.io.IOException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 20 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project aws-java-reference-pocs by hardikSinghBehl.

the class StorageService method save.

/**
 * @param file: represents an object to be saved in configured S3 Bucket
 * @return HttpStatus 200 OK if file was saved, HttpStatus 417
 *         EXPECTATION_FAILED if file was not saved.
 */
public HttpStatus save(final MultipartFile file) {
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(file.getSize());
    metadata.setContentType(file.getContentType());
    metadata.setContentDisposition(file.getOriginalFilename());
    try {
        amazonS3.putObject(awsS3ConfigurationProperties.getS3().getInputBucketName(), file.getOriginalFilename(), file.getInputStream(), metadata);
    } catch (SdkClientException | IOException e) {
        log.error("UNABLE TO STORE {} IN S3: {} ", file.getOriginalFilename(), LocalDateTime.now());
        return HttpStatus.EXPECTATION_FAILED;
    }
    return HttpStatus.OK;
}
Also used : SdkClientException(com.amazonaws.SdkClientException) IOException(java.io.IOException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Aggregations

ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)566 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)191 ByteArrayInputStream (java.io.ByteArrayInputStream)157 Test (org.junit.Test)143 IOException (java.io.IOException)101 InputStream (java.io.InputStream)80 File (java.io.File)62 AmazonClientException (com.amazonaws.AmazonClientException)61 AmazonServiceException (com.amazonaws.AmazonServiceException)61 S3Object (com.amazonaws.services.s3.model.S3Object)59 AmazonS3 (com.amazonaws.services.s3.AmazonS3)54 Date (java.util.Date)46 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)34 GetObjectMetadataRequest (com.amazonaws.services.s3.model.GetObjectMetadataRequest)33 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)32 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)30 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)29 Upload (com.amazonaws.services.s3.transfer.Upload)26 SdkClientException (com.amazonaws.SdkClientException)24 CopyObjectRequest (com.amazonaws.services.s3.model.CopyObjectRequest)24