Example usage for com.amazonaws.services.s3.internal BucketNameUtils validateBucketName

List of usage examples for com.amazonaws.services.s3.internal BucketNameUtils validateBucketName

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.internal BucketNameUtils validateBucketName.

Prototype

public static void validateBucketName(final String bucketName) 

Source Link

Document

Validates that the specified bucket name is valid for Amazon S3 V2 naming (i.e.

Usage

From source file:com.emc.vipr.services.s3.ViPRS3Client.java

License:Open Source License

/**
 * ViPR-specific create bucket command.  This version of the command adds some
 * options specific to EMC ViPR, specifically the ability to set the ViPR project ID
 * and Object Virtual Pool ID on the new bucket.
 * @param createBucketRequest the configuration parameters for the new bucket.
 *///from   w  w  w  .  j a va  2s.  c  o  m
public Bucket createBucket(ViPRCreateBucketRequest createBucketRequest)
        throws AmazonClientException, AmazonServiceException {
    assertParameterNotNull(createBucketRequest,
            "The CreateBucketRequest parameter must be specified when creating a bucket");

    String bucketName = createBucketRequest.getBucketName();
    String region = createBucketRequest.getRegion();
    assertParameterNotNull(bucketName, "The bucket name parameter must be specified when creating a bucket");

    if (bucketName != null)
        bucketName = bucketName.trim();
    BucketNameUtils.validateBucketName(bucketName);

    Request<ViPRCreateBucketRequest> request = createRequest(bucketName, null, createBucketRequest,
            HttpMethodName.PUT);

    if (createBucketRequest.getAccessControlList() != null) {
        addAclHeaders(request, createBucketRequest.getAccessControlList());
    } else if (createBucketRequest.getCannedAcl() != null) {
        request.addHeader(Headers.S3_CANNED_ACL, createBucketRequest.getCannedAcl().toString());
    }

    // ViPR specific: projectId,  vpoolId and fsAccessEnabled.
    if (createBucketRequest.getProjectId() != null) {
        request.addHeader(ViPRConstants.PROJECT_HEADER, createBucketRequest.getProjectId());
    }
    if (createBucketRequest.getVpoolId() != null) {
        request.addHeader(ViPRConstants.VPOOL_HEADER, createBucketRequest.getVpoolId());
    }
    if (createBucketRequest.isFsAccessEnabled()) {
        request.addHeader(ViPRConstants.FS_ACCESS_ENABLED, "true");
    }

    /*
     * If we're talking to a region-specific endpoint other than the US, we
     * *must* specify a location constraint. Try to derive the region from
     * the endpoint.
     */
    if (!(this.endpoint.getHost().equals(Constants.S3_HOSTNAME)) && (region == null || region.isEmpty())) {

        try {
            region = RegionUtils.getRegionByEndpoint(this.endpoint.getHost()).getName();
        } catch (IllegalArgumentException exception) {
            // Endpoint does not correspond to a known region; send the
            // request with no location constraint and hope for the best.
        }

    }

    /*
     * We can only send the CreateBucketConfiguration if we're *not*
     * creating a bucket in the US region.
     */
    if (region != null && !region.toUpperCase().equals(Region.US_Standard.toString())) {
        XmlWriter xml = new XmlWriter();
        xml.start("CreateBucketConfiguration", "xmlns", Constants.XML_NAMESPACE);
        xml.start("LocationConstraint").value(region).end();
        xml.end();

        request.setContent(new ByteArrayInputStream(xml.getBytes()));
    }

    invoke(request, voidResponseHandler, bucketName, null);

    return new Bucket(bucketName);
}