Search in sources :

Example 1 with XmlWriter

use of com.amazonaws.services.s3.internal.XmlWriter in project aws-sdk-android by aws-amplify.

the class AmazonS3Client method createBucket.

/*
     * (non-Javadoc)
     * @see
     * com.amazonaws.services.s3.AmazonS3#createBucket(com.amazonaws.services
     * .s3.model.CreateBucketRequest)
     */
@Override
public Bucket createBucket(CreateBucketRequest 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);
    final Request<CreateBucketRequest> 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());
    }
    /*
         * 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 (final 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 && !StringUtils.upperCase(region).equals(Region.US_Standard.toString())) {
        final XmlWriter xml = new XmlWriter();
        xml.start("CreateBucketConfiguration", "xmlns", Constants.XML_NAMESPACE);
        xml.start("LocationConstraint").value(region).end();
        xml.end();
        final byte[] bytes = xml.getBytes();
        request.addHeader(Headers.CONTENT_LENGTH, String.valueOf(bytes.length));
        request.setContent(new ByteArrayInputStream(bytes));
    }
    invoke(request, voidResponseHandler, bucketName, null);
    return new Bucket(bucketName);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XmlWriter(com.amazonaws.services.s3.internal.XmlWriter)

Example 2 with XmlWriter

use of com.amazonaws.services.s3.internal.XmlWriter in project aws-sdk-android by aws-amplify.

the class AclXmlFactory method convertToXmlByteArray.

/**
 * Converts the specified AccessControlList object to an XML fragment that
 * can be sent to Amazon S3.
 *
 * @param acl The AccessControlList to convert to XML.
 * @return an XML representation of the Access Control List object, suitable
 *         to send in a request to Amazon S3.
 */
public byte[] convertToXmlByteArray(AccessControlList acl) throws AmazonClientException {
    Owner owner = acl.getOwner();
    if (owner == null) {
        throw new AmazonClientException("Invalid AccessControlList: missing an S3Owner");
    }
    XmlWriter xml = new XmlWriter();
    xml.start("AccessControlPolicy", "xmlns", Constants.XML_NAMESPACE);
    xml.start("Owner");
    if (owner.getId() != null) {
        xml.start("ID").value(owner.getId()).end();
    }
    if (owner.getDisplayName() != null) {
        xml.start("DisplayName").value(owner.getDisplayName()).end();
    }
    xml.end();
    xml.start("AccessControlList");
    for (Grant grant : acl.getGrants()) {
        xml.start("Grant");
        convertToXml(grant.getGrantee(), xml);
        xml.start("Permission").value(grant.getPermission().toString()).end();
        xml.end();
    }
    xml.end();
    xml.end();
    return xml.getBytes();
}
Also used : Grant(com.amazonaws.services.s3.model.Grant) Owner(com.amazonaws.services.s3.model.Owner) AmazonClientException(com.amazonaws.AmazonClientException) XmlWriter(com.amazonaws.services.s3.internal.XmlWriter)

Example 3 with XmlWriter

use of com.amazonaws.services.s3.internal.XmlWriter in project aws-sdk-android by aws-amplify.

the class BucketConfigurationXmlFactory method addNoncurrentTransitions.

private void addNoncurrentTransitions(XmlWriter xml, List<NoncurrentVersionTransition> transitions) {
    if (transitions == null || transitions.isEmpty()) {
        return;
    }
    for (final NoncurrentVersionTransition t : transitions) {
        if (t != null) {
            xml.start("NoncurrentVersionTransition");
            if (t.getDays() != -1) {
                xml.start("NoncurrentDays");
                xml.value(Integer.toString(t.getDays()));
                xml.end();
            }
            xml.start("StorageClass");
            xml.value(t.getStorageClass().toString());
            // </StorageClass>
            xml.end();
            // </NoncurrentVersionTransition>
            xml.end();
        }
    }
}
Also used : NoncurrentVersionTransition(com.amazonaws.services.s3.model.BucketLifecycleConfiguration.NoncurrentVersionTransition)

