software.amazon.awssdk.services.s3.S3Client Java Examples
The following examples show how to use
software.amazon.awssdk.services.s3.S3Client.
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 |
@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: S3EndpointResolutionTest.java From aws-sdk-java-v2 with Apache License 2.0 | 7 votes |
@Test public void regionalSettingEnabled_usesRegionalIadEndpoint() throws UnsupportedEncodingException { EnvironmentVariableHelper environmentVariableHelper = new EnvironmentVariableHelper(); environmentVariableHelper.set(SdkSystemSetting.AWS_S3_US_EAST_1_REGIONAL_ENDPOINT.environmentVariable(), "regional"); mockHttpClient.stubNextResponse(mockListObjectsResponse()); S3Client s3Client = S3Client.builder() .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) .httpClient(mockHttpClient) .region(Region.US_EAST_1) .serviceConfiguration(S3Configuration.builder() .pathStyleAccessEnabled(true) .build()) .build(); try { s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build()); assertThat(mockHttpClient.getLastRequest().getUri().getHost()).isEqualTo("s3.us-east-1.amazonaws.com"); } finally { environmentVariableHelper.reset(); } }
Example #3
Source File: ChecksumResetsOnRetryTest.java From aws-sdk-java-v2 with Apache License 2.0 | 7 votes |
@Before public void setup() { StaticCredentialsProvider credentials = StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid")); s3Client = S3Client.builder() .credentialsProvider(credentials) .region(Region.US_WEST_2) .endpointOverride(URI.create("http://localhost:" + mockServer.port())) .build(); s3AsyncClient = S3AsyncClient.builder() .credentialsProvider(credentials) .region(Region.US_WEST_2) .endpointOverride(URI.create("http://localhost:" + mockServer.port())) .build(); body = "foo".getBytes(StandardCharsets.UTF_8); String checksumAsHexString = "acbd18db4cc2f85cedef654fccc4a4d8"; bodyEtag = "\"" + checksumAsHexString + "\""; bodyWithTrailingChecksum = ArrayUtils.addAll(body, BinaryUtils.fromHex(checksumAsHexString)); }
Example #4
Source File: GetObjectData.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
public static void main(String[] args) { if (args.length < 3) { System.out.println("Please specify a bucket name, a key name that represents a PDF file (ie, book.pdf), and a path (ie, C:\\AWS\\AdobePDF.pdf)"); System.exit(1); } String bucketName = args[0]; String keyName = args[1]; String path = args[2]; Region region = Region.US_WEST_2; S3Client s3 = S3Client.builder() .region(region) .build(); getObjectBytes(s3,bucketName,keyName, path); }
Example #5
Source File: GetBucketPolicy.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
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 #6
Source File: PutObject.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
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 #7
Source File: S3FileSystem.java From dremio-oss with Apache License 2.0 | 6 votes |
private S3Client getSyncClient(String bucket) throws IOException { try { return syncClientCache.get(bucket); } catch (ExecutionException | SdkClientException e ) { final Throwable cause = e.getCause(); final Throwable toChain; if (cause == null) { toChain = e; } else { Throwables.throwIfInstanceOf(cause, UserException.class); Throwables.throwIfInstanceOf(cause, IOException.class); toChain = cause; } throw new IOException(String.format("Unable to create a sync S3 client for bucket %s", bucket), toChain); } }
Example #8
Source File: S3PutObject.java From piper with Apache License 2.0 | 6 votes |
@Override public Object handle (TaskExecution aTask) throws Exception { AmazonS3URI s3Uri = new AmazonS3URI(aTask.getRequiredString("uri")); String bucketName = s3Uri.getBucket(); String key = s3Uri.getKey(); S3Client s3 = S3Client.builder().build(); s3.putObject(PutObjectRequest.builder() .bucket(bucketName) .key(key) .acl(aTask.getString("acl")!=null?ObjectCannedACL.fromValue(aTask.getString("acl")):null) .build(), Paths.get(aTask.getRequiredString("filepath"))); return null; }
Example #9
Source File: CompleteMultipartUploadFunctionalTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@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 #10
Source File: CompleteMultipartUploadFunctionalTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@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 #11
Source File: S3TestHelper.java From edison-microservice with Apache License 2.0 | 6 votes |
public static S3Client createS3Client(final Integer mappedPort) { final AwsBasicCredentials credentials = AwsBasicCredentials.create("test", "test"); final StaticCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(credentials); return S3Client.builder() .credentialsProvider(credentialsProvider) .endpointOverride(URI.create(String.format("http://localhost:%d", mappedPort))) .region(Region.EU_CENTRAL_1) .serviceConfiguration(S3Configuration.builder() .pathStyleAccessEnabled(true) .build()) .overrideConfiguration(ClientOverrideConfiguration .builder() .putAdvancedOption(SdkAdvancedClientOption.SIGNER, new NoOpSigner()) .build()) .build(); }
Example #12
Source File: DeleteObjects.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
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 #13
Source File: DeleteObjects.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
public static void main(String[] args) { final String USAGE = "\n" + "To run this example, supply the name of an S3 bucket and at least\n" + "one object name (key) to delete.\n" + "\n" + "Ex: DeleteObjects <bucketname> <objectname>\n"; if (args.length < 2) { System.out.println(USAGE); System.exit(1); } String bucketName = args[0]; String objectName = args[1]; System.out.println("Deleting an object from the S3 bucket: " + bucketName); // Create a S3Client object Region region = Region.US_WEST_2; S3Client s3 = S3Client.builder().region(region).build(); deleteBucketObjects(s3, bucketName, objectName); }
Example #14
Source File: S3EndpointResolutionTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
/** * Only APIs that operate on buckets uses virtual addressing. Service level operations like ListBuckets will use the normal * endpoint. */ @Test public void serviceLevelOperation_UsesStandardEndpoint() throws Exception { mockHttpClient.stubNextResponse(mockListBucketsResponse()); S3Client s3Client = buildClient(null); s3Client.listBuckets(); assertThat(mockHttpClient.getLastRequest().getUri()) .as("Uses regional S3 endpoint without bucket") .isEqualTo(URI.create(ENDPOINT_WITHOUT_BUCKET + "/")); assertThat(mockHttpClient.getLastRequest().encodedPath()) .as("Bucket is not in resource path") .isEqualTo("/"); }
Example #15
Source File: DeleteBucketPolicy.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " DeleteBucketPolicy <bucket>\n\n" + "Where:\n" + " bucket - the bucket to delete the policy from (i.e., bucket1)\n\n" + "Example:\n" + " bucket1\n\n"; if (args.length < 1) { System.out.println(USAGE); System.exit(1); } String bucketName = args[0]; System.out.format("Deleting policy from bucket: \"%s\"\n\n", bucketName); //Create a S3Client object Region region = Region.US_WEST_2; S3Client s3 = S3Client.builder().region(region).build(); //Delete the bucket policy deleteS3BucketPolicy(s3, bucketName); }
Example #16
Source File: S3EndpointResolutionTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void regionalSettingUnset_usesGlobalEndpoint() throws UnsupportedEncodingException { mockHttpClient.stubNextResponse(mockListObjectsResponse()); S3Client s3Client = S3Client.builder() .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) .httpClient(mockHttpClient) .region(Region.US_EAST_1) .serviceConfiguration(S3Configuration.builder() .pathStyleAccessEnabled(true) .build()) .build(); s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build()); assertThat(mockHttpClient.getLastRequest().getUri().getHost()).isEqualTo("s3.amazonaws.com"); }
Example #17
Source File: S3ObjectOperations.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
public static void createBucket( S3Client s3Client, String bucketName, Region region) { try{ s3Client.createBucket(CreateBucketRequest .builder() .bucket(bucketName) .createBucketConfiguration( CreateBucketConfiguration.builder() .locationConstraint(region.id()) .build()) .build()); System.out.println(bucketName); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
Example #18
Source File: S3EndpointResolutionTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void regionalSettingDisabled_usesGlobalEndpoint() throws UnsupportedEncodingException { EnvironmentVariableHelper environmentVariableHelper = new EnvironmentVariableHelper(); environmentVariableHelper.set(SdkSystemSetting.AWS_S3_US_EAST_1_REGIONAL_ENDPOINT.environmentVariable(), "nonregional"); mockHttpClient.stubNextResponse(mockListObjectsResponse()); S3Client s3Client = S3Client.builder() .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) .httpClient(mockHttpClient) .region(Region.US_EAST_1) .serviceConfiguration(S3Configuration.builder() .pathStyleAccessEnabled(true) .build()) .build(); try { s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build()); assertThat(mockHttpClient.getLastRequest().getUri().getHost()).isEqualTo("s3.amazonaws.com"); } finally { environmentVariableHelper.reset(); } }
Example #19
Source File: SetWebsiteConfiguration.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
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 #20
Source File: AwsS3SenderTest.java From fluency with Apache License 2.0 | 6 votes |
@Test void buildClientWithDefaults() { AwsS3Sender.Config config = new AwsS3Sender.Config(); S3Client s3Client = mock(S3Client.class); S3ClientBuilder s3ClientBuilder = mock(S3ClientBuilder.class); doReturn(s3Client).when(s3ClientBuilder).build(); new AwsS3Sender(s3ClientBuilder, config); verify(s3ClientBuilder, times(1)).build(); verify(s3ClientBuilder, times(0)).endpointOverride(any()); verify(s3ClientBuilder, times(0)).region(any()); verify(s3ClientBuilder, times(0)).credentialsProvider(any()); }
Example #21
Source File: DeleteWebsiteConfiguration.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
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 #22
Source File: GetObjectTags.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
public static void main(String[] args) { if (args.length < 2) { System.out.println("Please specify a bucket name and key name"); System.exit(1); } String bucketName = args[0]; String keyName = args[1]; Region region = Region.US_WEST_2; S3Client s3 = S3Client.builder() .region(region) .build(); listTags(s3,bucketName,keyName ); }
Example #23
Source File: ListObjects.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
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 #24
Source File: DeleteWebsiteConfiguration.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
public static void main(String[] args) { final String USAGE = "\n" + "DeleteWebsiteConfiguration - delete the website configuration for an S3 bucket\n\n" + "Usage: DeleteWebsiteConfiguration <bucket>\n\n" + "Where:\n" + " bucket - the bucket to delete the website configuration from\n"; if (args.length < 1) { System.out.println(USAGE); System.exit(1); } final String bucketName = args[0]; System.out.format("Deleting website configuration for bucket: %s\n", bucketName); // Create a S3Client object Region region = Region.US_WEST_2; S3Client s3 = S3Client.builder().region(region).build(); deleteBucketWebsiteConfig(s3, bucketName); }
Example #25
Source File: S3EndpointResolutionTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void accessPointArn_customEndpoint_throwsIllegalArgumentException() throws Exception { URI customEndpoint = URI.create("https://foobar.amazonaws.com"); mockHttpClient.stubNextResponse(mockListObjectsResponse()); S3Client s3Client = clientBuilder().endpointOverride(customEndpoint).build(); String accessPointArn = "arn:aws:s3:ap-south-1:12345678910:accesspoint:foobar"; assertThatThrownBy(() -> s3Client.listObjects(ListObjectsRequest.builder().bucket(accessPointArn).build())) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("endpoint override"); }
Example #26
Source File: S3EndpointResolutionTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
/** * If customer is using HTTP we need to preserve that scheme when switching to virtual addressing. */ @Test public void customHttpEndpoint_PreservesSchemeWhenSwitchingToVirtualAddressing() throws Exception { URI customEndpoint = URI.create("http://s3-external-1.amazonaws.com"); mockHttpClient.stubNextResponse(mockListObjectsResponse()); S3Client s3Client = clientBuilderWithMockSigner().endpointOverride(customEndpoint).build(); s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build()); assertEndpointMatches(mockHttpClient.getLastRequest(), String.format("http://%s.s3-external-1.amazonaws.com", BUCKET)); }
Example #27
Source File: S3EndpointResolutionTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
/** * If a custom S3 endpoint is provided (like s3-external-1 or a FIPS endpoint) then we should still use virtual addressing * when possible. */ @Test public void customS3EndpointProvided_UsesVirtualAddressing() throws Exception { URI customEndpoint = URI.create("https://s3-external-1.amazonaws.com"); mockHttpClient.stubNextResponse(mockListObjectsResponse()); S3Client s3Client = clientBuilder().endpointOverride(customEndpoint).build(); s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build()); assertEndpointMatches(mockHttpClient.getLastRequest(), String.format("https://%s.s3-external-1.amazonaws.com", BUCKET)); }
Example #28
Source File: S3WithUrlHttpClientIntegrationTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
/** * Creates all the test resources for the tests. */ @BeforeClass public static void createResources() throws Exception { s3 = S3Client.builder() .region(REGION) .httpClient(UrlConnectionHttpClient.builder().build()) .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN) .overrideConfiguration(o -> o.addExecutionInterceptor(new UserAgentVerifyingInterceptor())) .build(); createBucket(BUCKET_NAME, REGION); }
Example #29
Source File: S3AutoConfiguration.java From synapse with Apache License 2.0 | 5 votes |
@Bean @ConditionalOnMissingBean public S3Client s3Client(final AwsProperties awsProperties, final AwsCredentialsProvider awsCredentialsProvider) { return S3Client .builder() .region(of(awsProperties.getRegion())) .credentialsProvider(awsCredentialsProvider) .build(); }
Example #30
Source File: SetAcl.java From aws-doc-sdk-examples with Apache License 2.0 | 5 votes |
public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " SetAcl <bucket> [object] <email> <permission>\n\n" + "Where:\n" + " bucket - the bucket to grant permissions on\n" + " object - the object grant permissions on\n" + " owner id - The ID of the owner of this bucket (you can get this value from the AWS Console)\n" + " permission - The permission(s) to set. Can be one of:\n" + " FULL_CONTROL, READ, WRITE, READ_ACP, WRITE_ACP\n\n" + "Examples:\n" + " SetAcl testbucket testobject <uda0cb5d2c39f310136ead6278...> WRITE\n\n"; if (args.length < 4) { System.out.println(USAGE); System.exit(1); } String bucketName = args[0]; String objectKey = args[1]; String id = args[2]; String access = args[3]; System.out.format("Setting %s access for %s\n", access, id); System.out.println("for object: " + objectKey); System.out.println(" in bucket: " + bucketName); //Create the S3Client object Region region = Region.US_WEST_2; S3Client s3 = S3Client.builder().region(region).build(); setBucketAcl(s3, bucketName, id,access); System.out.println("Done!"); }