public static void getWebsiteConfig(String bucket_name) { final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); try { BucketWebsiteConfiguration config = s3.getBucketWebsiteConfiguration(bucket_name); if (config == null) { System.out.println("No website configuration found!"); } else { System.out.format("Index document: %s\n", config.getIndexDocumentSuffix()); System.out.format("Error document: %s\n", config.getErrorDocument()); } } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.out.println("Failed to get website configuration!"); System.exit(1); } }
void setBUCKETwebsite(String object, String access_key, String secret_key, String endpoint, String bucket) { try { AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key); AmazonS3 s3Client = new AmazonS3Client(credentials, new ClientConfiguration()); if (endpoint.contains("amazonaws.com")) { String aws_endpoint = s3Client.getBucketLocation(new GetBucketLocationRequest(bucket)); if (aws_endpoint.contains("US")) { s3Client.setEndpoint("https://s3.amazonaws.com"); } else if (aws_endpoint.contains("us-west")) { s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com"); } else if (aws_endpoint.contains("eu-west")) { s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com"); } else if (aws_endpoint.contains("ap-")) { s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com"); } else if (aws_endpoint.contains("sa-east-1")) { s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com"); } else { s3Client.setEndpoint("https://s3." + aws_endpoint + ".amazonaws.com"); } } else { s3Client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build()); s3Client.setEndpoint(endpoint); } BucketWebsiteConfiguration bucketWebsiteConfiguration = s3Client.getBucketWebsiteConfiguration(bucket); s3Client.setBucketAcl(bucket, CannedAccessControlList.PublicRead); s3Client.setBucketWebsiteConfiguration(bucket, new BucketWebsiteConfiguration("index.html", "error.html")); } catch (Exception setACLpublic) { mainFrame.jTextArea1.append("\nException occurred in ACL"); } }
@Override public BucketWebsiteConfiguration getBucketWebsiteConfiguration( GetBucketWebsiteConfigurationRequest getBucketWebsiteConfigurationRequest) throws AmazonClientException, AmazonServiceException { // TODO Auto-generated method stub return null; }
@Override public BucketWebsiteConfiguration getBucketWebsiteConfiguration(String bucketName) throws AmazonClientException, AmazonServiceException { return delegate.getBucketWebsiteConfiguration(bucketName); }
@Override public BucketWebsiteConfiguration getBucketWebsiteConfiguration(GetBucketWebsiteConfigurationRequest getBucketWebsiteConfigurationRequest) throws AmazonClientException, AmazonServiceException { return delegate.getBucketWebsiteConfiguration(getBucketWebsiteConfigurationRequest); }
@Override public void setBucketWebsiteConfiguration(String bucketName, BucketWebsiteConfiguration configuration) throws AmazonClientException, AmazonServiceException { delegate.setBucketWebsiteConfiguration(bucketName, configuration); }
/** * Converts the specified website configuration into an XML byte array to * send to S3. * * Sample XML: * <WebsiteConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> * <IndexDocument> * <Suffix>index.html</Suffix> * </IndexDocument> * <ErrorDocument> * <Key>404.html</Key> * </ErrorDocument> * </WebsiteConfiguration> * * @param websiteConfiguration * The configuration to convert. * @return The XML byte array representation. */ public byte[] convertToXmlByteArray(BucketWebsiteConfiguration websiteConfiguration) { XmlWriter xml = new XmlWriter(); xml.start("WebsiteConfiguration", "xmlns", Constants.XML_NAMESPACE); if (websiteConfiguration.getIndexDocumentSuffix() != null) { XmlWriter indexDocumentElement = xml.start("IndexDocument"); indexDocumentElement.start("Suffix").value(websiteConfiguration.getIndexDocumentSuffix()).end(); indexDocumentElement.end(); } if (websiteConfiguration.getErrorDocument() != null) { XmlWriter errorDocumentElement = xml.start("ErrorDocument"); errorDocumentElement.start("Key").value(websiteConfiguration.getErrorDocument()).end(); errorDocumentElement.end(); } RedirectRule redirectAllRequestsTo = websiteConfiguration.getRedirectAllRequestsTo(); if (redirectAllRequestsTo != null) { XmlWriter redirectAllRequestsElement = xml.start("RedirectAllRequestsTo"); if (redirectAllRequestsTo.getprotocol() != null) { xml.start("Protocol").value(redirectAllRequestsTo.getprotocol()).end(); } if (redirectAllRequestsTo.getHostName() != null) { xml.start("HostName").value(redirectAllRequestsTo.getHostName()).end(); } if (redirectAllRequestsTo.getReplaceKeyPrefixWith() != null) { xml.start("ReplaceKeyPrefixWith").value(redirectAllRequestsTo.getReplaceKeyPrefixWith()).end(); } if (redirectAllRequestsTo.getReplaceKeyWith() != null) { xml.start("ReplaceKeyWith").value(redirectAllRequestsTo.getReplaceKeyWith()).end(); } redirectAllRequestsElement.end(); } if (websiteConfiguration.getRoutingRules() != null && websiteConfiguration.getRoutingRules().size() > 0) { XmlWriter routingRules = xml.start("RoutingRules"); for (RoutingRule rule : websiteConfiguration.getRoutingRules()) { writeRule(routingRules, rule); } routingRules.end(); } xml.end(); return xml.getBytes(); }
@Override public BucketWebsiteConfiguration getBucketWebsiteConfiguration(String bucketName) throws SdkClientException, AmazonServiceException { return call(() -> getDelegate().getBucketWebsiteConfiguration(bucketName)); }
@Override public BucketWebsiteConfiguration getBucketWebsiteConfiguration(GetBucketWebsiteConfigurationRequest getBucketWebsiteConfigurationRequest) throws SdkClientException, AmazonServiceException { return call(() -> getDelegate().getBucketWebsiteConfiguration(getBucketWebsiteConfigurationRequest)); }
@Override public void setBucketWebsiteConfiguration(String bucketName, BucketWebsiteConfiguration configuration) throws SdkClientException, AmazonServiceException { run(() -> getDelegate().setBucketWebsiteConfiguration(bucketName, configuration)); }
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 BucketWebsiteConfiguration getBucketWebsiteConfiguration(String bucketName) throws AmazonClientException { return null; }
@Override public BucketWebsiteConfiguration getBucketWebsiteConfiguration(GetBucketWebsiteConfigurationRequest getBucketWebsiteConfigurationRequest) throws AmazonClientException { return null; }
@Override public void setBucketWebsiteConfiguration(String bucketName, BucketWebsiteConfiguration configuration) throws AmazonClientException { }
@Override public BucketWebsiteConfiguration getBucketWebsiteConfiguration(String bucketName) throws AmazonClientException, AmazonServiceException { // TODO Auto-generated method stub return null; }
@Override public void setBucketWebsiteConfiguration(String bucketName, BucketWebsiteConfiguration configuration) throws AmazonClientException, AmazonServiceException { // TODO Auto-generated method stub }