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

The following examples show how to use com.amazonaws.services.s3.AmazonS3#doesBucketExist() . 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: S3BucketManagementService.java    From wecube-platform with Apache License 2.0 6 votes vote down vote up
@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 2
Source File: S3BucketManagementService.java    From wecube-platform with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteItem(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())) {
            if (!amazonS3.listObjects(item.getName()).getObjectSummaries().isEmpty()) {
                throw new WecubeCoreException(String.format("Can not delete bucket [%s] : Bucket have [%s] amount of objects", item.getName(), amazonS3.listObjects(item.getName()).getObjectSummaries().size()));
            }
            amazonS3.deleteBucket(item.getName());
        } else {
            log.warn("To be delete bucket {%s} does not exists.", item.getName());
        }
    }
}
 
Example 3
Source File: AmazonS3Storage.java    From thunderbit with GNU Affero General Public License v3.0 6 votes vote down vote up
@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: RepositoryS3Backup.java    From document-management-system with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 5
Source File: S3ConfigBean.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private void validateBucket(Stage.Context context, List<Stage.ConfigIssue> issues, AmazonS3 s3Client,
                            String bucket, String groupName, String configName) {
  if(bucket == null || bucket.isEmpty()) {
    issues.add(context.createConfigIssue(groupName, configName, com.streamsets.pipeline.stage.origin.s3.Errors.S3_SPOOLDIR_11));
  } else if (!s3Client.doesBucketExist(bucket)) {
    issues.add(context.createConfigIssue(groupName, configName, com.streamsets.pipeline.stage.origin.s3.Errors.S3_SPOOLDIR_12, bucket));
  }
}
 
Example 6
Source File: TestUtil.java    From datacollector with Apache License 2.0 5 votes vote down vote up
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 7
Source File: ImportS3.java    From h2o-2 with Apache License 2.0 5 votes vote down vote up
@Override
protected String parse(String input) throws IllegalArgumentException {
  AmazonS3 s3 = PersistS3.getClient();
  if( !s3.doesBucketExist(input) )
    throw new IllegalArgumentException("S3 Bucket " + input + " not found!");
  return input;
}
 
Example 8
Source File: BucketClass.java    From cloudExplorer with GNU General Public License v3.0 4 votes vote down vote up
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;
}