List of usage examples for com.amazonaws.services.s3 AmazonS3 doesBucketExistV2
public boolean doesBucketExistV2(String bucketName) throws SdkClientException, AmazonServiceException;
From source file:com.handywedge.binarystore.store.aws.BinaryStoreManagerImpl.java
License:MIT License
/** * ?/*w w w .j a v a 2 s .c o m*/ * * @param bucketName * @return s3client * @throws StoreException * @throws Exception */ private AmazonS3 getS3Client(String bucketName) throws StoreException { logger.debug("get S3 Client start."); // ? AWSCredentialsProvider provider = new EnvironmentVariableCredentialsProvider(); // ClientConfiguration clientConfig = new ClientConfiguration() // .withProtocol(Protocol.HTTPS) // Proxy // .withProxyHost("proxyHost") // .withProxyPort(80) // .withProxyUsername("proxyUsername") // .withProxyPassword("proxyPassword") .withConnectionTimeout(10000); // ? AmazonS3 s3client = AmazonS3ClientBuilder.standard().withCredentials(provider) .withClientConfiguration(clientConfig).withRegion(DEFAULT_REGION) .withForceGlobalBucketAccessEnabled(true).build(); logger.debug("Region={}", s3client.getRegion()); try { // ?? if (!CommonUtils.isNullOrEmpty(bucketName) && !(s3client.doesBucketExistV2(bucketName))) { s3client.createBucket(new CreateBucketRequest(bucketName, DEFAULT_REGION.getName())); } // Get location. String bucketLocation = s3client.getBucketLocation(new GetBucketLocationRequest(bucketName)); logger.info("bucket location={}", bucketLocation); } catch (AmazonClientException ace) { throw new StoreException(HttpStatus.SC_CONFLICT, ErrorClassification.BS0003, ace, "?"); } logger.info("get S3 Client end."); return s3client; }
From source file:com.netflix.spinnaker.kork.secrets.engines.S3SecretEngine.java
License:Apache License
@Override protected InputStream downloadRemoteFile(EncryptedSecret encryptedSecret) throws IOException { String region = encryptedSecret.getParams().get(STORAGE_REGION); String bucket = encryptedSecret.getParams().get(STORAGE_BUCKET); String objName = encryptedSecret.getParams().get(STORAGE_FILE_URI); AmazonS3ClientBuilder s3ClientBuilder = AmazonS3ClientBuilder.standard().withRegion(region); AmazonS3 s3Client = s3ClientBuilder.build(); try {//www. j ava2 s. com if (!s3Client.doesBucketExistV2(bucket)) { throw new SecretException( String.format("S3 Bucket does not exist. Bucket: %s, Region: %s", bucket, region)); } S3Object s3Object = s3Client.getObject(bucket, objName); return s3Object.getObjectContent(); } catch (AmazonS3Exception ex) { StringBuilder sb = new StringBuilder("Error reading contents of S3 -- "); if (403 == ex.getStatusCode()) { sb.append(String.format( "Unauthorized access. Check connectivity and permissions to the bucket. -- Bucket: %s, Object: %s, Region: %s.\n" + "Error: %s ", bucket, objName, region, ex.toString())); } else if (404 == ex.getStatusCode()) { sb.append(String.format( "Not found. Does secret file exist? -- Bucket: %s, Object: %s, Region: %s.\nError: %s", bucket, objName, region, ex.toString())); } else { sb.append(String.format("Error: %s", ex.toString())); } throw new SecretException(sb.toString()); } catch (AmazonClientException ex) { throw new SecretException( String.format("Error reading contents of S3. Bucket: %s, Object: %s, Region: %s.\nError: %s", bucket, objName, region, ex.toString())); } }
From source file:io.konig.maven.DeleteAwsS3BucketAction.java
License:Apache License
public AwsDeployment from(String path) throws Exception { String cfTemplatePresent = System.getProperty("cfTemplatePresent"); if (cfTemplatePresent == null || cfTemplatePresent.equals("N")) { try {/*from www . ja va 2 s . c o m*/ File file = deployment.file(path); ObjectMapper mapper = new ObjectMapper(); S3Bucket bucket = mapper.readValue(file, S3Bucket.class); deployment.verifyAWSCredentials(); Regions regions = Regions.fromName(bucket.getRegion()); AmazonS3 s3client = AmazonS3ClientBuilder.standard().withCredentials(deployment.getCredential()) .withRegion(regions).build(); String envtName = ""; if (System.getProperty("environmentName") != null) { envtName = System.getProperty("environmentName"); } String bucketName = StringUtils.replaceOnce(bucket.getBucketName(), "${environmentName}", envtName); s3client.deleteBucket(bucketName); boolean status = s3client.doesBucketExistV2(bucketName); if (!status) deployment.setResponse("AWS S3 Bucket is deleted ::" + bucketName); } catch (Exception e) { throw e; } } else { deployment.setResponse("S3 Bucket will be deleted through cloud formation stack"); } return deployment; }
From source file:org.ow2.proactive.scheduler.examples.S3ConnectorUtils.java
License:Open Source License
/** * Creates an S3 bucket if it does not exist and returns its name. * * @param bucketName//from w w w .ja va2 s . co m * @param s3 * @return */ protected static Bucket createBucketIfNotExists(String bucketName, AmazonS3 s3) { Bucket b; if (s3.doesBucketExistV2(bucketName)) { b = getBucket(bucketName, s3); } else { logger.info("Bucket " + bucketName + " does not exist. Creating bucket ..."); b = s3.createBucket(bucketName); logger.info("Bucket " + bucketName + " created successfully!"); } return b; }