com.amazonaws.services.s3.model.GetObjectRequest Java Examples

The following examples show how to use com.amazonaws.services.s3.model.GetObjectRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AWSErrorManager.java    From pacbot with Apache License 2.0 8 votes vote down vote up
/**
 * Fetch error info.
 *
 * @param datasource the datasource
 * @param errorList the error list
 */
private void fetchErrorInfo(String datasource, List<Map<String,String>> errorList){
	if(errorInfo==null){
		ObjectMapper objectMapper = new ObjectMapper();
    	List<Map<String, String>> inventoryErrors = new ArrayList<>();
    	AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(new CredentialProvider().getCredentials(s3Account,s3Role))).withRegion(s3Region).build();
    	try {
	    	S3Object inventoryErrorData = s3Client.getObject(new GetObjectRequest(bucketName,dataPath+"/"+datasource+"-loaderror.data"));
	    	try (BufferedReader reader = new BufferedReader(new InputStreamReader(inventoryErrorData.getObjectContent()))) {
				inventoryErrors = objectMapper.readValue(reader.lines().collect(Collectors.joining("\n")),new TypeReference<List<Map<String, String>>>() {});
	        }
    	} catch (IOException e) {
    		LOGGER.error("Exception in collecting inventory error data",e);
            Map<String,String> errorMap = new HashMap<>();
            errorMap.put(ERROR, "Exception in collecting inventory error data");
            errorMap.put(ERROR_TYPE, WARN);
            errorMap.put(EXCEPTION, e.getMessage());
            errorList.add(errorMap);
		}
    	errorInfo = inventoryErrors.parallelStream().collect(Collectors.groupingBy(obj -> obj.get("type")));
	}
}
 
Example #2
Source File: InventoryReportRetriever.java    From s3-inventory-usage-examples with Apache License 2.0 6 votes vote down vote up
/**
 * Get the original inventory report from S3, unzip it, and transfer it into a String format.
 * @return inventReport String
 * @throws IOException when getting object from S3 fails
 * or the checksum of the inventory report and the checksum specified in the manifest file not match
 */
public String getInventoryReportToString() throws IOException {
    String inventReportKey = locator.getKey();
    String bucketName = inventoryManifest.getSourceBucket();

    try (S3Object s3InventoryReport = s3Client.getObject(
            new GetObjectRequest(bucketName, inventReportKey))) {
        InputStream objectData = s3InventoryReport.getObjectContent();
        byte[] zippedData = IOUtils.toByteArray(objectData);
        String actualChecksum = DigestUtils.md5Hex(zippedData);
        String expectedChecksum = locator.getMD5checksum();
        if (!actualChecksum.equals(expectedChecksum)) {
            throw new ChecksumMismatchException (expectedChecksum, actualChecksum);
        }
        return IOUtils.toString(new GZIPInputStream(new ByteArrayInputStream(zippedData)));
    }
}
 
Example #3
Source File: S3FileTransferImplTest.java    From genie with Apache License 2.0 6 votes vote down vote up
/**
 * Test the getFile method for valid s3 path.
 *
 * @throws GenieException If there is any problem
 */
@Test
public void testGetFileMethodValidS3Path() throws GenieException {
    final ObjectMetadata objectMetadata = Mockito.mock(ObjectMetadata.class);
    Mockito.when(this.s3Client.getObject(Mockito.any(GetObjectRequest.class), Mockito.any(File.class)))
        .thenReturn(objectMetadata);
    final ArgumentCaptor<GetObjectRequest> argument = ArgumentCaptor.forClass(GetObjectRequest.class);

    s3FileTransfer.getFile(S3_PATH, LOCAL_PATH);
    Mockito.verify(this.s3Client).getObject(argument.capture(), Mockito.any());
    Assert.assertEquals(S3_BUCKET, argument.getValue().getBucketName());
    Assert.assertEquals(S3_KEY, argument.getValue().getKey());
    Mockito
        .verify(this.downloadTimer, Mockito.times(1))
        .record(Mockito.anyLong(), Mockito.eq(TimeUnit.NANOSECONDS));
    Mockito
        .verify(this.registry, Mockito.times(1))
        .timer(Mockito.eq(S3FileTransferImpl.DOWNLOAD_TIMER_NAME), this.tagsCaptor.capture());
    Assert.assertEquals(SUCCESS_TAGS, this.tagsCaptor.getValue());

}
 
