Java Code Examples for com.amazonaws.services.s3.AmazonS3#listBuckets()

The following examples show how to use com.amazonaws.services.s3.AmazonS3#listBuckets() . 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: LocalstackContainerTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@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 2
Source File: S3SiteListResolver.java    From engine with GNU General Public License v3.0 6 votes vote down vote up
protected Collection<String> getSiteListFromBucketNames(String bucketNameRegex) {
    List<String> siteNames = new ArrayList<>();
    AmazonS3 client = clientBuilder.getClient();

    List<Bucket> buckets = client.listBuckets();
    if (CollectionUtils.isNotEmpty(buckets)) {
        for (Bucket bucket : buckets) {
            Matcher bucketNameMatcher = Pattern.compile(bucketNameRegex).matcher(bucket.getName());
            if (bucketNameMatcher.matches()) {
                siteNames.add(bucketNameMatcher.group(1));
            }
        }
    }

    return siteNames;
}
 
Example 3
Source File: SpringLocalstackDockerRunnerTest.java    From spring-localstack with Apache License 2.0 5 votes vote down vote up
@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 4
Source File: CreateBucket.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static Bucket getBucket(String bucket_name) {
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
    Bucket named_bucket = null;
    List<Bucket> buckets = s3.listBuckets();
    for (Bucket b : buckets) {
        if (b.getName().equals(bucket_name)) {
            named_bucket = b;
        }
    }
    return named_bucket;
}
 
Example 5
Source File: ListBuckets.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
    List<Bucket> buckets = s3.listBuckets();
    System.out.println("Your Amazon S3 buckets are:");
    for (Bucket b : buckets) {
        System.out.println("* " + b.getName());
    }
}
 
Example 6
Source File: ListGcsBuckets.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void listGcsBuckets(String googleAccessKeyId, String googleAccessKeySecret) {

    // String googleAccessKeyId = "your-google-access-key-id";
    // String googleAccessKeySecret = "your-google-access-key-secret";

    // Create a BasicAWSCredentials using Cloud Storage HMAC credentials.
    BasicAWSCredentials googleCreds =
        new BasicAWSCredentials(googleAccessKeyId, googleAccessKeySecret);

    // Create a new client and do the following:
    // 1. Change the endpoint URL to use the Google Cloud Storage XML API endpoint.
    // 2. Use Cloud Storage HMAC Credentials.
    AmazonS3 interopClient =
        AmazonS3ClientBuilder.standard()
            .withEndpointConfiguration(
                new AwsClientBuilder.EndpointConfiguration(
                    "https://storage.googleapis.com", "auto"))
            .withCredentials(new AWSStaticCredentialsProvider(googleCreds))
            .build();

    // Call GCS to list current buckets
    List<Bucket> buckets = interopClient.listBuckets();

    // Print bucket names
    System.out.println("Buckets:");
    for (Bucket bucket : buckets) {
      System.out.println(bucket.getName());
    }

    // Explicitly clean up client resources.
    interopClient.shutdown();
  }
 
Example 7
Source File: ListBuckets.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public cfData execute( cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException{

AmazonKey amazonKey	= getAmazonKey(_session, argStruct);
AmazonS3 s3Client		= getAmazonS3(amazonKey);

	try {
		//Create the results
		cfQueryResultData qD = new cfQueryResultData( new String[]{"bucket","createdate"}, null );
		qD.setQuerySource( "AmazonS3." + amazonKey.getDataSource() );

		
		List<Bucket> bucketList = s3Client.listBuckets();
		
		for ( Iterator<Bucket> it = bucketList.iterator(); it.hasNext(); ){
			Bucket bucket = it.next();

			qD.addRow(1);
			qD.setCurrentRow( qD.getSize() );
			qD.setCell( 1, new cfStringData( bucket.getName() ) );
			qD.setCell( 2, new cfDateData( bucket.getCreationDate() ) );
		}

	return qD;
} catch (Exception e) {
	throwException(_session, "AmazonS3: " + e.getMessage() );
	return cfBooleanData.FALSE;
}
}
 
Example 8
Source File: TypeaheadS3BucketRequest.java    From h2o-2 with Apache License 2.0 5 votes vote down vote up
@Override
protected JsonArray serve(String filter, int limit) {
  JsonArray array = new JsonArray();
  try {
    AmazonS3 s3 = PersistS3.getClient();
    filter = Strings.nullToEmpty(filter);
    for( Bucket b : s3.listBuckets() ) {
      if( b.getName().startsWith(filter) )
        array.add(new JsonPrimitive(b.getName()));
      if( array.size() == limit) break;
    }
  } catch( IllegalArgumentException xe ) { }
  return array;
}
 
Example 9
Source File: TypeaheadFileRequest.java    From h2o-2 with Apache License 2.0 5 votes vote down vote up
protected JsonArray serveS3(String filter, int limit){
  JsonArray array = new JsonArray();
  try {
    AmazonS3 s3 = PersistS3.getClient();
    filter = Strings.nullToEmpty(filter);
    for( Bucket b : s3.listBuckets() ) {
      if( b.getName().startsWith(filter) )
        array.add(new JsonPrimitive(b.getName()));
      if( array.size() == limit) break;
    }
  } catch( IllegalArgumentException xe ) { }
  return array;
}
 
Example 10
Source File: S3Samples.java    From aws-sdk-java-samples with Apache License 2.0 4 votes vote down vote up
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 11
Source File: BucketClass.java    From cloudExplorer with GNU General Public License v3.0 4 votes vote down vote up
String listBuckets(String access_key, String secret_key, String endpoint) {

        AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key);
        AmazonS3 s3Client = new AmazonS3Client(credentials,
                new ClientConfiguration());
        s3Client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build());
        s3Client.setEndpoint(endpoint);
        String[] array = new String[10];

        String bucketlist = null;

        int i = 0;
        try {

            for (Bucket bucket : s3Client.listBuckets()) {
                bucketlist = bucketlist + " " + bucket.getName();
            }

        } catch (AmazonServiceException ase) {
            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());
            }

        } catch (Exception lsbuckets) {

            if (lsbuckets.getMessage().contains("peer not authenticated") || lsbuckets.getMessage().contains("hostname in certificate didn't match")) {
                if (NewJFrame.gui) {
                    mainFrame.jTextArea1.append("\nError: This program does not support non-trusted SSL certificates\n\nor your SSL certificates are incorrect.");
                } else {
                    System.out.print("\n\nError: This program does not support non-trusted SSL certificates\n\nor your SSL certificates are not correctly installed.");
                }
            }
        }
        String parse = null;

        if (bucketlist != null) {
            parse = bucketlist.replace("null", "");

        } else {
            parse = "no_bucket_found";
        }

        return parse;
    }