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

The following examples show how to use software.amazon.awssdk.services.s3.model.S3Exception. 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: S3PinotFS.java    From incubator-pinot with Apache License 2.0 7 votes vote down vote up
@Override
public void init(Configuration config) {
  Preconditions.checkArgument(!isNullOrEmpty(config.getString(REGION)));
  String region = config.getString(REGION);

  AwsCredentialsProvider awsCredentialsProvider;
  try {

    if (!isNullOrEmpty(config.getString(ACCESS_KEY)) && !isNullOrEmpty(config.getString(SECRET_KEY))) {
      String accessKey = config.getString(ACCESS_KEY);
      String secretKey = config.getString(SECRET_KEY);
      AwsBasicCredentials awsBasicCredentials = AwsBasicCredentials.create(accessKey, secretKey);
      awsCredentialsProvider = StaticCredentialsProvider.create(awsBasicCredentials);
    } else {
      awsCredentialsProvider =
          AwsCredentialsProviderChain.builder().addCredentialsProvider(SystemPropertyCredentialsProvider.create())
              .addCredentialsProvider(EnvironmentVariableCredentialsProvider.create()).build();
    }

    _s3Client = S3Client.builder().region(Region.of(region)).credentialsProvider(awsCredentialsProvider).build();
  } catch (S3Exception e) {
    throw new RuntimeException("Could not initialize S3PinotFS", e);
  }
}
 
Example #2
Source File: GetBucketPolicy.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static String getPolicy(S3Client s3, String bucketName) {

        String policyText = "";
        System.out.format("Getting policy for bucket: \"%s\"\n\n", bucketName);

        GetBucketPolicyRequest policyReq = GetBucketPolicyRequest.builder()
                .bucket(bucketName)
                .build();

        try {
            GetBucketPolicyResponse policyRes = s3.getBucketPolicy(policyReq);
            policyText = policyRes.policy();
            return policyText;
        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        return "";
    }
 
Example #3
Source File: CompleteMultipartUploadFunctionalTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void completeMultipartUpload_syncClient_errorInResponseBody_correctType() {
    String bucket = "Example-Bucket";
    String key = "Example-Object";
    String xmlResponseBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                             + "<Error>\n"
                             + "<Code>InternalError</Code>\n"
                             + "<Message>We encountered an internal error. Please try again.</Message>\n"
                             + "<RequestId>656c76696e6727732072657175657374</RequestId>\n"
                             + "<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>\n"
                             + "</Error>";

    stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withBody(xmlResponseBody)));

    S3Client s3Client = getSyncClientBuilder().build();

    assertThatThrownBy(() -> s3Client.completeMultipartUpload(r -> r.bucket(bucket)
                                                                    .key(key)
                                                                    .uploadId("upload-id")))
        .isInstanceOf(S3Exception.class);
}
 
Example #4
Source File: CompleteMultipartUploadFunctionalTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void completeMultipartUpload_asyncClient_errorInResponseBody_correctType() {
    String bucket = "Example-Bucket";
    String key = "Example-Object";
    String xmlResponseBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                             + "<Error>\n"
                             + "<Code>InternalError</Code>\n"
                             + "<Message>We encountered an internal error. Please try again.</Message>\n"
                             + "<RequestId>656c76696e6727732072657175657374</RequestId>\n"
                             + "<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>\n"
                             + "</Error>";

    stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withBody(xmlResponseBody)));

    S3AsyncClient s3Client = getAsyncClientBuilder().build();

    assertThatThrownBy(() -> s3Client.completeMultipartUpload(r -> r.bucket(bucket)
                                                                    .key(key)
                                                                    .uploadId("upload-id"))
                                     .join())
        .isInstanceOf(CompletionException.class)
        .hasCauseInstanceOf(S3Exception.class);
}
 
