Search in sources :

Example 21 with GetObjectRequest

use of com.amazonaws.s3.model.GetObjectRequest in project trellis-extensions by trellis-ldp.

the class S3MementoServiceTest method testResourceError.

@Test
void testResourceError() {
    final AmazonS3 mockClient = mock(AmazonS3.class);
    final ObjectMetadata mockMetadata = mock(ObjectMetadata.class);
    final GetObjectRequest mockRequest = mock(GetObjectRequest.class);
    final S3Object mockObject = mock(S3Object.class);
    when(mockClient.getObject(eq(mockRequest))).thenReturn(mockObject);
    when(mockObject.getObjectContent()).thenAnswer(inv -> {
        throw new IOException("Expected");
    });
    final Resource testResource = new S3Resource(mockMetadata, mockClient, mockRequest, "");
    assertThrows(TrellisRuntimeException.class, testResource::stream);
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) Resource(org.trellisldp.api.Resource) S3Object(com.amazonaws.services.s3.model.S3Object) IOException(java.io.IOException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) Test(org.junit.jupiter.api.Test)

Example 22 with GetObjectRequest

use of com.amazonaws.s3.model.GetObjectRequest in project nrtsearch by Yelp.

the class ContentDownloaderImpl method getVersionContent.

@Override
public void getVersionContent(final String serviceName, final String resource, final String hash, final Path destDirectory) throws IOException {
    final String absoluteResourcePath = String.format("%s/%s/%s", serviceName, resource, hash);
    final Path parentDirectory = destDirectory.getParent();
    final Path tmpFile = parentDirectory.resolve(getTmpName());
    final InputStream s3InputStream;
    if (downloadAsStream) {
        // Stream the file download from s3 instead of writing to a file first
        GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest(bucketName, absoluteResourcePath);
        ObjectMetadata fullMetadata = transferManager.getAmazonS3Client().getObjectMetadata(metadataRequest);
        logger.debug("Full object size: " + fullMetadata.getContentLength());
        // get metadata for the 1st file part, needed to find the total number of parts
        ObjectMetadata partMetadata = transferManager.getAmazonS3Client().getObjectMetadata(metadataRequest.withPartNumber(1));
        int numParts = partMetadata.getPartCount() != null ? partMetadata.getPartCount() : 1;
        logger.debug("Object parts: " + numParts);
        s3InputStream = getObjectStream(absoluteResourcePath, numParts);
        logger.debug("Object streaming started...");
    } else {
        Download download = transferManager.download(new GetObjectRequest(bucketName, absoluteResourcePath), tmpFile.toFile(), new ContentDownloaderImpl.S3ProgressListenerImpl(serviceName, resource, "download"));
        try {
            download.waitForCompletion();
            logger.debug("S3 Download complete");
        } catch (InterruptedException e) {
            throw new IOException("S3 Download failed", e);
        }
        s3InputStream = new FileInputStream(tmpFile.toFile());
    }
    wrapInputStream(destDirectory, parentDirectory, tmpFile, s3InputStream, hash);
}
Also used : Path(java.nio.file.Path) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) SequenceInputStream(java.io.SequenceInputStream) FileInputStream(java.io.FileInputStream) LZ4FrameInputStream(net.jpountz.lz4.LZ4FrameInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) GetObjectMetadataRequest(com.amazonaws.services.s3.model.GetObjectMetadataRequest) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Download(com.amazonaws.services.s3.transfer.Download) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest)

Example 23 with GetObjectRequest

use of com.amazonaws.s3.model.GetObjectRequest in project nrmn-application by aodn.

the class StagedJobController method downloadFile.

