software.amazon.awssdk.services.s3.model.NoSuchBucketException Java Examples

The following examples show how to use software.amazon.awssdk.services.s3.model.NoSuchBucketException. 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: AmazonMachineLearningIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
private static void setUpS3() {
    s3 = S3Client.builder()
                 .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
                 .region(Region.US_EAST_1)
                 .build();

    s3.createBucket(CreateBucketRequest.builder().bucket(BUCKET_NAME).build());

    Waiter.run(() -> s3.putObject(PutObjectRequest.builder()
                                                  .bucket(BUCKET_NAME)
                                                  .key(KEY)
                                                  .acl(ObjectCannedACL.PUBLIC_READ)
                                                  .build(),
                                  RequestBody.fromBytes(DATA.getBytes())))
          .ignoringException(NoSuchBucketException.class)
          .orFail();
}
 
Example #2
Source File: S3TogglzConfigurationTest.java    From edison-microservice with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldThrowExceptionOnMissingBucket() {
    //given
    when(mock.listObjects(any(ListObjectsRequest.class))).thenThrow(NoSuchBucketException.builder().build());

    this.context.register(TogglzAutoConfiguration.class);
    this.context.register(S3TestConfiguration.class);

    TestPropertyValues
            .of("edison.togglz.s3.enabled=true")
            .and("edison.togglz.s3.bucket-name=togglz-bucket")
            .and("edison.togglz.s3.check-bucket=true")
            .applyTo(context);

    //expect
    Assertions.assertThrows(BeanCreationException.class, this.context::refresh);
}
 
Example #3
Source File: ExceptionUnmarshallingIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void headBucketNoSuchBucket() {
    assertThatThrownBy(() -> s3.headBucket(b -> b.bucket(KEY)))
        .isInstanceOf(NoSuchBucketException.class)
        .satisfies(e -> assertMetadata((NoSuchBucketException) e, "NoSuchBucket"))
        .satisfies(e -> assertThat(((NoSuchBucketException) e).statusCode()).isEqualTo(404));
}
 
Example #4
Source File: ExceptionUnmarshallingIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void asyncHeadBucketNoSuchBucket() {
    assertThatThrownBy(() -> s3Async.headBucket(b -> b.bucket(KEY)).join())
        .hasCauseInstanceOf(NoSuchBucketException.class)
        .satisfies(e -> assertMetadata(((NoSuchBucketException) (e.getCause())), "NoSuchBucket"))
        .satisfies(e -> assertThat(((NoSuchBucketException) (e.getCause())).statusCode()).isEqualTo(404));
}
 
Example #5
Source File: ExceptionUnmarshallingIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore("TODO")
public void errorResponseContainsRawBytes() {
    assertThatThrownBy(() -> s3.getObjectAcl(b -> b.bucket(BUCKET + KEY).key(KEY)))
        .isInstanceOf(NoSuchBucketException.class)
        .satisfies(e -> assertThat(
            ((NoSuchBucketException) e).awsErrorDetails().rawResponse().asString(StandardCharsets.UTF_8))
            .startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>NoSuchBucket</Code><Message>The "
                        + "specified bucket does not exist</Message>"));
}
 
Example #6
Source File: S3TestUtils.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private static String getBucketWithPrefix(S3Client s3, String bucketPrefix) {
    String testBucket =
        s3.listBuckets()
          .buckets()
          .stream()
          .map(Bucket::name)
          .filter(name -> name.startsWith(bucketPrefix))
          .findAny()
          .orElse(null);

    if (testBucket == null) {
        String newTestBucket = bucketPrefix + UUID.randomUUID();
        s3.createBucket(r -> r.bucket(newTestBucket));
        Waiter.run(() -> s3.headBucket(r -> r.bucket(newTestBucket)))
              .ignoringException(NoSuchBucketException.class)
              .orFail();
        testBucket = newTestBucket;
    }

    String finalTestBucket = testBucket;

    s3.putBucketLifecycleConfiguration(blc -> blc
        .bucket(finalTestBucket)
        .lifecycleConfiguration(lc -> lc
            .rules(r -> r.expiration(ex -> ex.days(1))
                         .status(ExpirationStatus.ENABLED)
                         .filter(f -> f.prefix(""))
                         .id("delete-old"))));


    return finalTestBucket;
}
 
Example #7
Source File: S3TestUtils.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
public static void putObject(Class<?> testClass, S3Client s3, String bucketName, String objectKey, String content) {
    s3.putObject(r -> r.bucket(bucketName).key(objectKey), RequestBody.fromString(content));
    Waiter.run(() -> s3.getObjectAcl(r -> r.bucket(bucketName).key(objectKey)))
          .ignoringException(NoSuchBucketException.class, NoSuchObjectException.class)
          .orFail();
    addCleanupTask(testClass, () -> s3.deleteObject(r -> r.bucket(bucketName).key(objectKey)));
}
 
