Java Code Examples for com.amazonaws.services.s3.AmazonS3#createBucket()
The following examples show how to use
com.amazonaws.services.s3.AmazonS3#createBucket() .
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: SpringCloudS3LiveTest.java From tutorials with MIT License | 6 votes |
@BeforeClass public static void setupResources() throws IOException { bucketName = UUID.randomUUID().toString(); testFileToDownload = "test-file-download.txt"; testFileToUpload = "test-file-upload.txt"; filesWithSimilarName = new String[] { "foo/hello-apple.txt", "foo/hello-orange.txt", "bar/hello-grapes.txt", }; similarNameFiles = new ArrayList<>(); for (String name : filesWithSimilarName) { similarNameFiles.add(new File(name.substring(0, name.lastIndexOf("/") + 1))); } Files.write(Paths.get(testFileToUpload), "Hello World Uploaded!".getBytes()); AmazonS3 amazonS3 = SpringCloudAwsTestUtil.amazonS3(); amazonS3.createBucket(bucketName); amazonS3.putObject(bucketName, testFileToDownload, "Hello World"); for (String s3Key : filesWithSimilarName) { amazonS3.putObject(bucketName, s3Key, "Hello World"); } }
Example 2
Source File: TestWhiteListedBuckets.java From dremio-oss with Apache License 2.0 | 6 votes |
@Before public void setup() { this.s3Mock = new S3Mock.Builder().withPort(0).withInMemoryBackend().build(); Http.ServerBinding binding = s3Mock.start(); port = binding.localAddress().getPort(); EndpointConfiguration endpoint = new EndpointConfiguration(String.format("http://localhost:%d", port), Region.US_EAST_1.toString()); AmazonS3 client = AmazonS3ClientBuilder .standard() .withPathStyleAccessEnabled(true) .withEndpointConfiguration(endpoint) .withCredentials(new AWSStaticCredentialsProvider(new AnonymousAWSCredentials())) .build(); client.createBucket("bucket-a"); client.createBucket("bucket-b"); client.createBucket("bucket-c"); }
Example 3
Source File: AmazonS3Storage.java From thunderbit with GNU Affero General Public License v3.0 | 6 votes |
@Inject public AmazonS3Storage (Configuration configuration) { bucketName = configuration.getString("storage.s3.bucket", "thunderbit"); String accessKey = configuration.getString("storage.s3.accesskey"); String secretKey = configuration.getString("storage.s3.secretkey"); credentials = new BasicAWSCredentials(accessKey, secretKey); AmazonS3 amazonS3 = new AmazonS3Client(credentials); if (configuration.getBoolean("storage.s3.createBucket", true)) { try { if (!(amazonS3.doesBucketExist(bucketName))) { amazonS3.createBucket(new CreateBucketRequest(bucketName)); } String bucketLocation = amazonS3.getBucketLocation(new GetBucketLocationRequest(bucketName)); logger.info("Amazon S3 bucket created at " + bucketLocation); } catch (AmazonServiceException ase) { logAmazonServiceException (ase); } catch (AmazonClientException ace) { logAmazonClientException(ace); } } }
Example 4
Source File: AmazonS3Config.java From ReCiter with Apache License 2.0 | 6 votes |
private void createBucket(AmazonS3 s3) { String accountNumber = getAccountIDUsingAccessKey(amazonAWSAccessKey, amazonAWSSecretKey); String bucketName = s3BucketName.toLowerCase() + "-" + awsS3Region.toLowerCase() + "-" + accountNumber; if(!isDynamicBucketName) { bucketName = s3BucketName; } if(s3.doesBucketExistV2(bucketName)) { log.info(bucketName.toLowerCase() + " Bucket Name already exists"); } else { try { s3.createBucket(bucketName.toLowerCase()); } catch(AmazonS3Exception e) { log.error(e.getErrorMessage()); } log.info("Bucket created with name: " + bucketName); } }
Example 5
Source File: LocalstackContainerTest.java From testcontainers-java with MIT License | 6 votes |
@Test public void s3TestOverBridgeNetwork() throws IOException { AmazonS3 s3 = AmazonS3ClientBuilder .standard() .withEndpointConfiguration(localstack.getEndpointConfiguration(S3)) .withCredentials(localstack.getDefaultCredentialsProvider()) .build(); final String bucketName = "foo"; s3.createBucket(bucketName); s3.putObject(bucketName, "bar", "baz"); final List<Bucket> buckets = s3.listBuckets(); final Optional<Bucket> maybeBucket = buckets.stream().filter(b -> b.getName().equals(bucketName)).findFirst(); assertTrue("The created bucket is present", maybeBucket.isPresent()); final Bucket bucket = maybeBucket.get(); assertEquals("The created bucket has the right name", bucketName, bucket.getName()); final ObjectListing objectListing = s3.listObjects(bucketName); assertEquals("The created bucket has 1 item in it", 1, objectListing.getObjectSummaries().size()); final S3Object object = s3.getObject(bucketName, "bar"); final String content = IOUtils.toString(object.getObjectContent(), Charset.forName("UTF-8")); assertEquals("The object can be retrieved", "baz", content); }
Example 6
Source File: S3BucketManagementService.java From wecube-platform with Apache License 2.0 | 6 votes |
@Override public ResourceItem createItem(ResourceItem item) { String password = EncryptionUtils.decryptWithAes(item.getResourceServer().getLoginPassword(), resourceProperties.getPasswordEncryptionSeed(), item.getResourceServer().getName()); AmazonS3 amazonS3 = newS3Client( item.getResourceServer().getHost(), item.getResourceServer().getPort(), item.getResourceServer().getLoginUsername(), password); if (amazonS3 != null) { if (amazonS3.doesBucketExist(item.getName())) { throw new WecubeCoreException(String.format("Can not create bucket [%s] : Bucket exists.", item.getName())); } amazonS3.createBucket(item.getName()); } return item; }
Example 7
Source File: S3NotebookRepoTest.java From zeppelin with Apache License 2.0 | 5 votes |
@Before public void setUp() throws IOException { String bucket = "test-bucket"; notebookRepo = new S3NotebookRepo(); ZeppelinConfiguration conf = ZeppelinConfiguration.create(); System.setProperty(ZeppelinConfiguration.ConfVars.ZEPPELIN_NOTEBOOK_S3_ENDPOINT.getVarName(), s3Proxy.getUri().toString()); System.setProperty(ZeppelinConfiguration.ConfVars.ZEPPELIN_NOTEBOOK_S3_BUCKET.getVarName(), bucket); System.setProperty("aws.accessKeyId", s3Proxy.getAccessKey()); System.setProperty("aws.secretKey", s3Proxy.getSecretKey()); notebookRepo.init(conf); // create bucket for notebook AmazonS3 s3Client = AmazonS3ClientBuilder .standard() .withCredentials( new AWSStaticCredentialsProvider( new BasicAWSCredentials(s3Proxy.getAccessKey(), s3Proxy.getSecretKey()))) .withEndpointConfiguration( new AwsClientBuilder.EndpointConfiguration(s3Proxy.getUri().toString(), Regions.US_EAST_1.getName())) .build(); s3Client.createBucket(bucket); }
Example 8
Source File: PrimitiveS3OperationHandler.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 5 votes |
/** * Creates a directory. * Fails if the parent directory does not exist. */ @Override public boolean makeDir(URI target) throws IOException { PooledS3Connection connection = null; try { connection = connect(target); AmazonS3 service = connection.getService(); URI parentUri = URIUtils.getParentURI(target); if (parentUri != null) { Info parentInfo = info(parentUri, connection); if (parentInfo == null) { throw new IOException("Parent dir does not exist"); } } String[] path = getPath(target); String bucketName = path[0]; try { if (path.length == 1) { service.createBucket(bucketName); } else { String dirName = appendSlash(path[1]); S3Utils.createEmptyObject(service, bucketName, dirName); } return true; } catch (AmazonClientException e) { throw S3Utils.getIOException(e); } } finally { disconnect(connection); } }
Example 9
Source File: CreateBucket.java From openbd-core with GNU General Public License v3.0 | 5 votes |
public cfData execute( cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException{ AmazonKey amazonKey = getAmazonKey(_session, argStruct); AmazonS3 s3Client = getAmazonS3(amazonKey); String bucket = getNamedStringParam(argStruct, "bucket", null ); try { s3Client.createBucket(bucket.toLowerCase(), amazonKey.getAmazonRegion() ); } catch (Exception e) { throwException(_session, "AmazonS3: " + e.getMessage() ); } return cfBooleanData.TRUE; }
Example 10
Source File: TestUtil.java From datacollector with Apache License 2.0 | 5 votes |
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)); }
Example 11
Source File: CreateBucket.java From aws-doc-sdk-examples with Apache License 2.0 | 5 votes |
public static Bucket createBucket(String bucket_name) { final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build(); Bucket b = null; if (s3.doesBucketExistV2(bucket_name)) { System.out.format("Bucket %s already exists.\n", bucket_name); b = getBucket(bucket_name); } else { try { b = s3.createBucket(bucket_name); } catch (AmazonS3Exception e) { System.err.println(e.getErrorMessage()); } } return b; }
Example 12
Source File: RepositoryS3Backup.java From document-management-system with GNU General Public License v2.0 | 5 votes |
/** * Performs a recursive repository content export with metadata */ public static ImpExpStats backup(String token, String fldPath, String bucket, boolean metadata, Writer out, InfoDecorator deco) throws PathNotFoundException, AccessDeniedException, RepositoryException, FileNotFoundException, ParseException, NoSuchGroupException, IOException, DatabaseException, GeneralException { log.debug("backup({}, {}, {}, {}, {}, {})", new Object[]{token, fldPath, bucket, metadata, out, deco}); ImpExpStats stats = null; if (running) { throw new GeneralException("Backup in progress"); } else { running = true; try { if (!Config.AMAZON_ACCESS_KEY.equals("") && !Config.AMAZON_SECRET_KEY.equals("")) { AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials(Config.AMAZON_ACCESS_KEY, Config.AMAZON_SECRET_KEY)); if (!s3.doesBucketExist(bucket)) { s3.createBucket(bucket, Region.EU_Ireland); } stats = backupHelper(token, fldPath, s3, bucket, metadata, out, deco); log.info("Backup finished!"); } else { throw new GeneralException("Missing Amazon Web Service keys"); } } finally { running = false; } } log.debug("exportDocuments: {}", stats); return stats; }
Example 13
Source File: S3StorageService.java From kayenta with Apache License 2.0 | 5 votes |
/** Check to see if the bucket exists, creating it if it is not there. */ public void ensureBucketExists(String accountName) { AwsNamedAccountCredentials credentials = accountCredentialsRepository.getRequiredOne(accountName); AmazonS3 amazonS3 = credentials.getAmazonS3(); String bucket = credentials.getBucket(); String region = credentials.getRegion(); HeadBucketRequest request = new HeadBucketRequest(bucket); try { amazonS3.headBucket(request); } catch (AmazonServiceException e) { if (e.getStatusCode() == 404) { if (com.amazonaws.util.StringUtils.isNullOrEmpty(region)) { log.warn("Bucket {} does not exist. Creating it in default region.", bucket); amazonS3.createBucket(bucket); } else { log.warn("Bucket {} does not exist. Creating it in region {}.", bucket, region); amazonS3.createBucket(bucket, region); } } else { log.error("Could not create bucket {}: {}", bucket, e); throw e; } } }
Example 14
Source File: SpringLocalstackDockerRunnerTest.java From spring-localstack with Apache License 2.0 | 5 votes |
@Test public void testS3() throws Exception { AmazonS3 client = amazonDockerClientsHolder.amazonS3(); client.createBucket("test-bucket"); List<Bucket> bucketList = client.listBuckets(); assertThat(bucketList.size(), is(1)); File file = File.createTempFile("localstack", "s3"); file.deleteOnExit(); try (FileOutputStream stream = new FileOutputStream(file)) { String content = "HELLO WORLD!"; stream.write(content.getBytes()); } PutObjectRequest request = new PutObjectRequest("test-bucket", "testData", file); client.putObject(request); ObjectListing listing = client.listObjects("test-bucket"); assertThat(listing.getObjectSummaries().size(), is(1)); S3Object s3Object = client.getObject("test-bucket", "testData"); String resultContent = IOUtils.toString(s3Object.getObjectContent()); assertThat(resultContent, is("HELLO WORLD!")); }
Example 15
Source File: S3.java From rdf-delta with Apache License 2.0 | 4 votes |
public static void createBucket(AmazonS3 client, String bucketName) { Bucket bucket = client.createBucket(bucketName); }
Example 16
Source File: S3Samples.java From aws-sdk-java-samples with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws IOException { //BEGIN_SAMPLE:AmazonS3.CreateClient //TITLE:Create an S3 client //DESCRIPTION:Create your credentials file at ~/.aws/credentials (C:\Users\USER_NAME\.aws\credentials for Windows users) AmazonS3 s3 = AmazonS3ClientBuilder.standard().build(); Region usWest2 = com.amazonaws.regions.Region.getRegion(Regions.US_WEST_2); s3.setRegion(usWest2); //END_SAMPLE 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 { System.out.println("Creating bucket " + bucketName + "\n"); //BEGIN_SAMPLE:AmazonS3.CreateBucket //TITLE:Create an S3 bucket //DESCRIPTION: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. s3.createBucket(bucketName); //END_SAMPLE System.out.println("Listing buckets"); //BEGIN_SAMPLE:AmazonS3.ListBuckets //TITLE:List buckets //DESCRIPTION:List the buckets in your account for (Bucket bucket : s3.listBuckets()) { System.out.println(" - " + bucket.getName()); } System.out.println(); //END_SAMPLE System.out.println("Uploading a new object to S3 from a file\n"); //BEGIN_SAMPLE:AmazonS3.PutObject //TITLE:Upload an object to a bucket //DESCRIPTION:You can easily upload a file to S3, or upload directly an InputStream if you know the length of the data in the stream. s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile())); //END_SAMPLE System.out.println("Downloading an object"); //BEGIN_SAMPLE:AmazonS3.GetObject //TITLE:Download an S3 object. //DESCRIPTION:When you download an object, you get all of the object's metadata and a stream from which to read the contents. S3Object object = s3.getObject(new GetObjectRequest(bucketName, key)); //END_SAMPLE System.out.println("Content-Type: " + object.getObjectMetadata().getContentType()); displayTextInputStream(object.getObjectContent()); System.out.println("Listing objects"); //BEGIN_SAMPLE:AmazonS3.ListObjects //TITLE:List S3 objects in bucket //DESCRIPTION:List objects in your bucket by prefix. 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. 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(); //END_SAMPLE System.out.println("Deleting an object\n"); //BEGIN_SAMPLE:AmazonS3.DeleteObject //TITLE:Delete an S3 object //DESCRIPTION:Unless versioning has been turned on for your bucket, there is no way to undelete an object, so use caution when deleting objects. s3.deleteObject(bucketName, key); //END_SAMPLE System.out.println("Deleting bucket " + bucketName + "\n"); //BEGIN_SAMPLE:AmazonS3.DeleteBucket //TITLE:Delete an S3 bucket //DESCRIPTION: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. s3.deleteBucket(bucketName); //END_SAMPLE } 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()); } }
Example 17
Source File: EndToEndTest.java From ecs-sync with Apache License 2.0 | 4 votes |
@Test public void testS3() throws Exception { Properties syncProperties = com.emc.ecs.sync.test.TestConfig.getProperties(); final String bucket = "ecs-sync-s3-test-bucket"; final String endpoint = syncProperties.getProperty(com.emc.ecs.sync.test.TestConfig.PROP_S3_ENDPOINT); final String accessKey = syncProperties.getProperty(com.emc.ecs.sync.test.TestConfig.PROP_S3_ACCESS_KEY_ID); final String secretKey = syncProperties.getProperty(com.emc.ecs.sync.test.TestConfig.PROP_S3_SECRET_KEY); final String region = syncProperties.getProperty(com.emc.ecs.sync.test.TestConfig.PROP_S3_REGION); Assume.assumeNotNull(endpoint, accessKey, secretKey); URI endpointUri = new URI(endpoint); ClientConfiguration config = new ClientConfiguration().withSignerOverride("S3SignerType"); AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard(); builder.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey))); builder.withClientConfiguration(config); builder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region)); AmazonS3 s3 = builder.build(); try { s3.createBucket(bucket); } catch (AmazonServiceException e) { if (!e.getErrorCode().equals("BucketAlreadyExists")) throw e; } // for testing ACLs String authUsers = "http://acs.amazonaws.com/groups/global/AuthenticatedUsers"; String everyone = "http://acs.amazonaws.com/groups/global/AllUsers"; String[] validGroups = {authUsers, everyone}; String[] validPermissions = {"READ", "WRITE", "FULL_CONTROL"}; AwsS3Config awsS3Config = new AwsS3Config(); if (endpointUri.getScheme() != null) awsS3Config.setProtocol(Protocol.valueOf(endpointUri.getScheme().toLowerCase())); awsS3Config.setHost(endpointUri.getHost()); awsS3Config.setPort(endpointUri.getPort()); awsS3Config.setAccessKey(accessKey); awsS3Config.setSecretKey(secretKey); awsS3Config.setRegion(region); awsS3Config.setLegacySignatures(true); awsS3Config.setDisableVHosts(true); awsS3Config.setBucketName(bucket); awsS3Config.setPreserveDirectories(true); TestConfig testConfig = new TestConfig(); testConfig.setObjectOwner(accessKey); testConfig.setValidGroups(validGroups); testConfig.setValidPermissions(validPermissions); try { multiEndToEndTest(awsS3Config, testConfig, true); } finally { try { s3.deleteBucket(bucket); } catch (Throwable t) { log.warn("could not delete bucket", t); } } }
Example 18
Source File: BucketClass.java From cloudExplorer with GNU General Public License v3.0 | 4 votes |
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; }
Example 19
Source File: CrossRegionReplication.java From aws-doc-sdk-examples with Apache License 2.0 | 3 votes |
private static void createBucket(AmazonS3 s3Client, Regions region, String bucketName) { CreateBucketRequest request = new CreateBucketRequest(bucketName, region.getName()); s3Client.createBucket(request); BucketVersioningConfiguration configuration = new BucketVersioningConfiguration().withStatus(BucketVersioningConfiguration.ENABLED); SetBucketVersioningConfigurationRequest enableVersioningRequest = new SetBucketVersioningConfigurationRequest(bucketName, configuration); s3Client.setBucketVersioningConfiguration(enableVersioningRequest); }