@GetMapping("/job/download/{jobId}")
public void downloadFile(final HttpServletResponse response, @PathVariable String jobId) {
    String s3Key = String.format(s3KeyTemplate, jobId);
    logger.info(LogInfo.withContext(String.format("Downloading original sheet for job %s from %s%s", jobId, bucketName, s3Key)));
    try {
        GetObjectRequest objectRequest = GetObjectRequest.builder().key(s3Key).bucket(bucketName).build();
        ResponseBytes<GetObjectResponse> objectBytes = s3client.getClient().getObjectAsBytes(objectRequest);
        response.getOutputStream().write(objectBytes.asByteArray());
        response.flushBuffer();
        logger.info(LogInfo.withContext(String.format("Retrieved sheet for job %s from %s/%s", jobId, bucketName, s3Key)));
    } catch (IOException ioException) {
        logger.error(LogInfo.withContext(String.format("Could not download sheet for job %s from \"%s/%s\".\n%s", jobId, bucketName, s3Key, ioException.getMessage())));
    } catch (NoSuchKeyException keyException) {
        logger.error(LogInfo.withContext(String.format("key \"%s\" does not exist in bucket \"%s\". Could not download original file for job %s", s3Key, bucketName, jobId)));
    } catch (SdkClientException clientException) {
        logger.error(LogInfo.withContext(String.format("Could not download original file for job %s. %s", jobId, clientException.getMessage())));
    }
}
Also used : NoSuchKeyException(software.amazon.awssdk.services.s3.model.NoSuchKeyException) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) IOException(java.io.IOException) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 24 with GetObjectRequest

use of com.amazonaws.s3.model.GetObjectRequest in project cerberus by Nike-Inc.

the class ConfigService method getCipherText.

private String getCipherText(String path) {
    final GetObjectRequest request = new GetObjectRequest(bucketName, path);
    try {
        S3Object s3Object = s3Client.getObject(request);
        InputStream object = s3Object.getObjectContent();
        return IOUtils.toString(object);
    } catch (AmazonServiceException ase) {
        if (StringUtils.equalsIgnoreCase(ase.getErrorCode(), "NoSuchKey")) {
            final String errorMessage = String.format("The S3 object doesn't exist. Bucket: %s, Key: %s", bucketName, request.getKey());
            logger.debug(errorMessage);
            throw new IllegalStateException(errorMessage);
        } else {
            logger.error("Unexpected error communicating with AWS.", ase);
            throw ase;
        }
    } catch (IOException e) {
        String errorMessage = String.format("Unable to read contents of S3 object. Bucket: %s, Key: %s, Expected Encoding: %s", bucketName, request.getKey(), Charset.defaultCharset());
        logger.error(errorMessage);
        throw new IllegalStateException(errorMessage, e);
    }
}
Also used : InputStream(java.io.InputStream) AmazonServiceException(com.amazonaws.AmazonServiceException) S3Object(com.amazonaws.services.s3.model.S3Object) IOException(java.io.IOException) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest)

Example 25 with GetObjectRequest

use of com.amazonaws.s3.model.GetObjectRequest in project baremaps by baremaps.

the class S3BlobStore method get.

/**
 * {@inheritDoc}
 */
@Override
public Blob get(URI uri) throws BlobStoreException {
    try {
        GetObjectRequest request = GetObjectRequest.builder().bucket(uri.getHost()).key(uri.getPath().substring(1)).build();
        ResponseInputStream<GetObjectResponse> responseInputStream = client.getObject(request);
        GetObjectResponse getObjectResponse = responseInputStream.response();
        return Blob.builder().withContentLength(getObjectResponse.contentLength()).withContentType(getObjectResponse.contentType()).withContentEncoding(getObjectResponse.contentEncoding()).withInputStream(responseInputStream).build();
    } catch (S3Exception e) {
        throw new BlobStoreException(e);
    }
}
Also used : GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) BlobStoreException(com.baremaps.blob.BlobStoreException)

Aggregations

GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)139 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)123 S3Object (com.amazonaws.services.s3.model.S3Object)80 Test (org.junit.Test)77 IOException (java.io.IOException)63 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)56 File (java.io.File)40 Instant (java.time.Instant)33 InputStream (java.io.InputStream)32 PresignedGetObjectRequest (software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest)31 URI (java.net.URI)30 Region (software.amazon.awssdk.regions.Region)30 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)29 Duration (java.time.Duration)26 ExecutionAttributes (software.amazon.awssdk.core.interceptor.ExecutionAttributes)25 S3Presigner (software.amazon.awssdk.services.s3.presigner.S3Presigner)25 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)23 Mockito (org.mockito.Mockito)23 RunWith (org.junit.runner.RunWith)22 AwsBasicCredentials (software.amazon.awssdk.auth.credentials.AwsBasicCredentials)22