Example #4
Source File: AwsUploadRepository.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream downloadFile(String filePath) throws BusinessException {
    if (!Optional.ofNullable(filePath).isPresent()) {
        throw new BusinessException(Validations.INVALID_PATH.getCode());
    }
    client.getClient(credentials.getCredentials());
    try {
        if (filePath.contains("/")) {
            filePath = filePath.split("/")[filePath.split("/").length - 1];
        }

        S3Object object = s3Client.getObject(
                new GetObjectRequest(cdnConfig.getName(), filePath));
        return object.getObjectContent();
    } catch (AmazonServiceException e) {
        throw new BusinessException(Validations.INVALID_S3_BUCKET_CREDENTIALS.getCode());
    }
}
 
Example #5
Source File: AwsS3EnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
private void setupS3(String fileName, String version, String propertyContent)
		throws UnsupportedEncodingException {
	final S3ObjectId s3ObjectId = new S3ObjectId("bucket1", fileName);
	final GetObjectRequest request = new GetObjectRequest(s3ObjectId);

	final S3Object s3Object = new S3Object();
	s3Object.setObjectContent(new StringInputStream(propertyContent));

	if (version != null) {
		final ObjectMetadata metadata = new ObjectMetadata();
		metadata.setHeader("x-amz-version-id", version);
		s3Object.setObjectMetadata(metadata);
	}

	when(s3Client.getObject(argThat(new GetObjectRequestMatcher(request))))
			.thenReturn(s3Object);
}
 
Example #6
Source File: S3BlockReader.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
/**
 * S3 block read would be achieved through the AmazonS3 client. Following are the steps to achieve:
 * (1) Create the objectRequest from bucketName and filePath.
 * (2) Set the range to the above created objectRequest.
 * (3) Get the object portion through AmazonS3 client API.
 * (4) Get the object content from the above object portion.
 * @return the block entity
 * @throws IOException
 */
@Override
protected Entity readEntity() throws IOException
{
  entity.clear();
  GetObjectRequest rangeObjectRequest = new GetObjectRequest(
      bucketName, filePath);
  rangeObjectRequest.setRange(offset, blockMetadata.getLength() - 1);
  S3Object objectPortion = s3Client.getObject(rangeObjectRequest);
  S3ObjectInputStream wrappedStream = objectPortion.getObjectContent();
  byte[] record = ByteStreams.toByteArray(wrappedStream);
  entity.setUsedBytes(record.length);
  entity.setRecord(record);
  wrappedStream.close();
  return entity;
}
 
Example #7
Source File: S3FeaturesDemoTest.java    From Scribengin with GNU Affero General Public License v3.0 6 votes vote down vote up
public S3Object download(String bucketName, String key) throws AmazonServiceException {
  /*
   * Download an object - When you download an object, you get all of the
   * object's metadata and a stream from which to read the contents. It's
   * important to read the contents of the stream as quickly as possibly
   * since the data is streamed directly from Amazon S3 and your network
   * connection will remain open until you read all the data or close the
   * input stream.
   * 
   * GetObjectRequest also supports several other options, including
   * conditional downloading of objects based on modification times, ETags,
   * and selectively downloading a range of an object.
   */
  System.out.println("Downloading an object");
  S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, key));
  System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
  return object;
}
 
Example #8
Source File: Loader.java    From AWS-MIMIC-IIItoOMOP with Apache License 2.0 6 votes vote down vote up
public void load(Configuration configuration)
{
    String destination = "";
    
    setConfiguration(configuration);
    
    try {
        for(Mapping mapping : configuration.getMappings())
        {
            destination = mapping.getSqlFile().contains("/") ? mapping.getSqlFile().substring(mapping.getSqlFile().lastIndexOf("/") + 1, mapping.getSqlFile().length()).replace(".sql", "") : mapping.getSqlFile().replace(".sql", "");
            
            loadSourceFiles(mapping);
            
            df = loadDf(client.getObject(new GetObjectRequest(configuration.getSqlBucket(), mapping.getSqlFile())).getObjectContent());
            
            if(mapping.getOverflowColumns().equals(new ArrayList<String>())) { write(destination); }
            else { write(destination, mapping.getOverflowColumns()); }
            
            sns.publishSuccess("SparkBatchLoaderTableCompletion", destination);
        }
    } 
    catch (IOException ex) { sns.publishFailure("SparkBatchLoaderTableFailure", destination, ex.toString()); }
}
 