Example #5
Source File: CompleteMultipartUploadFunctionalTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void completeMultipartUpload_syncClient_errorInResponseBody_correctCode() {
    String bucket = "Example-Bucket";
    String key = "Example-Object";
    String xmlResponseBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                             + "<Error>\n"
                             + "<Code>CustomError</Code>\n"
                             + "<Message>We encountered an internal error. Please try again.</Message>\n"
                             + "<RequestId>656c76696e6727732072657175657374</RequestId>\n"
                             + "<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>\n"
                             + "</Error>";

    stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withBody(xmlResponseBody)));

    S3Client s3Client = getSyncClientBuilder().build();

    assertThatThrownBy(() -> s3Client.completeMultipartUpload(r -> r.bucket(bucket)
                                                                    .key(key)
                                                                    .uploadId("upload-id")))
        .satisfies(e -> assertThat(((S3Exception)e).awsErrorDetails().errorCode()).isEqualTo("CustomError"));
}
 
Example #6
Source File: CompleteMultipartUploadFunctionalTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void completeMultipartUpload_asyncClient_errorInResponseBody_correctCode() {
    String bucket = "Example-Bucket";
    String key = "Example-Object";
    String xmlResponseBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                             + "<Error>\n"
                             + "<Code>CustomError</Code>\n"
                             + "<Message>We encountered an internal error. Please try again.</Message>\n"
                             + "<RequestId>656c76696e6727732072657175657374</RequestId>\n"
                             + "<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>\n"
                             + "</Error>";

    stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withBody(xmlResponseBody)));

    S3AsyncClient s3Client = getAsyncClientBuilder().build();

    assertThatThrownBy(() -> s3Client.completeMultipartUpload(r -> r.bucket(bucket)
                                                                    .key(key)
                                                                    .uploadId("upload-id"))
                                     .join())
        .satisfies(e -> {
            S3Exception s3Exception = (S3Exception) e.getCause();
            assertThat(s3Exception.awsErrorDetails().errorCode()).isEqualTo("CustomError");
        });
}
 
Example #7
Source File: CompleteMultipartUploadFunctionalTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void completeMultipartUpload_syncClient_errorInResponseBody_correctMessage() {
    String bucket = "Example-Bucket";
    String key = "Example-Object";
    String xmlResponseBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                             + "<Error>\n"
                             + "<Code>CustomError</Code>\n"
                             + "<Message>Foo bar</Message>\n"
                             + "<RequestId>656c76696e6727732072657175657374</RequestId>\n"
                             + "<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>\n"
                             + "</Error>";

    stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withBody(xmlResponseBody)));

    S3Client s3Client = getSyncClientBuilder().build();

    assertThatThrownBy(() -> s3Client.completeMultipartUpload(r -> r.bucket(bucket)
                                                                    .key(key)
                                                                    .uploadId("upload-id")))
        .satisfies(e -> assertThat(((S3Exception)e).awsErrorDetails().errorMessage()).isEqualTo("Foo bar"));
}
 
Example #8
Source File: CompleteMultipartUploadFunctionalTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void completeMultipartUpload_syncClient_errorInResponseBody_invalidErrorXml() {
    String bucket = "Example-Bucket";
    String key = "Example-Object";
    String xmlResponseBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                             + "<Error>\n"
                             + "<SomethingWeird></SomethingWeird>"
                             + "</Error>";

    stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withBody(xmlResponseBody)));

    S3Client s3Client = getSyncClientBuilder().build();

    assertThatThrownBy(() -> s3Client.completeMultipartUpload(r -> r.bucket(bucket)
                                                                    .key(key)
                                                                    .uploadId("upload-id")))
        .isInstanceOf(S3Exception.class);
}
 
