List of usage examples for com.amazonaws.services.s3.internal XmlWriter getBytes
public byte[] getBytes()
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. *//* w w w .j a v a2s . co 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); }
From source file:com.kolich.aws.services.s3.impl.KolichS3Client.java
License:Open Source License
@Override public Option<HttpFailure> createBucket(final String bucketName) { return new AwsS3HttpClosure<Bucket>(client_, SC_OK, bucketName) { @Override//from www . ja v a 2 s. c om public void prepare(final AwsHttpRequest request) throws Exception { // https://github.com/markkolich/kolich-aws/issues/1 // Can only send the CreateBucketConfiguration if we're *not* // creating a bucket in the US region. final String regionId; if ((regionId = region_.getRegionId()) != null) { final XmlWriter xml = new XmlWriter(); xml.start("CreateBucketConfiguration", "xmlns", XML_NAMESPACE); xml.start("LocationConstraint").value(regionId).end(); xml.end(); // Attach the XML entity to the request. final HttpRequestBase base = request.getRequestBase(); ((HttpPut) base).setEntity(new ByteArrayEntity(xml.getBytes())); } } @Override public void validate() throws Exception { checkNotNull(bucketName, "Bucket name cannot be null."); checkState(isValidBucketName(bucketName), "Invalid bucket name, " + "did not match expected bucket name pattern."); } }.putOption(); }