Example #9
Source File: DynamoDbS3Operations.java    From ReCiter with Apache License 2.0 6 votes vote down vote up
/**
 * This function retrieves large object from S3
 * @param bucketName
 * @param keyName
 * @param objectClass
 * @return
 */
public <T> Object retrieveLargeItem(String bucketName, String keyName, Class<T> objectClass) {
	try {
		S3Object s3Object = s3.getObject(new GetObjectRequest(bucketName.toLowerCase(), keyName));
		String objectContent = IOUtils.toString(s3Object.getObjectContent(), StandardCharsets.UTF_8);
		if(objectClass == ReCiterFeature.class) {
			ReCiterFeature reCiterFeature = OBJECT_MAPPER.readValue(objectContent, ReCiterFeature.class);
			return reCiterFeature;
		}
		
	} catch (IOException | AmazonServiceException e) {
		log.error(e.getMessage());
	}
	return null;
	
}
 
Example #10
Source File: AmazonS3Util.java    From datacollector with Apache License 2.0 6 votes vote down vote up
static S3Object getObject(
    AmazonS3 s3Client,
    String bucket,
    String objectKey,
    boolean useSSE,
    CredentialValue customerKey,
    CredentialValue customerKeyMd5
) throws StageException {
  GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, objectKey);
  if (useSSE) {
    SSECustomerKey sseCustomerKey = new SSECustomerKey(customerKey.get());
    sseCustomerKey.setMd5(customerKeyMd5.get());
    getObjectRequest.setSSECustomerKey(sseCustomerKey);
  }
  return s3Client.getObject(getObjectRequest);
}
 
Example #11
Source File: InventoryReportRetrieverTest.java    From s3-inventory-usage-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void getInventReportSuccess() throws Exception {
    testLocator.setMD5checksum(testMD5);
    testManifest.setFileSchema("storageClass, size");
    reportRetriever = new InventoryReportRetriever(mockS3Client, testLocator, testManifest);

    String expectedInventoryReportString = "testString";
    byte[] expectedInventoryReportBytes = inventReportBytes(expectedInventoryReportString);
    when(mockS3Object.getObjectContent()).thenReturn(new S3ObjectInputStream(
            new ByteArrayInputStream(expectedInventoryReportBytes), null));
    when(mockS3Client.getObject(getObjectRequestCaptor.capture())).thenReturn(mockS3Object);

    String result = reportRetriever.getInventoryReportToString();
    assertThat(result, is(expectedInventoryReportString));

    GetObjectRequest request = getObjectRequestCaptor.getValue();
    assertThat(request.getBucketName(), is("testBucket"));
    assertThat(request.getKey(), is("testInventReportKey"));
}
 
Example #12
Source File: InventoryManifestRetriever.java    From s3-inventory-usage-examples with Apache License 2.0 6 votes vote down vote up
/**
 * Check if the MD5s of manifest.json and manifest.checksum equal
 * if so, pull out the manifest file and map it into a POJO
 * @return inventoryManifestStorage InventoryManifest, which stores all the elements of the manifest.json file
 */
public InventoryManifest getInventoryManifest() throws Exception {
    // Get manifest.json and transfer it to String
    GetObjectRequest requestJson = new GetObjectRequest(bucketName, bucketKeyJson);
    S3Object jsonObject = s3Client.getObject(requestJson);
    String jsonFile = inputStreamToString(jsonObject.getObjectContent());
    jsonObject.close();

    // Get manifest.checksum and transfer it to String with no whitespace
    GetObjectRequest requestChecksum = new GetObjectRequest(bucketName, bucketKeyChecksum);
    S3Object checksumObject = s3Client.getObject(requestChecksum);
    String expectedChecksum = inputStreamToString(checksumObject.getObjectContent())
            .replaceAll("\\s","");
    checksumObject.close();

    // Compare manifest.json and manifest.checksum's MD5 value
    String actualChecksum = DigestUtils.md5Hex(jsonFile);
    if (!actualChecksum.equals(expectedChecksum)) {
        throw new ChecksumMismatchException (expectedChecksum, actualChecksum);
    }

    return mapper.readValue(jsonFile, InventoryManifest.class);
}
 