Example #8
Source File: ExceptionTranslationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void headBucket404_shouldTranslateException() {
    S3Exception s3Exception = create404S3Exception();
    Context.FailedExecution failedExecution = getFailedExecution(s3Exception,
                                                                 HeadBucketRequest.builder().build());

    assertThat(interceptor.modifyException(failedExecution, new ExecutionAttributes()))
        .isExactlyInstanceOf(NoSuchBucketException.class);
}
 
Example #9
Source File: S3SnapshotMessageStoreTest.java    From synapse with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldThrowExceptionIfBucketNotExists() {
    // given
    S3Exception bucketNotFoundException = NoSuchBucketException.builder().message("boom - simulate exception while loading from S3").build();

    when(snapshotReadService.retrieveLatestSnapshot(any())).thenThrow(bucketNotFoundException);

    // when
    try {
        new S3SnapshotMessageStore(STREAM_NAME, snapshotReadService, eventPublisher);

        fail("should throw RuntimeException");
    } catch (RuntimeException ignored) {

    }

    // then
    SnapshotReaderNotification expectedFailedEvent = builder()
            .withChannelName(STREAM_NAME)
            .withStatus(SnapshotReaderStatus.FAILED)
            .withMessage("Failed to load snapshot from S3: boom - simulate exception while loading from S3")
            .build();

    ArgumentCaptor<SnapshotReaderNotification> notificationArgumentCaptor = ArgumentCaptor.forClass(SnapshotReaderNotification.class);
    verify(eventPublisher, times(2)).publishEvent(notificationArgumentCaptor.capture());

    assertThat(notificationArgumentCaptor.getAllValues().get(1), is(expectedFailedEvent));
}
 
Example #10
Source File: ExceptionUnmarshallingIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void getObjectNoSuchBucket() {
    assertThatThrownBy(() -> s3.getObject(GetObjectRequest.builder().bucket(BUCKET + KEY).key(KEY).build()))
        .isInstanceOf(NoSuchBucketException.class)
        .satisfies(e -> assertMetadata((S3Exception) e, "NoSuchBucket"));
}
 
Example #11
Source File: ExceptionUnmarshallingIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void getBucketPolicyNoSuchBucket() {
    assertThatThrownBy(() -> s3.getBucketPolicy(b -> b.bucket(BUCKET + KEY)))
        .isInstanceOf(NoSuchBucketException.class)
        .satisfies(e -> assertMetadata((S3Exception) e, "NoSuchBucket"));
}
 
Example #12
Source File: ExceptionUnmarshallingIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void asyncGetBucketPolicyNoSuchBucket() {
    assertThatThrownBy(() -> s3Async.getBucketPolicy(b -> b.bucket(BUCKET + KEY)).join())
        .hasCauseExactlyInstanceOf(NoSuchBucketException.class)
        .satisfies(e -> assertMetadata((S3Exception) e.getCause(), "NoSuchBucket"));
}
 
Example #13
Source File: ExceptionUnmarshallingIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void getObjectAclNoSuchBucket() {
    assertThatThrownBy(() -> s3.getObjectAcl(b -> b.bucket(BUCKET + KEY).key(KEY)))
        .isInstanceOf(NoSuchBucketException.class)
        .satisfies(e -> assertMetadata((S3Exception) e, "NoSuchBucket"));
}
 
Example #14
Source File: S3TestUtils.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
public static void deleteBucketAndAllContents(S3Client s3, String bucketName) {
    try {
        System.out.println("Deleting S3 bucket: " + bucketName);
        ListObjectsResponse response = Waiter.run(() -> s3.listObjects(r -> r.bucket(bucketName)))
                                             .ignoringException(NoSuchBucketException.class)
                                             .orFail();
        List<S3Object> objectListing = response.contents();

        if (objectListing != null) {
            while (true) {
                for (Iterator<?> iterator = objectListing.iterator(); iterator.hasNext(); ) {
                    S3Object objectSummary = (S3Object) iterator.next();
                    s3.deleteObject(DeleteObjectRequest.builder().bucket(bucketName).key(objectSummary.key()).build());
                }

                if (response.isTruncated()) {
                    objectListing = s3.listObjects(ListObjectsRequest.builder()
                                                                     .bucket(bucketName)
                                                                     .marker(response.marker())
                                                                     .build())
                                      .contents();
                } else {
                    break;
                }
            }
        }


        ListObjectVersionsResponse versions = s3
                .listObjectVersions(ListObjectVersionsRequest.builder().bucket(bucketName).build());

        if (versions.deleteMarkers() != null) {
            versions.deleteMarkers().forEach(v -> s3.deleteObject(DeleteObjectRequest.builder()
                                                                                     .versionId(v.versionId())
                                                                                     .bucket(bucketName)
                                                                                     .key(v.key())
                                                                                     .build()));
        }

        if (versions.versions() != null) {
            versions.versions().forEach(v -> s3.deleteObject(DeleteObjectRequest.builder()
                                                                                .versionId(v.versionId())
                                                                                .bucket(bucketName)
                                                                                .key(v.key())
                                                                                .build()));
        }

        s3.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build());
    } catch (Exception e) {
        System.err.println("Failed to delete bucket: " + bucketName);
        e.printStackTrace();
    }
}