Java Code Examples for com.amazonaws.services.s3.model.AmazonS3Exception#setStatusCode()

The following examples show how to use com.amazonaws.services.s3.model.AmazonS3Exception#setStatusCode() . 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: S3FileSystemTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void matchNonGlobNotFound() {
  S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Options());

  S3ResourceId path = S3ResourceId.fromUri("s3://testbucket/testdirectory/nonexistentfile");
  AmazonS3Exception exception = new AmazonS3Exception("mock exception");
  exception.setStatusCode(404);
  when(s3FileSystem
          .getAmazonS3Client()
          .getObjectMetadata(
              argThat(
                  new GetObjectMetadataRequestMatcher(
                      new GetObjectMetadataRequest(path.getBucket(), path.getKey())))))
      .thenThrow(exception);

  MatchResult result = s3FileSystem.matchNonGlobPath(path);
  assertThat(
      result,
      MatchResultMatcher.create(MatchResult.Status.NOT_FOUND, new FileNotFoundException()));
}
 
Example 2
Source File: S3FileSystemTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void matchNonGlobForbidden() {
  S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Options());

  AmazonS3Exception exception = new AmazonS3Exception("mock exception");
  exception.setStatusCode(403);
  S3ResourceId path = S3ResourceId.fromUri("s3://testbucket/testdirectory/keyname");
  when(s3FileSystem
          .getAmazonS3Client()
          .getObjectMetadata(
              argThat(
                  new GetObjectMetadataRequestMatcher(
                      new GetObjectMetadataRequest(path.getBucket(), path.getKey())))))
      .thenThrow(exception);

  assertThat(
      s3FileSystem.matchNonGlobPath(path),
      MatchResultMatcher.create(MatchResult.Status.ERROR, new IOException(exception)));
}
 
Example 3
Source File: MockAmazonS3.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public S3Object getObject(final GetObjectRequest request) throws AmazonClientException {
    assertThat(request.getBucketName(), equalTo(bucket));

    final String blobName = request.getKey();
    final byte[] content = blobs.get(blobName);
    if (content == null) {
        AmazonS3Exception exception = new AmazonS3Exception("[" + blobName + "] does not exist.");
        exception.setStatusCode(404);
        throw exception;
    }

    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(content.length);

    S3Object s3Object = new S3Object();
    s3Object.setObjectContent(new S3ObjectInputStream(new ByteArrayInputStream(content), null, false));
    s3Object.setKey(blobName);
    s3Object.setObjectMetadata(metadata);

    return s3Object;
}
 
Example 4
Source File: MockAmazonS3.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectMetadata getObjectMetadata(GetObjectMetadataRequest getObjectMetadataRequest)
{
    this.getObjectMetadataRequest = getObjectMetadataRequest;
    if (getObjectMetadataHttpCode != HTTP_OK) {
        AmazonS3Exception exception = new AmazonS3Exception("Failing getObjectMetadata call with " + getObjectMetadataHttpCode);
        exception.setStatusCode(getObjectMetadataHttpCode);
        throw exception;
    }
    return null;
}
 
Example 5
Source File: MockAmazonS3.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public S3Object getObject(GetObjectRequest getObjectRequest)
{
    if (getObjectHttpCode != HTTP_OK) {
        AmazonS3Exception exception = new AmazonS3Exception("Failing getObject call with " + getObjectHttpCode);
        exception.setStatusCode(getObjectHttpCode);
        throw exception;
    }
    return null;
}
 
Example 6
Source File: ScholarBucketPaperSource.java    From science-parse with Apache License 2.0 5 votes vote down vote up
private S3Object getS3Object(final String paperId) {
    final String key = paperId.substring(0, 4) + "/" + paperId.substring(4) + ".pdf";

    for(int bucketIndex = 0; bucketIndex < buckets.length; ++bucketIndex) {
        try {
            return s3.getObject(buckets[bucketIndex], key);
        } catch (final AmazonS3Exception e) {
            if(bucketIndex < buckets.length - 1 && e.getStatusCode() == 404)
                continue;   // Try again with the next bucket.

            final AmazonS3Exception rethrown =
                new AmazonS3Exception(
                    String.format(
                        "Error for key s3://%s/%s",
                        bucket,
                        key),
                    e);
            rethrown.setExtendedRequestId(e.getExtendedRequestId());
            rethrown.setErrorCode(e.getErrorCode());
            rethrown.setErrorType(e.getErrorType());
            rethrown.setRequestId(e.getRequestId());
            rethrown.setServiceName(e.getServiceName());
            rethrown.setStatusCode(e.getStatusCode());
            throw rethrown;
        }
    }

    throw new IllegalStateException("We should never get here.");
}
 