Example #13
Source File: UploadFileSetToS3TaskTests.java    From aws-ant-tasks with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecuteSingleFile() throws FileNotFoundException,
        IOException {
    UploadFileSetToS3Task task = new UploadFileSetToS3Task();
    task.setProject(new Project());
    FileSet fileset = new FileSet();
    fileset.setDir(testFile1.getParentFile());
    fileset.setFile(testFile1);
    task.addFileset(fileset);
    task.setBucketName(BUCKET_NAME);
    task.setKeyPrefix(KEY_PREFIX);
    task.execute();
    resFile1 = File.createTempFile(RES_FILE_1, TESTFILE_SUFFIX);
    client.getObject(new GetObjectRequest(BUCKET_NAME, KEY_PREFIX
            + fileName1), resFile1);
    assertTrue(FileUtils.contentEquals(testFile1, resFile1));
}
 
Example #14
Source File: S3FileTransferImplTest.java    From genie with Apache License 2.0 6 votes vote down vote up
/**
 * Test the getFile method for valid s3 path.
 *
 * @throws GenieException If there is any problem
 */
@Test(expected = GenieServerException.class)
public void testGetFileMethodFailureToFetch() throws GenieException {
    Mockito.when(this.s3Client.getObject(Mockito.any(GetObjectRequest.class), Mockito.any(File.class)))
        .thenThrow(new AmazonS3Exception("something"));
    final ArgumentCaptor<GetObjectRequest> argument = ArgumentCaptor.forClass(GetObjectRequest.class);

    try {
        this.s3FileTransfer.getFile(S3_PATH, LOCAL_PATH);
    } finally {
        Mockito.verify(this.s3Client).getObject(argument.capture(), Mockito.any());
        Assert.assertEquals(S3_BUCKET, argument.getValue().getBucketName());
        Assert.assertEquals(S3_KEY, argument.getValue().getKey());
        Mockito
            .verify(this.downloadTimer, Mockito.times(1))
            .record(Mockito.anyLong(), Mockito.eq(TimeUnit.NANOSECONDS));
        Mockito
            .verify(this.registry, Mockito.times(1))
            .timer(Mockito.eq(S3FileTransferImpl.DOWNLOAD_TIMER_NAME), this.tagsCaptor.capture());
        Assert.assertEquals(
            MetricsUtils.newFailureTagsSetForException(new GenieServerException("blah")),
            this.tagsCaptor.getValue()
        );
    }
}
 
Example #15
Source File: S3FileManagerImpl.java    From entrada with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Optional<InputStream> open(String location) {
  log.debug("Open S3 file: " + location);
  Optional<S3Details> details = S3Details.from(location);

  if (details.isPresent()) {
    try {
      return Optional
          .of(amazonS3
              .getObject(new GetObjectRequest(details.get().getBucket(), details.get().getKey()))
              .getObjectContent());
    } catch (Exception e) {
      log.error("Cannot open {}", location, e);
    }

  }

  return Optional.empty();
}
 
Example #16
Source File: ErrorManager.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch error info.
 *
 * @param datasource the datasource
 * @param errorList the error list
 */