Example 4 with XmlWriter

use of com.amazonaws.services.s3.internal.XmlWriter in project aws-sdk-android by aws-amplify.

the class BucketConfigurationXmlFactory method convertToXmlByteArray.

/**
 * Converts the specified {@link BucketCrossOriginConfiguration} object to
 * an XML fragment that can be sent to Amazon S3.
 *
 * @param config The {@link BucketCrossOriginConfiguration}
 */
/*
     * <CORSConfiguration> <CORSRule>
     * <AllowedOrigin>http://www.foobar.com</AllowedOrigin>
     * <AllowedMethod>GET</AllowedMethod> <MaxAgeSeconds>3000</MaxAgeSec>
     * <ExposeHeader>x-amz-server-side-encryption</ExposeHeader> </CORSRule>
     * </CORSConfiguration>
     */
public byte[] convertToXmlByteArray(BucketCrossOriginConfiguration config) throws AmazonClientException {
    final XmlWriter xml = new XmlWriter();
    xml.start("CORSConfiguration", "xmlns", Constants.XML_NAMESPACE);
    for (final CORSRule rule : config.getRules()) {
        writeRule(xml, rule);
    }
    xml.end();
    return xml.getBytes();
}
Also used : CORSRule(com.amazonaws.services.s3.model.CORSRule) XmlWriter(com.amazonaws.services.s3.internal.XmlWriter)

Example 5 with XmlWriter

use of com.amazonaws.services.s3.internal.XmlWriter in project aws-sdk-android by aws-amplify.

the class BucketConfigurationXmlFactory method addTransitions.

private void addTransitions(XmlWriter xml, List<Transition> transitions) {
    if (transitions == null || transitions.isEmpty()) {
        return;
    }
    for (final Transition t : transitions) {
        if (t != null) {
            xml.start("Transition");
            if (t.getDate() != null) {
                xml.start("Date");
                xml.value(ServiceUtils.formatIso8601Date(t.getDate()));
                xml.end();
            }
            if (t.getDays() != -1) {
                xml.start("Days");
                xml.value(Integer.toString(t.getDays()));
                xml.end();
            }
            xml.start("StorageClass");
            xml.value(t.getStorageClass().toString());
            // <StorageClass>
            xml.end();
            // </Transition>
            xml.end();
        }
    }
}
Also used : Transition(com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Transition) NoncurrentVersionTransition(com.amazonaws.services.s3.model.BucketLifecycleConfiguration.NoncurrentVersionTransition)

Aggregations

XmlWriter (com.amazonaws.services.s3.internal.XmlWriter)19 RedirectRule (com.amazonaws.services.s3.model.RedirectRule)3 NoncurrentVersionTransition (com.amazonaws.services.s3.model.BucketLifecycleConfiguration.NoncurrentVersionTransition)2 CORSRule (com.amazonaws.services.s3.model.CORSRule)2 FilterRule (com.amazonaws.services.s3.model.FilterRule)2 ReplicationRule (com.amazonaws.services.s3.model.ReplicationRule)2 RoutingRule (com.amazonaws.services.s3.model.RoutingRule)2 Map (java.util.Map)2 AmazonClientException (com.amazonaws.AmazonClientException)1 Rule (com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule)1 Transition (com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Transition)1 BucketNotificationConfiguration (com.amazonaws.services.s3.model.BucketNotificationConfiguration)1 TopicConfiguration (com.amazonaws.services.s3.model.BucketNotificationConfiguration.TopicConfiguration)1 AllowedMethods (com.amazonaws.services.s3.model.CORSRule.AllowedMethods)1 CloudFunctionConfiguration (com.amazonaws.services.s3.model.CloudFunctionConfiguration)1 KeyVersion (com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion)1 Filter (com.amazonaws.services.s3.model.Filter)1 Grant (com.amazonaws.services.s3.model.Grant)1 LambdaConfiguration (com.amazonaws.services.s3.model.LambdaConfiguration)1 NotificationConfiguration (com.amazonaws.services.s3.model.NotificationConfiguration)1