/** * Add IAM specific headers based on the credentials set & any optional * parameters added to the CreateBucketRequest object * * @param request * @param createBucketRequest * @return Request<CreateBucketRequest> */ protected Request<CreateBucketRequest> addIAMHeaders(Request<CreateBucketRequest> request, CreateBucketRequest createBucketRequest){ if ((null != this.awsCredentialsProvider ) && (this.awsCredentialsProvider.getCredentials() instanceof IBMOAuthCredentials)) { if (null != createBucketRequest.getServiceInstanceId()) { request.addHeader(Headers.IBM_SERVICE_INSTANCE_ID, createBucketRequest.getServiceInstanceId()); } else { IBMOAuthCredentials oAuthCreds = (IBMOAuthCredentials)this.awsCredentialsProvider.getCredentials(); if (oAuthCreds.getServiceInstanceId() != null) { request.addHeader(Headers.IBM_SERVICE_INSTANCE_ID, oAuthCreds.getServiceInstanceId()); } } } return request; }
/** * Create an amazon bucket in the specified region * @param bucket - The s3 bucket name * @param region - The S3 region the bucket should be created in * @param accessList - The access control list settings for the bucket */ public void createBucket(final String bucket, final Region region, final CannedAccessControlList cannedACL, final AccessControlList accessList){ final CreateBucketRequest request = new CreateBucketRequest(bucket, region); if(cannedACL!=null){ request.withCannedAcl(cannedACL); } if(accessList!=null){ request.withAccessControlList(accessList); } this.client.createBucket(request); }
@Override public Bucket createBucket(CreateBucketRequest createBucketRequest) throws AmazonClientException, AmazonServiceException { if ("nonExistingBucket".equals(createBucketRequest.getBucketName())) { nonExistingBucketCreated = true; } Bucket bucket = new Bucket(); bucket.setName(createBucketRequest.getBucketName()); bucket.setCreationDate(new Date()); bucket.setOwner(new Owner("c2efc7302b9011ba9a78a92ac5fd1cd47b61790499ab5ddf5a37c31f0638a8fc ", "Christian Mueller")); return bucket; }
/** * Test Service Instance Id is added to the Request Object and * the runtime parameter takes precedence over any set by the * CredentialProvider * */ @Test public void testServiceInstanceRuntimeParamTakesPrecedence() { String serviceInstanceId = "12345"; CreateBucketRequest request = new CreateBucketRequest("testbucket").withServiceInstanceId(serviceInstanceId); Request<CreateBucketRequest> defaultRequest = new DefaultRequest(Constants.S3_SERVICE_DISPLAY_NAME); AmazonS3Client s3Client = new AmazonS3Client(new BasicIBMOAuthCredentials(new TokenMangerUtilTest(), "54321")); defaultRequest = s3Client.addIAMHeaders(defaultRequest, request); assertEquals(defaultRequest.getHeaders().get(Headers.IBM_SERVICE_INSTANCE_ID), serviceInstanceId); }
/** * Test Service Instance Id is added to the Request Object by * the CredentialProvider * */ @Test public void testServiceInstanceHeaderIsAddedByCredentialProvdier() { String serviceInstanceId = "12345"; CreateBucketRequest request = new CreateBucketRequest("testbucket"); Request<CreateBucketRequest> defaultRequest = new DefaultRequest(Constants.S3_SERVICE_DISPLAY_NAME); AmazonS3Client s3Client = new AmazonS3Client(new BasicIBMOAuthCredentials(new TokenMangerUtilTest(), serviceInstanceId)); defaultRequest = s3Client.addIAMHeaders(defaultRequest, request); assertEquals(defaultRequest.getHeaders().get(Headers.IBM_SERVICE_INSTANCE_ID), serviceInstanceId); }
/** * Test No IAM Headers are added & no null pointers are thrown * */ @Test public void testNoIAMHeadersAreAdded() { CreateBucketRequest request = new CreateBucketRequest("testbucket"); Request<CreateBucketRequest> defaultRequest = new DefaultRequest(Constants.S3_SERVICE_DISPLAY_NAME); AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials("987654321", "123456789")); defaultRequest = s3Client.addIAMHeaders(defaultRequest, request); assertEquals(defaultRequest.getHeaders().get(Headers.IBM_SERVICE_INSTANCE_ID), null); }
/** * Creates new bucket if not existing * * @param bucket Bucket * @param region Region * @return true on success, false on failure */ public static boolean createBucketIfNotExists(String bucket, String region) { if (getS3Client().doesBucketExist(bucket)) { return false; } getS3Client().createBucket(new CreateBucketRequest(bucket, region)); return true; }
@Override public Bucket createBucket(final String bucketName, final CannedAccessControlList cannedAcl) throws AmazonClientException, AmazonServiceException { LOGGER.info("createBucket invoked, bucketName: {} and cannedAccessControlList: {}",bucketName,cannedAcl); final CreateBucketRequest createBucketReq = new CreateBucketRequest(bucketName).withCannedAcl(cannedAcl); return s3client.createBucket(createBucketReq); }
@Override public Bucket createBucket(final String bucketName, final boolean isPublicAccessible) throws AmazonClientException, AmazonServiceException { LOGGER.info("createBucket invoked, bucketName: {} and isPublicAccessible: {}",bucketName,isPublicAccessible); final CreateBucketRequest createBucketReq = new CreateBucketRequest(bucketName); if(isPublicAccessible){ createBucketReq.setCannedAcl(CannedAccessControlList.PublicRead); } return s3client.createBucket(createBucketReq); }
public static void createBucket(AmazonS3 s3client, String bucketName) { if(s3client.doesBucketExist(bucketName)) { for(S3ObjectSummary s : S3Objects.inBucket(s3client, bucketName)) { s3client.deleteObject(bucketName, s.getKey()); } s3client.deleteBucket(bucketName); } Assert.assertFalse(s3client.doesBucketExist(bucketName)); // Note that CreateBucketRequest does not specify region. So bucket is // bucketName s3client.createBucket(new CreateBucketRequest(bucketName)); }
@Override public Optional<Boolean> createBucket(Bucket bucket) { try { CreateBucketRequest bucketRequest = new CreateBucketRequest(bucket.getBucketName(), Region.US_West); bucketRequest.withCannedAcl(CannedAccessControlList.PublicRead); s3Client.createBucket(bucketRequest); return TRUE; } catch (Exception e) { log.error("Error creating bucket", e); return FALSE; } }
public static void main(String[] args) { if (!S3.doesBucketExist(S3_BUCKET_NAME)) { S3.createBucket(new CreateBucketRequest(S3_BUCKET_NAME)); } System.out.println("Using Amazon S3 Bucket: " + S3_BUCKET_NAME); }
public static void main(String[] args) throws Exception { CreateBucketRequest createBucketRequest = new CreateBucketRequest(AWSResources.S3_BUCKET_NAME); try { AWSResources.S3.createBucket(createBucketRequest); System.out.println("Bucket Created..."); } catch (AmazonS3Exception ex){ if(!ex.getErrorCode().equals(createBucketRequest)) { throw ex; } } }
@Override public Bucket createBucket(CreateBucketRequest createBucketRequest) throws AmazonClientException, AmazonServiceException { return delegate.createBucket(createBucketRequest); }
@Override public Bucket createBucket(String bucketName) throws SdkClientException, AmazonServiceException { return createBucket(new CreateBucketRequest(bucketName)); }
@Override @Deprecated public Bucket createBucket(String bucketName, Region region) throws SdkClientException, AmazonServiceException { return createBucket(new CreateBucketRequest(bucketName, region)); }
@Override @Deprecated public Bucket createBucket(String bucketName, String region) throws SdkClientException, AmazonServiceException { return createBucket(new CreateBucketRequest(bucketName, region)); }
@Override public Bucket createBucket(CreateBucketRequest createBucketRequest) throws SdkClientException, AmazonServiceException { createBucketRequest = beforeClientExecution(createBucketRequest); rejectNull(createBucketRequest, "The CreateBucketRequest parameter must be specified when creating a bucket"); String bucketName = createBucketRequest.getBucketName(); rejectNull(bucketName, "The bucket name parameter must be specified when creating a bucket"); bucketName = bucketName.trim(); String requestRegion = createBucketRequest.getRegion(); URI requestEndpoint = getCreateBucketEndpoint(requestRegion); BucketNameUtils.validateBucketName(bucketName); Request<CreateBucketRequest> request = createRequest(bucketName, null, createBucketRequest, HttpMethodName.PUT, requestEndpoint); //Add IBM Service Instance Id & Encryption to headers if ((null != this.awsCredentialsProvider ) && (this.awsCredentialsProvider.getCredentials() instanceof IBMOAuthCredentials)) { IBMOAuthCredentials oAuthCreds = (IBMOAuthCredentials)this.awsCredentialsProvider.getCredentials(); if (oAuthCreds.getServiceInstanceId() != null) { request.addHeader(Headers.IBM_SERVICE_INSTANCE_ID, oAuthCreds.getServiceInstanceId()); if (null != createBucketRequest.getEncryptionType()) { request.addHeader(Headers.IBM_SSE_KP_ENCRYPTION_ALGORITHM, createBucketRequest.getEncryptionType().getKmsEncryptionAlgorithm()); request.addHeader(Headers.IBM_SSE_KP_CUSTOMER_ROOT_KEY_CRN, createBucketRequest.getEncryptionType().getIBMSSEKMSCustomerRootKeyCrn()); } } } if (createBucketRequest.getAccessControlList() != null) { addAclHeaders(request, createBucketRequest.getAccessControlList()); } else if (createBucketRequest.getCannedAcl() != null) { request.addHeader(Headers.S3_CANNED_ACL, createBucketRequest.getCannedAcl().toString()); } /* * If we're talking to a region-specific endpoint other than the US, we * *must* specify a location constraint. Try to derive the region from * the endpoint. */ if (getSignerRegion() != null && !getSignerRegion().equals("us-east-1") && StringUtils.isNullOrEmpty(requestRegion)) { requestRegion = AwsHostNameUtils.parseRegion(requestEndpoint.getHost(), S3_SERVICE_NAME); } /* * We can only send the CreateBucketConfiguration if we're *not* * creating a bucket in the US region. */ if (requestRegion != null && !StringUtils.upperCase(requestRegion).equals(Region.US_Standard.toString())) { XmlWriter xml = new XmlWriter(); xml.start("CreateBucketConfiguration", "xmlns", Constants.XML_NAMESPACE); xml.start("LocationConstraint").value(requestRegion).end(); xml.end(); request.setContent(new ByteArrayInputStream(xml.getBytes())); } invoke(request, voidResponseHandler, bucketName, null); return new Bucket(bucketName); }
private boolean shouldPerformHeadRequestToFindRegion(Request<?> request, String bucket) { return bucket != null && !(request.getOriginalRequest() instanceof CreateBucketRequest) && bucketRegionShouldBeCached(request); }
@Override public Bucket createBucket(CreateBucketRequest createBucketRequest) throws SdkClientException, AmazonServiceException { return call(() -> getDelegate().createBucket(createBucketRequest)); }
public void deploy() { /* * check for existence because once created, we aren't going to delete * it. Amazon could give the name to someone else. This won't matter * when we move CDN. */ final Optional<Bucket> maybeBucket = this.s3.listBuckets() .stream() .filter(b -> b.getName().equals(BucketName)) .findAny(); if (!maybeBucket.isPresent()) { this.s3.createBucket(new CreateBucketRequest(BucketName)); } this.s3.setBucketWebsiteConfiguration(BucketName, new BucketWebsiteConfiguration("index.html")); /* * Zone must exist */ final HostedZone zone = this.route53.listHostedZonesByName(new ListHostedZonesByNameRequest().withDNSName(Tld)) .getHostedZones() .stream() .findAny() .get(); final String zoneId = zone.getId().replaceAll("/.*/", ""); final ResourceRecord record = new ResourceRecord().withValue(Domain + ".s3.amazonaws.com"); final ResourceRecordSet records = new ResourceRecordSet().withName(Domain + ".") .withType(RRType.CNAME) .withTTL(60L) .withResourceRecords(record); final Change change = new Change().withAction(ChangeAction.UPSERT).withResourceRecordSet(records); final List<Change> changes = Collections.singletonList(change); final ChangeBatch changeBatch = new ChangeBatch().withChanges(changes); final ChangeResourceRecordSetsRequest changeRecordsRequest = new ChangeResourceRecordSetsRequest() .withHostedZoneId(zoneId).withChangeBatch(changeBatch); this.route53.changeResourceRecordSets(changeRecordsRequest); upload(SignupObjectName); upload(ThankYouObjectName); }
@Override public void doStart() throws Exception { super.doStart(); s3Client = configuration.getAmazonS3Client() != null ? configuration.getAmazonS3Client() : createS3Client(); if (ObjectHelper.isNotEmpty(configuration.getAmazonS3Endpoint())) { s3Client.setEndpoint(configuration.getAmazonS3Endpoint()); } String fileName = getConfiguration().getFileName(); if (fileName != null) { LOG.trace("File name [{}] requested, so skipping bucket check...", fileName); return; } String bucketName = getConfiguration().getBucketName(); LOG.trace("Querying whether bucket [{}] already exists...", bucketName); try { s3Client.listObjects(new ListObjectsRequest(bucketName, null, null, null, 0)); LOG.trace("Bucket [{}] already exists", bucketName); return; } catch (AmazonServiceException ase) { /* 404 means the bucket doesn't exist */ if (ase.getStatusCode() != 404) { throw ase; } } LOG.trace("Bucket [{}] doesn't exist yet", bucketName); // creates the new bucket because it doesn't exist yet CreateBucketRequest createBucketRequest = new CreateBucketRequest(getConfiguration().getBucketName()); if (getConfiguration().getRegion() != null) { createBucketRequest.setRegion(getConfiguration().getRegion()); } LOG.trace("Creating bucket [{}] in region [{}] with request [{}]...", new Object[]{configuration.getBucketName(), configuration.getRegion(), createBucketRequest}); s3Client.createBucket(createBucketRequest); LOG.trace("Bucket created"); if (configuration.getPolicy() != null) { LOG.trace("Updating bucket [{}] with policy [{}]", bucketName, configuration.getPolicy()); s3Client.setBucketPolicy(bucketName, configuration.getPolicy()); LOG.trace("Bucket policy updated"); } }
public void createS3BucketIfNotExist(String bucketName, String regionName, boolean isOutputBucket) throws TranscodeException{ s3client.setRegion(Region.getRegion(Regions.fromName(regionName))); try { if(!(s3client.doesBucketExist(bucketName))) { // Note that CreateBucketRequest does not specify region. So bucket is // created in the region specified in the client. s3client.createBucket(new CreateBucketRequest( bucketName)); } if (isOutputBucket){ String policyText = "{" + "\"Id\": \"1\"," + "\"Version\": \"2012-10-17\"," + "\"Statement\": [" + " {" + " \"Sid\": \"1\"," + " \"Action\": [" + " \"s3:GetObject\"" + " ]," + " \"Effect\": \"Allow\"," + " \"Resource\": \"arn:aws:s3:::"+bucketName+"/*\"," + " \"Principal\": \"*\"" + " }" + "]" + "}"; SetBucketPolicyRequest setBucketPolicyRequest = new SetBucketPolicyRequest(bucketName, policyText); s3client.setBucketPolicy(setBucketPolicyRequest); } } catch (AmazonServiceException ase) { logger.error("Caught an AmazonServiceException, which " + "means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason.", ase); throw new TranscodeException(ase.getMessage()); } catch (AmazonClientException ace) { logger.error("Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network.", ace); throw new TranscodeException(ace.getMessage()); } catch (IllegalArgumentException ie) { logger.error("Couldn't get the region", ie); throw new TranscodeException(ie.getMessage()); } }
public String FindOrCreateWeatherPipeJobDirectory() { String bucketLocation = null; try { if(!(s3client.doesBucketExist(jobBucketName))) { // Note that CreateBucketRequest does not specify region. So bucket is // created in the region specified in the client. s3client.createBucket(new CreateBucketRequest( jobBucketName)); } else { s3client.headBucket(new HeadBucketRequest(jobBucketName)); } bucketLocation = "s3n://" + jobBucketName + "/"; } catch (AmazonServiceException ase) { if(ase.getStatusCode() == 403) { System.out.println("You do not have propper permissions to access " + jobBucketName + ". S3 uses a global name space, please make sure you are using a unique bucket name."); System.exit(1); } else { 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()); } System.exit(1); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); System.exit(1); } return bucketLocation; }
@Override public Bucket createBucket(CreateBucketRequest createBucketRequest) throws AmazonClientException { return null; }
String makeBucket(String access_key, String secret_key, String bucket, String endpoint, String region) { String message = null; AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key); AmazonS3 s3Client = new AmazonS3Client(credentials, new ClientConfiguration()); if (endpoint.contains("amazonaws.com")) { s3Client.setEndpoint(endpoint); if (region.length() > 5) { if (region.contains("us-east-1")) { s3Client.setEndpoint("https://s3.amazonaws.com"); } else if (region.contains("us-west")) { s3Client.setEndpoint("https://s3-" + region + ".amazonaws.com"); } else if (region.contains("eu-west")) { s3Client.setEndpoint("https://s3-" + region + ".amazonaws.com"); } else if (region.contains("ap-")) { s3Client.setEndpoint("https://s3-" + region + ".amazonaws.com"); } else if (region.contains("sa-east-1")) { s3Client.setEndpoint("https://s3-" + region + ".amazonaws.com"); } else { s3Client.setEndpoint("https://s3." + region + ".amazonaws.com"); } } } else { s3Client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build()); s3Client.setEndpoint(endpoint); } message = ("\nAttempting to create the bucket. Please view the Bucket list window for an update."); try { if (!(s3Client.doesBucketExist(bucket))) { s3Client.createBucket(new CreateBucketRequest(bucket)); } String bucketLocation = s3Client.getBucketLocation(new GetBucketLocationRequest(bucket)); } catch (AmazonServiceException ase) { if (ase.getMessage().contains("InvalidLocationConstraint")) { s3Client.createBucket(new CreateBucketRequest(bucket, region)); } else { if (NewJFrame.gui) { mainFrame.jTextArea1.append("\n\nError Message: " + ase.getMessage()); mainFrame.jTextArea1.append("\nHTTP Status Code: " + ase.getStatusCode()); mainFrame.jTextArea1.append("\nAWS Error Code: " + ase.getErrorCode()); mainFrame.jTextArea1.append("\nError Type: " + ase.getErrorType()); mainFrame.jTextArea1.append("\nRequest ID: " + ase.getRequestId()); calibrate(); } else { System.out.print("\n\nError Message: " + ase.getMessage()); System.out.print("\nHTTP Status Code: " + ase.getStatusCode()); System.out.print("\nAWS Error Code: " + ase.getErrorCode()); System.out.print("\nError Type: " + ase.getErrorType()); System.out.print("\nRequest ID: " + ase.getRequestId()); } } } return message; }
@Override public Bucket createBucket(CreateBucketRequest createBucketRequest) throws AmazonClientException, AmazonServiceException { // TODO Auto-generated method stub return null; }
/** * create bucket * @param region * @param tableName * @throws ConfigException */ public static void createBucket(String region, String tableName) throws ConfigException { AmazonS3Client client = getClient(region); client.createBucket(new CreateBucketRequest(tableName)); }