Example 7
Source File: AwsObjectStorageConnectorTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void getObjectStorageMetadataAccessDenied() {
    AmazonS3Exception exception = new AmazonS3Exception(ERROR_MESSAGE);
    exception.setStatusCode(403);
    when(s3Client.getBucketLocation(BUCKET_NAME)).thenThrow(exception);
    ObjectStorageMetadataRequest request = ObjectStorageMetadataRequest.builder().withObjectStoragePath(BUCKET_NAME).build();
    ObjectStorageMetadataResponse result = underTest.getObjectStorageMetadata(request);
    verify(s3Client).getBucketLocation(BUCKET_NAME);
    assertNull(result.getRegion());
    assertEquals(ResponseStatus.ACCESS_DENIED, result.getStatus());
}
 
Example 8
Source File: S3FileSystemTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Test
public void matchVariousInvokeThreadPool() throws IOException {
  S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Options());

  AmazonS3Exception notFoundException = new AmazonS3Exception("mock exception");
  notFoundException.setStatusCode(404);
  S3ResourceId pathNotExist =
      S3ResourceId.fromUri("s3://testbucket/testdirectory/nonexistentfile");
  when(s3FileSystem
          .getAmazonS3Client()
          .getObjectMetadata(
              argThat(
                  new GetObjectMetadataRequestMatcher(
                      new GetObjectMetadataRequest(
                          pathNotExist.getBucket(), pathNotExist.getKey())))))
      .thenThrow(notFoundException);

  AmazonS3Exception forbiddenException = new AmazonS3Exception("mock exception");
  forbiddenException.setStatusCode(403);
  S3ResourceId pathForbidden =
      S3ResourceId.fromUri("s3://testbucket/testdirectory/forbiddenfile");
  when(s3FileSystem
          .getAmazonS3Client()
          .getObjectMetadata(
              argThat(
                  new GetObjectMetadataRequestMatcher(
                      new GetObjectMetadataRequest(
                          pathForbidden.getBucket(), pathForbidden.getKey())))))
      .thenThrow(forbiddenException);

  S3ResourceId pathExist = S3ResourceId.fromUri("s3://testbucket/testdirectory/filethatexists");
  ObjectMetadata s3ObjectMetadata = new ObjectMetadata();
  s3ObjectMetadata.setContentLength(100);
  s3ObjectMetadata.setLastModified(new Date(1540000000000L));
  s3ObjectMetadata.setContentEncoding("not-gzip");
  when(s3FileSystem
          .getAmazonS3Client()
          .getObjectMetadata(
              argThat(
                  new GetObjectMetadataRequestMatcher(
                      new GetObjectMetadataRequest(pathExist.getBucket(), pathExist.getKey())))))
      .thenReturn(s3ObjectMetadata);

  S3ResourceId pathGlob = S3ResourceId.fromUri("s3://testbucket/path/part*");

  S3ObjectSummary foundListObject = new S3ObjectSummary();
  foundListObject.setBucketName(pathGlob.getBucket());
  foundListObject.setKey("path/part-0");
  foundListObject.setSize(200);
  foundListObject.setLastModified(new Date(1541000000000L));

  ListObjectsV2Result listObjectsResult = new ListObjectsV2Result();
  listObjectsResult.setNextContinuationToken(null);
  listObjectsResult.getObjectSummaries().add(foundListObject);
  when(s3FileSystem.getAmazonS3Client().listObjectsV2(notNull(ListObjectsV2Request.class)))
      .thenReturn(listObjectsResult);

  ObjectMetadata metadata = new ObjectMetadata();
  metadata.setContentEncoding("");
  when(s3FileSystem
          .getAmazonS3Client()
          .getObjectMetadata(
              argThat(
                  new GetObjectMetadataRequestMatcher(
                      new GetObjectMetadataRequest(pathGlob.getBucket(), "path/part-0")))))
      .thenReturn(metadata);

  assertThat(
      s3FileSystem.match(
          ImmutableList.of(
              pathNotExist.toString(),
              pathForbidden.toString(),
              pathExist.toString(),
              pathGlob.toString())),
      contains(
          MatchResultMatcher.create(MatchResult.Status.NOT_FOUND, new FileNotFoundException()),
          MatchResultMatcher.create(
              MatchResult.Status.ERROR, new IOException(forbiddenException)),
          MatchResultMatcher.create(100, 1540000000000L, pathExist, true),
          MatchResultMatcher.create(
              200,
              1541000000000L,
              S3ResourceId.fromComponents(pathGlob.getBucket(), foundListObject.getKey()),
              true)));
}
 