Example #9
Source File: CompleteMultipartUploadFunctionalTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void completeMultipartUpload_asyncClient_errorInResponseBody_invalidErrorXml() {
    String bucket = "Example-Bucket";
    String key = "Example-Object";
    String xmlResponseBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                             + "<Error>\n"
                             + "<SomethingWeird></SomethingWeird>"
                             + "</Error>";

    stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withBody(xmlResponseBody)));

    S3AsyncClient s3Client = getAsyncClientBuilder().build();

    assertThatThrownBy(() -> s3Client.completeMultipartUpload(r -> r.bucket(bucket)
                                                                    .key(key)
                                                                    .uploadId("upload-id"))
                      .join())
        .isInstanceOf(CompletionException.class)
        .hasCauseInstanceOf(S3Exception.class);
}
 
Example #10
Source File: DeleteObjects.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void deleteBucketObjects(S3Client s3, String bucketName, String objectName) {

        // Create a S3Client object
        ArrayList<ObjectIdentifier> toDelete = new ArrayList<ObjectIdentifier>();
        toDelete.add(ObjectIdentifier.builder().key(objectName).build());

        try {
            DeleteObjectsRequest dor = DeleteObjectsRequest.builder()
                    .bucket(bucketName)
                    .delete(Delete.builder().objects(toDelete).build())
                    .build();
            s3.deleteObjects(dor);
        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        System.out.println("Done!");
    }
 
Example #11
Source File: DeleteWebsiteConfiguration.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void deleteBucketWebsiteConfig(S3Client s3,String bucketName ) {

        //Create a DeleteBucketWebsiteRequest object
        DeleteBucketWebsiteRequest delReq = DeleteBucketWebsiteRequest.builder()
                .bucket(bucketName)
                .build();
        try {
            s3.deleteBucketWebsite(delReq);

        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.out.println("Failed to delete website configuration!");
            System.exit(1);
        }
        System.out.println("Done!");
    }
 
Example #12
Source File: CompleteMultipartUploadFunctionalTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void completeMultipartUpload_asyncClient_errorInResponseBody_correctMessage() {
    String bucket = "Example-Bucket";
    String key = "Example-Object";
    String xmlResponseBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                             + "<Error>\n"
                             + "<Code>CustomError</Code>\n"
                             + "<Message>Foo bar</Message>\n"
                             + "<RequestId>656c76696e6727732072657175657374</RequestId>\n"
                             + "<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>\n"
                             + "</Error>";

    stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withBody(xmlResponseBody)));

    S3AsyncClient s3Client = getAsyncClientBuilder().build();

    assertThatThrownBy(() -> s3Client.completeMultipartUpload(r -> r.bucket(bucket)
                                                                    .key(key)
                                                                    .uploadId("upload-id"))
                      .join())
        .satisfies(e -> {
            S3Exception s3Exception = (S3Exception) e.getCause();
            assertThat(s3Exception.awsErrorDetails().errorMessage()).isEqualTo("Foo bar");
        });
}
 
