Example usage for com.amazonaws.services.s3 AmazonS3 createBucket

List of usage examples for com.amazonaws.services.s3 AmazonS3 createBucket

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3 createBucket.

Prototype

@Deprecated
public Bucket createBucket(String bucketName, String region) throws SdkClientException, AmazonServiceException;

Source Link

Document

Creates a new Amazon S3 bucket with the specified name in the specified Amazon S3 region.

Usage

From source file:com.openkm.util.backup.RepositoryS3Backup.java

License:Open Source License

/**
 * Performs a recursive repository content export with metadata
 *//*from www.j a  v a 2s. c  o m*/
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;
}

From source file:datameer.awstasks.ant.s3.model.CreateBucketCommand.java

License:Apache License

@Override
public void execute(Project project, AmazonS3 s3Service) {
    String name = getNormalizedName();
    boolean doesBucketExist = s3Service.doesBucketExist(name);

    if (isEmptyIfExistent() && doesBucketExist) {
        List<S3ObjectSummary> s3Objects = s3Service.listObjects(name).getObjectSummaries();
        for (S3ObjectSummary s3Object : s3Objects) {
            s3Service.deleteObject(name, s3Object.getKey());
        }/*w w w  .j av  a2  s.  c  o m*/
        doesBucketExist = false;
    }
    if (!doesBucketExist) {
        try {
            s3Service.createBucket(name, _location);
            System.out.println("created bucket '" + name + "'");
        } catch (Exception e) {
            throw new RuntimeException("failed to create bucket '" + name + "'", e);
        }
    }
}

From source file:mail.server.storage.AWSStorageCreation.java

License:GNU General Public License