Example 9
Source File: S3DaoImplTest.java    From herd with Apache License 2.0 4 votes vote down vote up
private void testRestoreObjectsWithS3Exception(String exceptionMessage, int statusCode)
{
    List<File> files = Collections.singletonList(new File(TEST_FILE));

    // Create an S3 file transfer request parameters DTO to access S3 objects.
    S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
    s3FileTransferRequestParamsDto.setS3BucketName(S3_BUCKET_NAME);
    s3FileTransferRequestParamsDto.setS3KeyPrefix(S3_KEY_PREFIX);
    s3FileTransferRequestParamsDto.setFiles(files);

    // Create a retry policy.
    RetryPolicy retryPolicy =
        new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, INTEGER_VALUE, true);

    // Create an Object Metadata
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setOngoingRestore(false);
    objectMetadata.setHeader(Headers.STORAGE_CLASS, StorageClass.DeepArchive);

    ArgumentCaptor<AmazonS3Client> s3ClientCaptor = ArgumentCaptor.forClass(AmazonS3Client.class);
    ArgumentCaptor<String> s3BucketNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> keyCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<RestoreObjectRequest> requestStoreCaptor = ArgumentCaptor.forClass(RestoreObjectRequest.class);

    // Create an Amazon S3 Exception
    AmazonS3Exception amazonS3Exception = new AmazonS3Exception(exceptionMessage);
    amazonS3Exception.setStatusCode(statusCode);

    // Mock the external calls.
    when(retryPolicyFactory.getRetryPolicy()).thenReturn(retryPolicy);
    when(s3Operations.getObjectMetadata(s3BucketNameCaptor.capture(), keyCaptor.capture(), s3ClientCaptor.capture())).thenReturn(objectMetadata);
    doThrow(amazonS3Exception).when(s3Operations).restoreObject(requestStoreCaptor.capture(), s3ClientCaptor.capture());

    try
    {
        // Call the method under test.
        s3DaoImpl.restoreObjects(s3FileTransferRequestParamsDto, EXPIRATION_IN_DAYS, Tier.Standard.toString());

        // If this is not a restore already in progress exception message (409) then we should have caught an exception.
        // Else if this is a restore already in progress message (409) then continue as usual.
        if (!exceptionMessage.equals(RESTORE_ALREADY_IN_PROGRESS_EXCEPTION_MESSAGE))
        {
            // Should not be here. Fail!
            fail();
        }
        else
        {
            RestoreObjectRequest requestStore = requestStoreCaptor.getValue();
            assertEquals(S3_BUCKET_NAME, s3BucketNameCaptor.getValue());
            assertEquals(TEST_FILE, keyCaptor.getValue());

            // Verify Bulk option is used when the option is not provided
            assertEquals(StringUtils.isNotEmpty(Tier.Standard.toString())
                ? Tier.Standard.toString() : Tier.Bulk.toString(), requestStore.getGlacierJobParameters().getTier());
        }
    }
    catch (IllegalStateException illegalStateException)
    {
        assertEquals(String.format("Failed to initiate a restore request for \"%s\" key in \"%s\" bucket. " +
                "Reason: com.amazonaws.services.s3.model.AmazonS3Exception: %s " +
                "(Service: null; Status Code: %s; Error Code: null; Request ID: null; S3 Extended Request ID: null), S3 Extended Request ID: null",
            TEST_FILE, S3_BUCKET_NAME, exceptionMessage, statusCode), illegalStateException.getMessage());
    }

    // Verify the external calls
    verify(retryPolicyFactory).getRetryPolicy();
    verify(s3Operations).getObjectMetadata(anyString(), anyString(), any(AmazonS3Client.class));
    verify(s3Operations).restoreObject(any(RestoreObjectRequest.class), any(AmazonS3Client.class));
    verifyNoMoreInteractionsHelper();
}