Example #13
Source File: ListObjects.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void listBucketObjects(S3Client s3, String bucketName ) {

       try {
            ListObjectsRequest listObjects = ListObjectsRequest
                    .builder()
                    .bucket(bucketName)
                    .build();

            ListObjectsResponse res = s3.listObjects(listObjects);
            List<S3Object> objects = res.contents();

            for (ListIterator iterVals = objects.listIterator(); iterVals.hasNext(); ) {
                S3Object myValue = (S3Object) iterVals.next();
                System.out.print("\n The name of the key is " + myValue.key());
                System.out.print("\n The object is " + calKb(myValue.size()) + " KBs");
                System.out.print("\n The owner is " + myValue.owner());
                }
        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
 
Example #14
Source File: SetBucketPolicy.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void setPolicy(S3Client s3, String bucketName, String polText) {

        try {

            String policyText = getBucketPolicyFromFile(polText);
            PutBucketPolicyRequest policyReq = PutBucketPolicyRequest.builder()
                    .bucket(bucketName)
                    .policy(policyText)
                    .build();
            s3.putBucketPolicy(policyReq);
        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        System.out.println("Done!");
    }
 
Example #15
Source File: GetAcl.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static String getBucketACL(S3Client s3, String objectKey,String bucketName) {

        GetObjectAclRequest aclReq = GetObjectAclRequest.builder()
                .bucket(bucketName)
                .key(objectKey)
                .build();

        try {
            GetObjectAclResponse aclRes = s3.getObjectAcl(aclReq);
            List<Grant> grants = aclRes.grants();
            String grantee = "";
            for (Grant grant : grants) {
                System.out.format("  %s: %s\n", grant.grantee().id(),
                        grant.permission());
                grantee = grant.grantee().id();
            }
            return grantee;
        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        return "";
    }
 
Example #16
Source File: DeleteBucketPolicy.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void deleteS3BucketPolicy(S3Client s3, String bucketName) {

        //Create a DeleteBucketPolicyRequest object
        DeleteBucketPolicyRequest delReq = DeleteBucketPolicyRequest.builder()
                .bucket(bucketName)
                .build();
        try {
            s3.deleteBucketPolicy(delReq);
            System.out.println("Done!");

            //response.responseMetadata()
        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
 
Example #17
Source File: PutObject.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static String putS3Object(S3Client s3, String bucketName, String objectKey, String objectPath) {

        try {
            //Put a file into the bucket
            PutObjectResponse response = s3.putObject(PutObjectRequest.builder()
                            .bucket(bucketName)
                            .key(objectKey)
                            .build(),
                    RequestBody.fromBytes(getObjectFile(objectPath)));

            return response.eTag();
        } catch (S3Exception | FileNotFoundException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        return "";
    }
 
Example #18
Source File: SetWebsiteConfiguration.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void setWebsiteConfig(
        String bucketName, String indexDoc, String errorDoc) {

    WebsiteConfiguration websiteConfig = WebsiteConfiguration.builder()
            .indexDocument(IndexDocument.builder().suffix(indexDoc).build())
            .errorDocument(ErrorDocument.builder().key(errorDoc).build())
            .build();

    Region region = Region.US_WEST_2;
    S3Client s3 = S3Client.builder().region(region).build();

    PutBucketWebsiteRequest pubWebsiteReq = PutBucketWebsiteRequest.builder()
            .bucket(bucketName)
            .websiteConfiguration(websiteConfig)
            .build();

    try {
        s3.putBucketWebsite(pubWebsiteReq);
    } catch (S3Exception e) {
        System.out.format(
                "Failed to set the website configuration for bucket '%s'!\n",
                bucketName);
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
 
Example #19
Source File: Synch.java    From pegasus with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a S3 bucket with the given name
 *
 * @param name
 * @return boolean true in case a bucket was created, false if already exists,
 * @throws S3Exception in case unable to create bucket
 */
public boolean createS3Bucket(String name) {
    S3Client s3Client = S3Client.builder().region(mAWSRegion).build();
    boolean created = true;
    try {
        CreateBucketResponse cbr =
                s3Client.createBucket(
                        CreateBucketRequest.builder()
                                .bucket(name)
                                .createBucketConfiguration(
                                        CreateBucketConfiguration.builder()
                                                .locationConstraint(mAWSRegion.value())
                                                .build())
                                .build());
    } catch (S3Exception ex) {
        if (ex.getErrorCode().equals("BucketAlreadyOwnedByYou")) {
            created = false;
        } else {
            // rethrow the exception as is
            throw ex;
        }
    }

    return created;
}
 
Example #20
Source File: BucketAnalyticsConfigurationIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test(expected = S3Exception.class)
public void setBucketAnalyticsConfiguration_fails_when_requiredfield_is_missing() throws Exception {
    String configId = "id";
    StorageClassAnalysisDataExport dataExport = StorageClassAnalysisDataExport.builder()
                                                                              .outputSchemaVersion(
                                                                                      StorageClassAnalysisSchemaVersion.V_1)
                                                                              .destination(
                                                                                      AnalyticsExportDestination.builder()
                                                                                                                .build())
                                                                              .build();

    AnalyticsConfiguration config = AnalyticsConfiguration.builder()
                                                          .id(configId)
                                                          .storageClassAnalysis(StorageClassAnalysis.builder()
                                                                                                    .dataExport(dataExport)
                                                                                                    .build())
                                                          .build();

    s3.putBucketAnalyticsConfiguration(PutBucketAnalyticsConfigurationRequest.builder()
                                                                             .bucket(BUCKET_NAME)
                                                                             .analyticsConfiguration(config)
                                                                             .id(configId)
                                                                             .build());
}
 
Example #21
Source File: CachingS3ResourceLoader.java    From moirai with Apache License 2.0 6 votes vote down vote up
@Override
public String get() {
    GetObjectRequest.Builder requestBuilder = GetObjectRequest.builder().bucket(bucket).key(key);
    if (cachedObject != null) {
        requestBuilder.ifNoneMatch(cachedObject.eTag);
    }

    try {
        ResponseBytes<GetObjectResponse> responseBytes = s3Client.getObjectAsBytes(requestBuilder.build());
        return cacheAndReturnObject(responseBytes);
    } catch (S3Exception s3Exception) {
        if (s3Exception.statusCode() == 304) {
            return cachedObject.content;
        }

        throw s3Exception;
    }
}
 
Example #22
Source File: BucketAnalyticsConfigurationIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test(expected = S3Exception.class)
public void testAnalyticsConfiguration_works_properly_with_emptyFilter() throws Exception {
    String configId = "id";
    AnalyticsConfiguration config = AnalyticsConfiguration.builder()
                                                          .id(configId)
                                                          .filter(AnalyticsFilter.builder()
                                                                                 .build())
                                                          .storageClassAnalysis(getStorageClassAnalysis())
                                                          .build();

    s3.putBucketAnalyticsConfiguration(PutBucketAnalyticsConfigurationRequest.builder()
                                                                             .bucket(BUCKET_NAME)
                                                                             .analyticsConfiguration(config)
                                                                             .id(configId)
                                                                             .build());
}
 
Example #23
Source File: BucketAnalyticsConfigurationIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test(expected = S3Exception.class)
public void testAnalyticsConfiguration_works_properly_with_emptyPrefix() throws Exception {

    String configId = "id";
    AnalyticsConfiguration config = AnalyticsConfiguration.builder()
                                                          .id(configId)
                                                          .filter(AnalyticsFilter.builder()
                                                                                 .prefix("")
                                                                                 .build())
                                                          .storageClassAnalysis(getStorageClassAnalysis())
                                                          .build();

    s3.putBucketAnalyticsConfiguration(PutBucketAnalyticsConfigurationRequest.builder()
                                                                             .bucket(BUCKET_NAME)
                                                                             .analyticsConfiguration(config)
                                                                             .id(configId)
                                                                             .build());
}
 
Example #24
Source File: AwsS3V4SignerIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test (expected = S3Exception.class)
public void test_UsingSdkClient_WithIncorrectSigner_SetInConfig() {
    S3Client customClient = getClientBuilder()
        .overrideConfiguration(ClientOverrideConfiguration.builder()
                                                          .putAdvancedOption(SIGNER, Aws4Signer.create())
                                                          .build())
        .build();

   customClient.getObjectAsBytes(req -> req.bucket(BUCKET_NAME).key(KEY))
               .asString(StandardCharsets.UTF_8);
}
 
Example #25
Source File: S3IntegrationTestBase.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private static void createBucket(String bucketName, int retryCount) {
    try {
        s3.createBucket(
            CreateBucketRequest.builder()
                               .bucket(bucketName)
                               .createBucketConfiguration(
                                   CreateBucketConfiguration.builder()
                                                            .locationConstraint(BucketLocationConstraint.US_WEST_2)
                                                            .build())
                               .build());
    } catch (S3Exception e) {
        System.err.println("Error attempting to create bucket: " + bucketName);
        if (e.awsErrorDetails().errorCode().equals("BucketAlreadyOwnedByYou")) {
            System.err.printf("%s bucket already exists, likely leaked by a previous run\n", bucketName);
        } else if (e.awsErrorDetails().errorCode().equals("TooManyBuckets")) {
            System.err.println("Printing all buckets for debug:");
            s3.listBuckets().buckets().forEach(System.err::println);
            if (retryCount < 2) {
                System.err.println("Retrying...");
                createBucket(bucketName, retryCount + 1);
            } else {
                throw e;
            }
        } else {
            throw e;
        }
    }
}
 
Example #26
Source File: S3PinotFS.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream open(URI uri)
    throws IOException {
  try {
    String path = sanitizePath(uri.getPath());
    GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(uri.getHost()).key(path).build();

    ResponseBytes responseBytes = _s3Client.getObjectAsBytes(getObjectRequest);
    return responseBytes.asInputStream();
  } catch (S3Exception e) {
    throw e;
  }
}
 
Example #27
Source File: ExceptionTranslationInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private boolean isS3Exception404(Throwable thrown) {
    if (!(thrown instanceof S3Exception)) {
        return false;
    }

    return ((S3Exception) thrown).statusCode() == 404;
}
 
Example #28
Source File: GetObjectData.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void getObjectBytes (S3Client s3, String bucketName, String keyName, String path ) {

        try {
            // create a GetObjectRequest instance
            GetObjectRequest objectRequest = GetObjectRequest
                    .builder()
                    .key(keyName)
                    .bucket(bucketName)
                    .build();

            // get the byte[] this AWS S3 object
            ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
            byte[] data = objectBytes.asByteArray();

            //Write the data to a local file
            File myFile = new File(path );
            OutputStream os = new FileOutputStream(myFile);
            os.write(data);
            System.out.println("Successfully obtained bytes from an S3 object");

            // Close the file
            os.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (S3Exception e) {
          System.err.println(e.awsErrorDetails().errorMessage());
           System.exit(1);
        }
        // snippet-end:[s3.java2.getobjectdata.main]
    }
 
Example #29
Source File: SetAcl.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void setBucketAcl(S3Client s3, String bucketName, String id, String access) {

       try {
            // set access for the grantee  in acl
            Grantee grantee = Grantee.builder().emailAddress(id).build();
            Permission permission = Permission.valueOf(access);

            Grant ownerGrant = Grant.builder()
                    .grantee(builder -> {
                        builder.id(id)
                                .type(Type.CANONICAL_USER);
                    })
                    .permission(Permission.FULL_CONTROL)
                    .build();

            List<Grant> grantList2 = new ArrayList<>();
            grantList2.add(ownerGrant);

            //put the new acl
            AccessControlPolicy acl = AccessControlPolicy.builder()
                    .owner(builder -> builder.id(id))
                    .grants(grantList2)
                    .build();
            //put the new acl
            PutBucketAclRequest putAclReq = PutBucketAclRequest.builder()
                    .bucket(bucketName)
                    .accessControlPolicy(acl)
                    .build();

            s3.putBucketAcl(putAclReq);

        } catch (S3Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
 
Example #30
Source File: GetObjectTags.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void listTags(S3Client s3, String bucketName, String keyName ) {

         try {
           // create a GetObjectTaggingRequest instance
            GetObjectTaggingRequest getTaggingRequest = GetObjectTaggingRequest
                    .builder()
                    .key(keyName)
                    .bucket(bucketName)
                    .build();

            // get the tags for this AWS S3 object
            GetObjectTaggingResponse tags = s3.getObjectTagging(getTaggingRequest);
            List<Tag> tagSet= tags.tagSet();

            // write out the tags
            Iterator<Tag> tagIterator = tagSet.iterator();
            while(tagIterator.hasNext()) {

                Tag tag = (Tag)tagIterator.next();
                System.out.println(tag.key());
                System.out.println(tag.value());
            }
        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        // snippet-end:[s3.java2.getobjecttags.main]
    }