private void fetchErrorInfo(List<Map<String,String>> errorList){
	if(errorInfo==null){
		ObjectMapper objectMapper = new ObjectMapper();
    	List<Map<String, String>> inventoryErrors = new ArrayList<>();
    	AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(new CredentialProvider().getCredentials(s3Account,s3Role))).withRegion(s3Region).build();
    	try {
	    	S3Object inventoryErrorData = s3Client.getObject(new GetObjectRequest(bucketName,dataPath+"/"+dataSource+"-loaderror.data"));
	    	try (BufferedReader reader = new BufferedReader(new InputStreamReader(inventoryErrorData.getObjectContent()))) {
				inventoryErrors = objectMapper.readValue(reader.lines().collect(Collectors.joining("\n")),new TypeReference<List<Map<String, String>>>() {});
	        }
    	} catch (IOException e) {
    		LOGGER.error("Exception in collecting inventory error data",e);
            Map<String,String> errorMap = new HashMap<>();
            errorMap.put(ERROR, "Exception in collecting inventory error data");
            errorMap.put(ERROR_TYPE, WARN);
            errorMap.put(EXCEPTION, e.getMessage());
            errorList.add(errorMap);
		}
    	errorInfo = inventoryErrors.parallelStream().collect(Collectors.groupingBy(obj -> obj.get("type")));
	}
}
 
Example #17
Source File: S3Restorer.java    From cassandra-backup with Apache License 2.0 6 votes vote down vote up
@Override
public void downloadFile(final Path localPath, final RemoteObjectReference objectReference) throws Exception {
    final GetObjectRequest getObjectRequest = new GetObjectRequest(request.storageLocation.bucket, objectReference.canonicalPath);

    Files.createDirectories(localPath.getParent());

    final Optional<AmazonClientException> exception = ofNullable(transferManager.download(getObjectRequest,
                                                                                          localPath.toFile(),
                                                                                          new DownloadProgressListener(objectReference)).waitForException());

    if (exception.isPresent()) {
        if (exception.get() instanceof AmazonS3Exception && ((AmazonS3Exception) exception.get()).getStatusCode() == 404) {
            logger.error("Remote object reference {} does not exist.", objectReference);
        }

        throw exception.get();
    }
}
 
Example #18
Source File: S3URLConnection.java    From render with GNU General Public License v2.0 6 votes vote down vote up
private synchronized void connectToS3() throws IOException {
    if (! connected) {
        try {
            String s3key;
            try {
                s3key = java.net.URLDecoder.decode(s3uri.getKey(), "UTF-8");
            } catch (final UnsupportedEncodingException e) {
                LOG.warn("failed to decode key, using raw key instead", e);
                // TODO: Better error handling with badly encoded URLs?
                s3key = s3uri.getKey();
            }
            s3object = s3Client.getObject(new GetObjectRequest(s3uri.getBucket(), s3key));
            connected = true;
        } catch (final AmazonServiceException ase) {
            throw new IOException("Amazon S3 service failure for error type " + ase.getErrorType(), ase);
        } catch (final AmazonClientException ace) {
            throw new IOException("Amazon S3 client failure", ace);
        }
    }
}
 
Example #19
Source File: TestS3DataTransferer.java    From oodt with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetrieveProduct() throws DataTransferException, IOException {
	dataTransferer.retrieveProduct(product, stagingDir);

	ArgumentCaptor<GetObjectRequest> argument = ArgumentCaptor.forClass(GetObjectRequest.class);
	verify(s3Client).getObject(argument.capture());

	GetObjectRequest request = argument.getValue();
	assertThat(request.getBucketName(), is(S3_BUCKET_NAME));
	assertThat(request.getKey(), is(EXPECTED_DATA_STORE_REF));
}
 
Example #20
Source File: MockS3OperationsImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
@Override
public S3Object getS3Object(GetObjectRequest getObjectRequest, AmazonS3 s3)
{
    MockS3Object mockS3Object = getMockS3Object(getObjectRequest.getBucketName(), getObjectRequest.getKey());

    S3Object s3Object = new S3Object();
    s3Object.setBucketName(getObjectRequest.getBucketName());
    s3Object.setKey(getObjectRequest.getKey());
    s3Object.setObjectContent(new ByteArrayInputStream(mockS3Object.getData()));
    s3Object.setObjectMetadata(mockS3Object.getObjectMetadata());

    return s3Object;
}
 
