List of usage examples for com.amazonaws.services.s3 AmazonS3ClientBuilder defaultClient
public static AmazonS3 defaultClient()
From source file:aws.example.s3.SetWebsiteConfiguration.java
License:Open Source License
public static void setWebsiteConfig(String bucket_name, String index_doc, String error_doc) { BucketWebsiteConfiguration website_config = null; if (index_doc == null) { website_config = new BucketWebsiteConfiguration(); } else if (error_doc == null) { website_config = new BucketWebsiteConfiguration(index_doc); } else {//from w w w .jav a2 s . c o m website_config = new BucketWebsiteConfiguration(index_doc, error_doc); } final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); try { s3.setBucketWebsiteConfiguration(bucket_name, website_config); } catch (AmazonServiceException e) { System.out.format("Failed to set website configuration for bucket '%s'!\n", bucket_name); System.err.println(e.getErrorMessage()); System.exit(1); } }
From source file:com.springboot.demo.framework.aws.s3.S3Sample.java
License:Open Source License
public static void main(String[] args) throws IOException { /*//w ww.j a v a 2 s. co m * The ProfileCredentialsProvider will return your [default] * credential profile by reading from the credentials file located at * (~/.aws/credentials). */ AWSCredentials basicCredentials = new BasicAWSCredentials(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY); AWSCredentials credentials = null; try { credentials = new ProfileCredentialsProvider().getCredentials(); } catch (Exception e) { throw new AmazonClientException("Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (~/.aws/credentials), and is in valid format.", e); } /* * Create S3 Client */ AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); Region usWest2 = Region.getRegion(Regions.US_WEST_2); s3.setRegion(usWest2); String bucketName = "my-first-s3-bucket-" + UUID.randomUUID(); String key = "MyObjectKey"; System.out.println("==========================================="); System.out.println("Getting Started with Amazon S3"); System.out.println("===========================================\n"); try { /* * Create a new S3 bucket - Amazon S3 bucket names are globally unique, * so once a bucket name has been taken by any user, you can't create * another bucket with that same name. * * You can optionally specify a location for your bucket if you want to * keep your data closer to your applications or users. */ System.out.println("Creating bucket " + bucketName + "\n"); s3.createBucket(bucketName); /* * List the buckets in your account */ System.out.println("Listing buckets"); for (Bucket bucket : s3.listBuckets()) { System.out.println(" - " + bucket.getName()); } System.out.println(); /* * Upload an object to your bucket - You can easily upload a file to * S3, or upload directly an InputStream if you know the length of * the data in the stream. You can also specify your own metadata * when uploading to S3, which allows you set a variety of options * like content-type and content-encoding, plus additional metadata * specific to your applications. */ System.out.println("Uploading a new object to S3 from a file\n"); s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile())); /* * Returns an URL for the object stored in the specified bucket and key */ URL url = s3.getUrl(bucketName, key); System.out.println("upload file url : " + url.toString()); /* * 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 = s3.getObject(new GetObjectRequest(bucketName, key)); System.out.println("Content-Type: " + object.getObjectMetadata().getContentType()); displayTextInputStream(object.getObjectContent()); /* * List objects in your bucket by prefix - There are many options for * listing the objects in your bucket. Keep in mind that buckets with * many objects might truncate their results when listing their objects, * so be sure to check if the returned object listing is truncated, and * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve * additional results. */ System.out.println("Listing objects"); ObjectListing objectListing = s3 .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My")); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { System.out.println( " - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")"); } System.out.println(); /* * Delete an object - Unless versioning has been turned on for your bucket, * there is no way to undelete an object, so use caution when deleting objects. */ System.out.println("Deleting an object\n"); s3.deleteObject(bucketName, key); /* * Delete a bucket - A bucket must be completely empty before it can be * deleted, so remember to delete any objects from your buckets before * you try to delete them. */ System.out.println("Deleting bucket " + bucketName + "\n"); s3.deleteBucket(bucketName); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon S3, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:com.yahoo.athenz.auth.impl.aws.AwsPrivateKeyStore.java
License:Apache License
private static AmazonS3 initAmazonS3() { String s3Region = System.getProperty(ATHENZ_PROP_AWS_S3_REGION); if (null != s3Region && !s3Region.isEmpty()) { return AmazonS3ClientBuilder.standard().withRegion(s3Region).build(); }/*from w ww . j a va 2 s.c o m*/ return AmazonS3ClientBuilder.defaultClient(); }
From source file:org.apache.flink.streaming.tests.util.s3.S3UtilProgram.java
License:Apache License
private static List<String> listByFullPathPrefix(final String bucket, final String s3prefix) { return AmazonS3ClientBuilder.defaultClient().listObjects(bucket, s3prefix).getObjectSummaries().stream() .map(S3ObjectSummary::getKey).collect(Collectors.toList()); }
From source file:org.apache.flink.streaming.tests.util.s3.S3UtilProgram.java
License:Apache License
private static void deleteFile(ParameterTool params) { final String bucket = params.getRequired("bucket"); final String s3file = params.getRequired("s3file"); AmazonS3ClientBuilder.defaultClient().deleteObject(bucket, s3file); }
From source file:org.apache.flink.streaming.tests.util.s3.S3UtilProgram.java
License:Apache License
private static void deleteByFullPathPrefix(ParameterTool params) { final String bucket = params.getRequired("bucket"); final String s3prefix = params.getRequired("s3prefix"); String[] keys = listByFullPathPrefix(bucket, s3prefix).toArray(new String[] {}); if (keys.length > 0) { DeleteObjectsRequest request = new DeleteObjectsRequest(bucket).withKeys(keys); AmazonS3ClientBuilder.defaultClient().deleteObjects(request); }/*from www .j av a 2 s . c o m*/ }
From source file:org.apache.flink.streaming.tests.util.s3.S3UtilProgram.java
License:Apache License
private static void numberOfLinesInFile(ParameterTool params) { final String bucket = params.getRequired("bucket"); final String s3file = params.getRequired("s3file"); AmazonS3 s3client = AmazonS3ClientBuilder.defaultClient(); System.out.print(S3QueryUtil.queryFile(s3client, bucket, s3file, countQuery)); s3client.shutdown();//from w ww .j a v a 2 s. com }
From source file:org.apache.flink.streaming.tests.util.s3.S3UtilProgram.java
License:Apache License
private static void numberOfLinesInFilesWithFullAndNamePrefix(ParameterTool params) { final String bucket = params.getRequired("bucket"); final String s3prefix = params.getRequired("s3prefix"); final String s3filePrefix = params.get("s3filePrefix", ""); int parallelism = params.getInt("parallelism", 10); List<String> files = listByFullPathPrefix(bucket, s3prefix); ExecutorService executor = Executors.newFixedThreadPool(parallelism); AmazonS3 s3client = AmazonS3ClientBuilder.defaultClient(); List<CompletableFuture<Integer>> requests = submitLineCountingRequestsForFilesAsync(executor, s3client, bucket, files, s3filePrefix); int count = waitAndComputeTotalLineCountResult(requests); executor.shutdownNow();/*from www . j a v a 2 s .c om*/ s3client.shutdown(); System.out.print(count); }
From source file:org.goobi.api.mq.ImportEPHandler.java
private boolean checkIfExistsOnS3(final String _reference) { if (ConfigurationHelper.getInstance().useCustomS3()) { return false; }//from w w w .j ava2 s .co m String bucket; try { XMLConfiguration config = new XMLConfiguration( "/opt/digiverso/goobi/config/plugin_wellcome_editorial_process_creation.xml"); bucket = config.getString("bucket", "wellcomecollection-editorial-photography");// "wellcomecollection-editorial-photography"; log.debug("using bucket " + bucket); } catch (ConfigurationException e) { bucket = "wellcomecollection-editorial-photography"; log.debug("using bucket " + bucket); } String reference = _reference.replaceAll(" |\t", "_"); int refLen = reference.length(); String keyPrefix = reference.substring(refLen - 2, refLen) + "/" + reference + "/"; String key = keyPrefix + reference + ".xml"; AmazonS3 s3client = AmazonS3ClientBuilder.defaultClient(); return s3client.doesObjectExist(bucket, key); }