public Map<String, String> create(String email, String region) throws Exception {
    log.debug("I will now figure out what region to put things in", region);
    Region awsRegion = Region.valueOf(region);
    String awsRegionString = awsRegion.toString();
    if (awsRegionString == null)
        awsRegionString = "";

    String awsRegionStringEndPoint = awsRegionString.isEmpty() ? "s3.amazonaws.com"
            : ("s3-" + awsRegionString + ".amazonaws.com");

    log.debug("I will now log in to S3 and the IdentityManagement to check these credentials.");

    SimpleAWSCredentials credentials = new SimpleAWSCredentials(awsAccessKeyId, awsSecretKey);
    AmazonS3 s3 = new AmazonS3Client(credentials);
    AmazonIdentityManagement im = new AmazonIdentityManagementClient(credentials);

    log.debug("Successfully logged into S3");

    log.debug("I will now derive names for items");
    deriveNames(generateBucketName(email));

    log.debug("I will now try to:\n" + "  1. Create the S3 Bucket with name ", bucketName,
            "\n" + "  2. Create two IAM Identities for permissions -\n" + "       ", writeIdentity,
            " to be sent to the mail server to be able to write to the mailbox.\n" + "       ", writeIdentity,
            " to be stored in your configuration to enable the mail client to read and write mail.\n\n");

    s3.setEndpoint(awsRegionStringEndPoint);
    s3.createBucket(bucketName, awsRegion);

    log.debug("Setting website configuration");

    BucketWebsiteConfiguration bwc = new BucketWebsiteConfiguration("index.html");
    s3.setBucketWebsiteConfiguration(bucketName, bwc);
    log.debug("Done");

    log.debug("Enabling CORS");
    CORSRule rule1 = new CORSRule().withId("CORSRule1")
            .withAllowedMethods(Arrays.asList(new CORSRule.AllowedMethods[] { CORSRule.AllowedMethods.GET,
                    CORSRule.AllowedMethods.PUT, CORSRule.AllowedMethods.DELETE }))
            .withAllowedOrigins(Arrays.asList(new String[] { "*" })).withMaxAgeSeconds(3000)
            .withAllowedHeaders(Arrays.asList(new String[] { "*" }))
            .withExposedHeaders(Arrays.asList(new String[] { "ETag" }));

    BucketCrossOriginConfiguration cors = new BucketCrossOriginConfiguration();
    cors.setRules(Arrays.asList(new CORSRule[] { rule1 }));

    s3.setBucketCrossOriginConfiguration(bucketName, cors);
    log.debug("Done");

    log.format("Creating group %s ... ", groupName);
    im.createGroup(new CreateGroupRequest().withGroupName(groupName));
    log.debug("Done");

    log.format("Creating user %s ... ", writeIdentity);
    im.createUser(new CreateUserRequest().withUserName(writeIdentity));
    log.debug("Done");

    log.format("Adding user %s to group %s ... ", writeIdentity, groupName);
    im.addUserToGroup(new AddUserToGroupRequest().withGroupName(groupName).withUserName(writeIdentity));
    log.debug("Done");

    log.format("Creating user %s ... ", readWriteIdentity);
    im.createUser(new CreateUserRequest().withUserName(readWriteIdentity));
    log.debug("Done");

    log.format("Adding user %s to group %s ... ", readWriteIdentity, groupName);
    im.addUserToGroup(new AddUserToGroupRequest().withGroupName(groupName).withUserName(readWriteIdentity));
    log.debug("Done");

    log.format("Creating permissions for %s to write to bucket %s ... \n", writeIdentity, bucketName);

    String writePolicyRaw = "{                        \n" + "  #Statement#: [            \n"
            + "    {                     \n" + "      #Sid#: #SID#,         \n"
            + "      #Action#: [            \n" + "        #s3:PutObject#,      \n"
            + "        #s3:PutObjectAcl#      \n" + "      ],                  \n"
            + "      #Effect#: #Allow#,      \n" + "      #Resource#: [         \n"
            + "        #arn:aws:s3:::BUCKET/*#\n" + "      ]                  \n"
            + "    }                     \n" + "  ]                     \n" + "}\n";

    String writePolicy = writePolicyRaw.replaceAll("#", "\"").replace("SID", policyWriteName).replace("BUCKET",
            bucketName);// w  w  w  . ja  v a  2 s. c  o m
    //      q.println ("Policy definition: " + writePolicy);
    im.putUserPolicy(new PutUserPolicyRequest().withUserName(writeIdentity).withPolicyDocument(writePolicy)
            .withPolicyName(policyWriteName));
    log.debug("Done");

    log.format("Creating permissions for %s to read/write to bucket %s ... \n", writeIdentity, bucketName);

    String readWritePolicyRaw = "{                        \n" + "  #Statement#: [            \n"
            + "  {                     \n" + "      #Sid#: #SID#,         \n"
            + "      #Action#: [            \n" + "        #s3:PutObject#,      \n"
            + "        #s3:PutObjectAcl#,      \n" + "        #s3:DeleteObject#,      \n"
            + "        #s3:Get*#,            \n" + "        #s3:List*#            \n"
            + "      ],                  \n" + "      #Effect#: #Allow#,      \n"
            + "      #Resource#: [         \n" + "        #arn:aws:s3:::BUCKET/*#,\n"
            + "        #arn:aws:s3:::BUCKET#   \n" + "      ]                  \n"
            + "    }                     \n" + "  ]                     \n" + "}\n";

    String readWritePolicy = readWritePolicyRaw.replaceAll("#", "\"").replace("SID", policyReadWriteName)
            .replace("BUCKET", bucketName);
    //      q.println ("Policy definition: " + readPolicy);
    im.putUserPolicy(new PutUserPolicyRequest().withUserName(readWriteIdentity)
            .withPolicyDocument(readWritePolicy).withPolicyName(policyReadWriteName));
    log.debug("Done");

    log.format("Requesting access key for %s", writeIdentity);
    writeAccessKey = im.createAccessKey(new CreateAccessKeyRequest().withUserName(writeIdentity))
            .getAccessKey();
    log.format("Received [%s] [%s] Done.\n", writeAccessKey.getAccessKeyId(),
            writeAccessKey.getSecretAccessKey());

    log.format("Requesting access key for %s", readWriteIdentity);
    readWriteAccessKey = im.createAccessKey(new CreateAccessKeyRequest().withUserName(readWriteIdentity))
            .getAccessKey();
    log.format("Received [%s] [%s] Done.\n", readWriteAccessKey.getAccessKeyId(),
            readWriteAccessKey.getSecretAccessKey());

    log.debug();
    log.debug("I have finished the creating the S3 items.\n");

    return Maps.toMap("bucketName", bucketName, "bucketRegion", awsRegionString, "writeAccessKey",
            writeAccessKey.getAccessKeyId(), "writeSecretKey", writeAccessKey.getSecretAccessKey(),
            "readWriteAccessKey", readWriteAccessKey.getAccessKeyId(), "readWriteSecretKey",
            readWriteAccessKey.getSecretAccessKey());
}

From source file:org.alanwilliamson.amazon.s3.CreateBucket.java

License:Open Source License

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 {//www . ja v a 2  s  .  c om
        s3Client.createBucket(bucket.toLowerCase(), amazonKey.getAmazonRegion());
    } catch (Exception e) {
        throwException(_session, "AmazonS3: " + e.getMessage());
    }

    return cfBooleanData.TRUE;
}

From source file:org.elasticsearch.cloud.aws.blobstore.S3BlobStore.java

License:Apache License

public S3BlobStore(Settings settings, AmazonS3 client, String bucket, @Nullable String region,
        ThreadPool threadPool, boolean serverSideEncryption) {
    super(settings);
    this.client = client;
    this.bucket = bucket;
    this.region = region;
    this.threadPool = threadPool;
    this.serverSideEncryption = serverSideEncryption;

    this.bufferSizeInBytes = (int) settings
            .getAsBytesSize("buffer_size", new ByteSizeValue(100, ByteSizeUnit.KB)).bytes();

    if (!client.doesBucketExist(bucket)) {
        if (region != null) {
            client.createBucket(bucket, region);
        } else {/*ww  w .  j a  v a 2  s . c o  m*/
            client.createBucket(bucket);
        }
    }
}