Example #21
Source File: S3FileTransferImpl.java    From genie with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void getFile(
    @NotBlank(message = "Source file path cannot be empty.") final String srcRemotePath,
    @NotBlank(message = "Destination local path cannot be empty") final String dstLocalPath
) throws GenieException {
    final long start = System.nanoTime();
    final Set<Tag> tags = Sets.newHashSet();
    try {
        log.debug("Called with src path {} and destination path {}", srcRemotePath, dstLocalPath);

        final AmazonS3URI s3Uri = getS3Uri(srcRemotePath);
        try {
            this.s3ClientFactory
                .getClient(s3Uri)
                .getObject(
                    new GetObjectRequest(s3Uri.getBucket(), s3Uri.getKey()),
                    new File(dstLocalPath)
                );
        } catch (final AmazonS3Exception ase) {
            log.error("Error fetching file {} from s3 due to exception {}", srcRemotePath, ase.toString());
            throw new GenieServerException("Error downloading file from s3. Filename: " + srcRemotePath, ase);
        }
        MetricsUtils.addSuccessTags(tags);
    } catch (Throwable t) {
        MetricsUtils.addFailureTagsWithException(tags, t);
        throw t;
    } finally {
        this.registry.timer(DOWNLOAD_TIMER_NAME, tags).record(System.nanoTime() - start, TimeUnit.NANOSECONDS);
    }
}
 
Example #22
Source File: SimpleStorageResource.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream getInputStream() throws IOException {
	GetObjectRequest getObjectRequest = new GetObjectRequest(this.bucketName,
			this.objectName);
	if (this.versionId != null) {
		getObjectRequest.setVersionId(this.versionId);
	}
	return this.amazonS3.getObject(getObjectRequest).getObjectContent();
}
 
Example #23
Source File: S3DataTransferer.java    From oodt with Apache License 2.0 5 votes vote down vote up
@Override
public void retrieveProduct(Product product, File directory) throws DataTransferException,
    IOException {
	for (Reference ref : product.getProductReferences()) {
     GetObjectRequest request = new GetObjectRequest(bucketName, stripProtocol(
         ref.getDataStoreReference(), true));
		S3Object file = s3Client.getObject(request);
		stageFile(file, ref, directory);
	}
}
 
Example #24
Source File: AwsSdkTest.java    From s3proxy with Apache License 2.0 5 votes vote down vote up
@Test
public void testAwsV2SignatureWithOverrideParameters() throws Exception {
    client = AmazonS3ClientBuilder.standard()
            .withClientConfiguration(V2_SIGNER_CONFIG)
            .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
            .withEndpointConfiguration(s3EndpointConfig).build();

    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(BYTE_SOURCE.size());
    client.putObject(containerName, "foo", BYTE_SOURCE.openStream(),
            metadata);

    String blobName = "foo";

    ResponseHeaderOverrides headerOverride = new ResponseHeaderOverrides();

    String expectedContentDisposition = "attachment; " + blobName;
    headerOverride.setContentDisposition(expectedContentDisposition);

    String expectedContentType = "text/plain";
    headerOverride.setContentType(expectedContentType);

    GetObjectRequest request = new GetObjectRequest(containerName,
            blobName);
    request.setResponseHeaders(headerOverride);

    S3Object object = client.getObject(request);
    assertThat(object.getObjectMetadata().getContentLength()).isEqualTo(
            BYTE_SOURCE.size());
    assertThat(object.getObjectMetadata().getContentDisposition())
            .isEqualTo(expectedContentDisposition);
    assertThat(object.getObjectMetadata().getContentType()).isEqualTo(
            expectedContentType);
    try (InputStream actual = object.getObjectContent();
         InputStream expected = BYTE_SOURCE.openStream()) {
        assertThat(actual).hasContentEqualTo(expected);
    }
}
 
Example #25
Source File: S3DaoImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves an S3 object.
 *
 * @param s3Client the S3 client
 * @param bucketName the S3 bucket name
 * @param key the S3 object key
 * @param errorOnNoSuchKey true to throw an error when the object key is not found, otherwise return null
 *
 * @return the S3 object
 * @throws ObjectNotFoundException when specified bucket or key does not exist or access to bucket or key is denied
 */
private S3Object getS3Object(AmazonS3Client s3Client, String bucketName, String key, boolean errorOnNoSuchKey)
{
    try
    {
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        return s3Operations.getS3Object(getObjectRequest, s3Client);
    }
    catch (AmazonServiceException amazonServiceException)
    {
        String errorCode = amazonServiceException.getErrorCode();

        if (S3Operations.ERROR_CODE_ACCESS_DENIED.equals(errorCode))
        {
            throw new ObjectNotFoundException(
                "Application does not have access to the specified S3 object at bucket '" + bucketName + "' and key '" + key + "'.",
                amazonServiceException);
        }
        else if (S3Operations.ERROR_CODE_NO_SUCH_BUCKET.equals(errorCode))
        {
            throw new ObjectNotFoundException("Specified S3 bucket '" + bucketName + "' does not exist.", amazonServiceException);
        }
        else if (S3Operations.ERROR_CODE_NO_SUCH_KEY.equals(errorCode))
        {
            if (errorOnNoSuchKey)
            {
                throw new ObjectNotFoundException("Specified S3 object key '" + key + "' does not exist.", amazonServiceException);
            }
            else
            {
                return null;
            }
        }
        else
        {
            throw amazonServiceException;
        }
    }
}
 
Example #26
Source File: S3RemoteFileSystem.java    From imhotep with Apache License 2.0 5 votes vote down vote up
@Override
public void copyFileInto(String fullPath, File localFile) throws IOException {
    final String relativePath = mounter.getMountRelativePath(fullPath, mountPoint);
    final String s3path = getS3path(relativePath);
    final ObjectMetadata metadata;
    
    try {
        metadata = client.getObject(new GetObjectRequest(s3bucket, s3path), localFile);
    } catch(AmazonS3Exception e) {
        throw new IOException(e);
    }
}
 
Example #27
Source File: S3ChangeLogStoreTest.java    From athenz with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetSignedDomainServiceException() {
    MockS3ChangeLogStore store = new MockS3ChangeLogStore(null);
    
    when(store.awsS3Client.getObject(any(GetObjectRequest.class))).thenThrow(new AmazonServiceException("failed server operation"));
    assertNull(store.getSignedDomain(store.awsS3Client, "iaas"));
}
 
Example #28
Source File: S3TableConfigClient.java    From presto with Apache License 2.0 5 votes vote down vote up
/**
 * Connect to S3 directory to look for new or updated table definitions and then
 * update the map.
 */
private void updateTablesFromS3()
{
    long now = System.currentTimeMillis();

    AmazonS3Client s3client = clientManager.getS3Client();

    for (S3ObjectSummary summary : getObjectSummaries()) {
        if (!descriptors.containsKey(summary.getKey()) || summary.getLastModified().getTime() >= lastCheck) {
            // New or updated file, so we must read from AWS
            if (summary.getKey().endsWith("/")) {
                continue;
            }

            log.info("Getting : %s - %s", summary.getBucketName(), summary.getKey());
            S3Object object = s3client.getObject(new GetObjectRequest(summary.getBucketName(), summary.getKey()));

            try (BufferedReader reader = new BufferedReader(new InputStreamReader(object.getObjectContent(), UTF_8))) {
                KinesisStreamDescription table = streamDescriptionCodec.fromJson(CharStreams.toString(reader));
                descriptors.put(summary.getKey(), table);
                log.info("Put table description into the map from %s", summary.getKey());
            }
            catch (IOException iox) {
                log.error("Problem reading input stream from object.", iox);
                throwIfUnchecked(iox);
                throw new RuntimeException(iox);
            }
        }
    }

    log.info("Completed updating table definitions from S3.");
    lastCheck = now;
}
 
Example #29
Source File: S3ChangeLogStoreTest.java    From athenz with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetSignedDomainNotFound() {
    MockS3ChangeLogStore store = new MockS3ChangeLogStore(null);
    when(store.awsS3Client.getObject(any(GetObjectRequest.class))).thenReturn(null);
    
    assertNull(store.getSignedDomain(store.awsS3Client, "iaas"));
}
 
Example #30
Source File: S3ChangeLogStoreTest.java    From athenz with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetSignedDomainClientException() {
    MockS3ChangeLogStore store = new MockS3ChangeLogStore(null);
    
    when(store.awsS3Client.getObject(any(GetObjectRequest.class))).thenThrow(new AmazonClientException("failed client operation"));
    assertNull(store.getSignedDomain(store.awsS3Client, "iaas"));
}