Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * Copyright 2010 Amazon.com, Inc. or its affiliates. All Rights Reserved.
    *
    * Licensed under the Apache License, Version 2.0 (the "License").
    * You may not use this file except in compliance with the License.
    * A copy of the License is located at
    *
    *  http://aws.amazon.com/apache2.0
    *
   * or in the "license" file accompanying this file. This file is distributed
   * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
   * express or implied. See the License for the specific language governing
   * permissions and limitations under the License.
   */
  package com.amazonaws.services.s3;
  
  import java.io.File;
  import java.net.URI;
  import java.net.URL;
  import java.util.Arrays;
  import java.util.Date;
  import java.util.List;
  import java.util.Map;
  
  

Provides the client for accessing the Amazon S3 web service.

Amazon S3 provides storage for the Internet, and is designed to make web-scale computing easier for developers.

The Amazon S3 Java SDK provides a simple interface that can be used to store and retrieve any amount of data, at any time, from anywhere on the web. It gives any developer access to the same highly scalable, reliable, secure, fast, inexpensive infrastructure that Amazon uses to run its own global network of web sites. The service aims to maximize benefits of scale and to pass those benefits on to developers.

For more information about Amazon S3, please see http://aws.amazon.com/s3

 
 public class AmazonS3Client extends AmazonWebServiceClient implements AmazonS3 {

    
Shared logger for client events
 
     private static Log log = LogFactory.getLog(AmazonS3Client.class);

    
The AWS credentials (access key ID and secret key) to use when authenticating with AWS services.
 
     private AWSCredentials awsCredentials;

    
Low level client for sending requests to AWS services.
 
     protected final HttpClient client;

    
Optional request handlers for additional request processing.
 
     private List<RequestHandlerrequestHandlers = new ArrayList<RequestHandler>();

    
Responsible for handling error responses from all S3 service calls.
 
         new S3ErrorResponseHandler();

    
Shared response handler for operations that don't need to unmarshall anything.
 
         new S3XmlResponseHandler<Void>(null);

    
Utilities for validating bucket names
 
     private final BucketNameUtils bucketNameUtils = new BucketNameUtils();

    
Shared factory for converting configuration objects to XML
 

Constructs a new Amazon S3 client that will make anonymous requests to Amazon S3.

Only a subset of the Amazon S3 API will work with anonymous (i.e. unsigned) requests, but this can prove useful in some situations. For example:

 
     public AmazonS3Client() {
         this(null);
     }

    

Constructs a new Amazon S3 client using the specified AWS credentials to access Amazon S3.

Parameters:
awsCredentials The AWS credentials to use when making requests to Amazon S3 with this client.
See also:
AmazonS3Client.AmazonS3Client()
AmazonS3Client.AmazonS3Client(com.amazonaws.auth.AWSCredentials,com.amazonaws.ClientConfiguration)
 
     public AmazonS3Client(AWSCredentials awsCredentials) {
         this(awsCredentialsnew ClientConfiguration());
     }

    

Constructs a new Amazon S3 client using the specified AWS credentials and client configuration to access Amazon S3.

Parameters:
awsCredentials The AWS credentials to use when making requests to Amazon S3 with this client.
clientConfiguration The client configuration options controlling how this client connects to Amazon S3 (e.g. proxy settings, retry counts, etc).
See also:
AmazonS3Client.AmazonS3Client()
AmazonS3Client.AmazonS3Client(com.amazonaws.auth.AWSCredentials)
 
     public AmazonS3Client(AWSCredentials awsCredentialsClientConfiguration clientConfiguration) {
         super(clientConfiguration);
         this. = awsCredentials;
 
          = new HttpClient(clientConfiguration);
 
                 "/com/amazonaws/services/s3/request.handlers");
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#listNextBatchOfVersions(com.amazonaws.services.s3.model.S3VersionListing)
      */
     public VersionListing listNextBatchOfVersions(VersionListing previousVersionListing)
             throws AmazonClientExceptionAmazonServiceException {
         assertParameterNotNull(previousVersionListing,
             "The previous version listing parameter must be specified when listing the next batch of versions in a bucket");
 
         if (!previousVersionListing.isTruncated()) {
             VersionListing emptyListing = new VersionListing();
             emptyListing.setBucketName(previousVersionListing.getBucketName());
             emptyListing.setDelimiter(previousVersionListing.getDelimiter());
             emptyListing.setKeyMarker(previousVersionListing.getNextKeyMarker());
             emptyListing.setVersionIdMarker(previousVersionListing.getNextVersionIdMarker());
             emptyListing.setMaxKeys(previousVersionListing.getMaxKeys());
             emptyListing.setPrefix(previousVersionListing.getPrefix());
             emptyListing.setTruncated(false);
 
             return emptyListing;
         }
 
         return listVersions(new ListVersionsRequest(
                 previousVersionListing.getBucketName(),
                 previousVersionListing.getPrefix(),
                 previousVersionListing.getNextKeyMarker(),
                 previousVersionListing.getNextVersionIdMarker(),
                 previousVersionListing.getDelimiter(),
                 new IntegerpreviousVersionListing.getMaxKeys() ) ));
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#listVersions(java.lang.String, java.lang.String)
      */
     public VersionListing listVersions(String bucketNameString prefix)
             throws AmazonClientExceptionAmazonServiceException {
         return listVersions(new ListVersionsRequest(bucketNameprefixnullnullnullnull));
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#listVersions(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.Integer)
      */
     public VersionListing listVersions(String bucketNameString prefixString keyMarkerString versionIdMarkerString delimiterInteger maxKeys)
             throws AmazonClientExceptionAmazonServiceException {
 
         ListVersionsRequest request = new ListVersionsRequest()
             .withBucketName(bucketName)
             .withPrefix(prefix)
             .withDelimiter(delimiter)
             .withKeyMarker(keyMarker)
             .withVersionIdMarker(versionIdMarker)
             .withMaxResults(maxKeys);
         return listVersions(request);
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#listVersions(com.amazonaws.services.s3.model.ListVersionsRequest)
      */
     public VersionListing listVersions(ListVersionsRequest listVersionsRequest)
             throws AmazonClientExceptionAmazonServiceException {
         String bucketName = listVersionsRequest.getBucketName();
         String prefix = listVersionsRequest.getPrefix();
         String keyMarker = listVersionsRequest.getKeyMarker();
         String versionIdMarker = listVersionsRequest.getVersionIdMarker();
         String delimiter = listVersionsRequest.getDelimiter();
         Integer maxResults = listVersionsRequest.getMaxResults();
 
         assertParameterNotNull(bucketName"The bucket name parameter must be specified when listing versions in a bucket");
 
         Request<Voidrequest = createRequest(bucketNamenulllistVersionsRequest);
         request.addParameter("versions"null);
 
         if (prefix != nullrequest.addParameter("prefix"prefix);
         if (keyMarker != nullrequest.addParameter("key-marker"keyMarker);
         if (versionIdMarker != nullrequest.addParameter("version-id-marker"versionIdMarker);
         if (delimiter != nullrequest.addParameter("delimiter"delimiter);
         if (maxResults != null && maxResults.intValue() >= 0) request.addParameter("max-keys"maxResults.toString());
 
         signRequest(request.bucketNamenull);
         HttpRequest httpRequest = convertToHttpRequest(request.);
 
         S3XmlResponseHandler<VersionListingresponseHandler =
 
         return (VersionListing).execute(httpRequestresponseHandler);
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#listObjects(java.lang.String)
      */
     public ObjectListing listObjects(String bucketName)
             throws AmazonClientExceptionAmazonServiceException {
         return listObjects(new ListObjectsRequest(bucketNamenullnullnullnull));
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#listObjects(java.lang.String, java.lang.String)
      */
     public ObjectListing listObjects(String bucketNameString prefix)
             throws AmazonClientExceptionAmazonServiceException {
         return listObjects(new ListObjectsRequest(bucketNameprefixnullnullnull));
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#listObjects(com.amazonaws.services.s3.model.ListObjectsRequest)
      */
     public ObjectListing listObjects(ListObjectsRequest listObjectsRequest)
             throws AmazonClientExceptionAmazonServiceException {
         String bucketName = listObjectsRequest.getBucketName();
         String prefix = listObjectsRequest.getPrefix();
         String marker = listObjectsRequest.getMarker();
         String delimiter = listObjectsRequest.getDelimiter();
         Integer maxKeys = listObjectsRequest.getMaxKeys();
 
         assertParameterNotNull(bucketName"The bucket name parameter must be specified when listing objects in a bucket");
 
         Request<Voidrequest = createRequest(bucketNamenulllistObjectsRequest);
         if (prefix != nullrequest.addParameter("prefix"prefix);
         if (marker != nullrequest.addParameter("marker"marker);
         if (delimiter != nullrequest.addParameter("delimiter"delimiter);
         if (maxKeys != null && maxKeys.intValue() > 0) request.addParameter("max-keys"maxKeys.toString());
 
         signRequest(request.bucketNamenull);
         HttpRequest httpRequest = convertToHttpRequest(request.);
 
         S3XmlResponseHandler<ObjectListingresponseHandler =
 
         return (ObjectListing).execute(httpRequestresponseHandler);
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#listNextBatchOfObjects(com.amazonaws.services.s3.model.S3ObjectListing)
      */
     public ObjectListing listNextBatchOfObjects(ObjectListing previousObjectListing)
             throws AmazonClientExceptionAmazonServiceException {
         assertParameterNotNull(previousObjectListing,
                 "The previous object listing parameter must be specified when listing the next batch of objects in a bucket");
 
         if (!previousObjectListing.isTruncated()) {
             ObjectListing emptyListing = new ObjectListing();
             emptyListing.setBucketName(previousObjectListing.getBucketName());
             emptyListing.setDelimiter(previousObjectListing.getDelimiter());
             emptyListing.setMarker(previousObjectListing.getNextMarker());
             emptyListing.setMaxKeys(previousObjectListing.getMaxKeys());
             emptyListing.setPrefix(previousObjectListing.getPrefix());
             emptyListing.setTruncated(false);
 
             return emptyListing;
         }
 
         return listObjects(new ListObjectsRequest(
                 previousObjectListing.getBucketName(),
                 previousObjectListing.getPrefix(),
                 previousObjectListing.getNextMarker(),
                 previousObjectListing.getDelimiter(),
                 new IntegerpreviousObjectListing.getMaxKeys() ) ));
     }
 
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#getS3AccountOwner()
      */
     public Owner getS3AccountOwner()
             throws AmazonClientExceptionAmazonServiceException {
         Request<Voidrequest = createRequest(nullnullnull);
 
         signRequest(request.nullnull);
         HttpRequest httpRequest = convertToHttpRequest(request.);
 
         S3XmlResponseHandler<OwnerresponseHandler =
 
         return (Owner).execute(httpRequestresponseHandler);
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#listBuckets()
      */
     public List<BucketlistBuckets(ListBucketsRequest listBucketsRequest)
             throws AmazonClientExceptionAmazonServiceException {
         Request<Voidrequest = createRequest(nullnulllistBucketsRequest);
 
         signRequest(request.nullnull);
         HttpRequest httpRequest = convertToHttpRequest(request.);
 
         S3XmlResponseHandler<List<Bucket>> responseHandler =
             new S3XmlResponseHandler<List<Bucket>>(new Unmarshallers.ListBucketsUnmarshaller());
 
         return (List<Bucket>).execute(httpRequestresponseHandler);
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#listBuckets()
      */
     public List<BucketlistBuckets()
             throws AmazonClientExceptionAmazonServiceException {
         return listBuckets(new ListBucketsRequest());
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#getBucketLocation(java.lang.String)
      */
     public String getBucketLocation(String bucketName)
             throws AmazonClientExceptionAmazonServiceException {
         assertParameterNotNull(bucketName"The bucket name parameter must be specified when requesting a bucket's location");
 
         Request<Voidrequest = createRequest(bucketNamenullnull);
         request.addParameter("location"null);
 
         signRequest(request.bucketNamenull);
         HttpRequest httpRequest = convertToHttpRequest(request.);
 
         S3XmlResponseHandler<StringresponseHandler =
 
         return (String).execute(httpRequestresponseHandler);
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#createBucket(java.lang.String)
      */
     public Bucket createBucket(String bucketName)
             throws AmazonClientExceptionAmazonServiceException {
         return createBucket(new CreateBucketRequest(bucketName));
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#createBucket(java.lang.String, com.amazonaws.services.s3.model.Region)
      */
     public Bucket createBucket(String bucketNameRegion region)
             throws AmazonClientExceptionAmazonServiceException {
         return createBucket(new CreateBucketRequest(bucketNameregion));
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#createBucket(java.lang.String, java.lang.String)
      */
     public Bucket createBucket(String bucketNameString region)
             throws AmazonClientExceptionAmazonServiceException {
         return createBucket(new CreateBucketRequest(bucketNameregion));
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#createBucket(com.amazonaws.services.s3.model.CreateBucketRequest)
      */
     public Bucket createBucket(CreateBucketRequest createBucketRequest)
             throws AmazonClientExceptionAmazonServiceException {
         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 != nullbucketName = bucketName.trim();
         .validateBucketName(bucketName);
 
         Request<Voidrequest = createRequest(bucketNamenullcreateBucketRequest);
         signRequest(request.bucketNamenull);
         HttpRequest httpRequest = convertToHttpRequest(request.);
 
         /*
          * We can only send the CreateBucketConfiguration if we're *not*
          * creating a bucket in the US region.
          */
         if (region != null && !region.toUpperCase().equals(..toString())) {
             XmlWriter xml = new XmlWriter();
             xml.start("CreateBucketConfiguration""xmlns".);
             xml.start("LocationConstraint").value(region).end();
             xml.end();
 
             httpRequest.setContent(new ByteArrayInputStream(xml.getBytes()));
         }
 
         .execute(httpRequest);
 
         return new Bucket(bucketName);
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#getObjectAcl(java.lang.String, java.lang.String)
      */
     public AccessControlList getObjectAcl(String bucketNameString key)
             throws AmazonClientExceptionAmazonServiceException {
         return getObjectAcl(bucketNamekeynull);
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#getObjectAcl(java.lang.String, java.lang.String, java.lang.String)
      */
     public AccessControlList getObjectAcl(String bucketNameString keyString versionId)
             throws AmazonClientExceptionAmazonServiceException {
         assertParameterNotNull(bucketName"The bucket name parameter must be specified when requesting an object's ACL");
         assertParameterNotNull(key"The key parameter must be specified when requesting an object's ACL");
 
         return getAcl(bucketNamekeyversionId);
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#setObjectAcl(java.lang.String, java.lang.String, com.amazonaws.services.s3.model.AccessControlList)
      */
     public void setObjectAcl(String bucketNameString keyAccessControlList acl)
             throws AmazonClientExceptionAmazonServiceException {
         setObjectAcl(bucketNamekeynullacl);
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#setObjectAcl(java.lang.String, java.lang.String, com.amazonaws.services.s3.model.CannedAccessControlList)
      */
     public void setObjectAcl(String bucketNameString keyCannedAccessControlList acl)
             throws AmazonClientExceptionAmazonServiceException {
         setObjectAcl(bucketNamekeynullacl);
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#setObjectAcl(java.lang.String, java.lang.String, java.lang.String, com.amazonaws.services.s3.model.AccessControlList)
      */
     public void setObjectAcl(String bucketNameString keyString versionIdAccessControlList acl)
             throws AmazonClientExceptionAmazonServiceException {
         assertParameterNotNull(bucketName"The bucket name parameter must be specified when setting an object's ACL");
         assertParameterNotNull(key"The key parameter must be specified when setting an object's ACL");
         assertParameterNotNull(acl"The ACL parameter must be specified when setting an object's ACL");
 
         setAcl(bucketNamekeyversionIdacl);
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#setObjectAcl(java.lang.String, java.lang.String, java.lang.String, com.amazonaws.services.s3.model.CannedAccessControlList)
      */
     public void setObjectAcl(String bucketNameString keyString versionIdCannedAccessControlList acl)
             throws AmazonClientExceptionAmazonServiceException {
         assertParameterNotNull(bucketName"The bucket name parameter must be specified when setting an object's ACL");
         assertParameterNotNull(key"The key parameter must be specified when setting an object's ACL");
         assertParameterNotNull(acl"The ACL parameter must be specified when setting an object's ACL");
 
         setAcl(bucketNamekeyversionIdacl);
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#getBucketAcl(java.lang.String)
      */
     public AccessControlList getBucketAcl(String bucketName)
             throws AmazonClientExceptionAmazonServiceException {
         assertParameterNotNull(bucketName"The bucket name parameter must be specified when requesting a bucket's ACL");
 
         return getAcl(bucketNamenullnull);
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#setBucketAcl(java.lang.String, com.amazonaws.services.s3.model.AccessControlList)
      */
     public void setBucketAcl(String bucketNameAccessControlList acl)
             throws AmazonClientExceptionAmazonServiceException {
         assertParameterNotNull(bucketName"The bucket name parameter must be specified when setting a bucket's ACL");
         assertParameterNotNull(acl"The ACL parameter must be specified when setting a bucket's ACL");
 
         setAcl(bucketNamenullnullacl);
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#setBucketAcl(java.lang.String, com.amazonaws.services.s3.model.CannedAccessControlList)
      */
     public void setBucketAcl(String bucketNameCannedAccessControlList acl)
             throws AmazonClientExceptionAmazonServiceException {
         assertParameterNotNull(bucketName"The bucket name parameter must be specified when setting a bucket's ACL");
         assertParameterNotNull(acl"The ACL parameter must be specified when setting a bucket's ACL");
 
         setAcl(bucketNamenullnullacl);
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#getObjectMetadata(java.lang.String, java.lang.String)
      */
     public ObjectMetadata getObjectMetadata(String bucketNameString key)
             throws AmazonClientExceptionAmazonServiceException {
         return getObjectMetadata(new GetObjectMetadataRequest(bucketNamekey));
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#getObjectMetadata(com.amazonaws.services.s3.model.GetObjectMetadataRequest)
      */
     public ObjectMetadata getObjectMetadata(GetObjectMetadataRequest getObjectMetadataRequest)
             throws AmazonClientExceptionAmazonServiceException {
         assertParameterNotNull(getObjectMetadataRequest"The GetObjectMetadataRequest parameter must be specified when requesting an object's metadata");
 
         String bucketName = getObjectMetadataRequest.getBucketName();
         String key = getObjectMetadataRequest.getKey();
         String versionId = getObjectMetadataRequest.getVersionId();
 
         assertParameterNotNull(bucketName"The bucket name parameter must be specified when requesting an object's metadata");
         assertParameterNotNull(key"The key parameter must be specified when requesting an object's metadata");
 
         Request<Voidrequest = createRequest(bucketNamekeygetObjectMetadataRequest);
         if (versionId != nullrequest.addParameter("versionId"versionId);
 
         signRequest(request.bucketNamekey);
         HttpRequest httpRequest = convertToHttpRequest(request.);
 
         S3MetadataResponseHandler responseHandler = new S3MetadataResponseHandler();
         return (ObjectMetadata).execute(httpRequestresponseHandler);
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#getObject(java.lang.String, java.lang.String)
      */
     public S3Object getObject(String bucketNameString key)
             throws AmazonClientExceptionAmazonServiceException {
         return getObject(new GetObjectRequest(bucketNamekey));
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#doesBucketExist(java.lang.String)
      */
     public boolean doesBucketExist(String bucketName)
         throws AmazonClientExceptionAmazonServiceException {
 
         try {
             listObjects(new ListObjectsRequest(bucketNamenullnullnull, 0));
 
             // it exists and the current account owns it
             return true;
         } catch (AmazonServiceException ase) {
             switch (ase.getStatusCode()) {
             case 403:
                 /*
                  * A permissions error means the bucket exists, but is owned by
                  * another account.
                  */
                 return true;
             case 404:
                 return false;
             default:
                 throw ase;
             }
         }
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#changeStorageClass(java.lang.String, java.lang.String, java.lang.String)
      */
     public void changeObjectStorageClass(String bucketNameString keyStorageClass newStorageClass)
         throws AmazonClientExceptionAmazonServiceException {
         assertParameterNotNull(bucketName,
             "The bucketName parameter must be specified when changing an object's storage class");
         assertParameterNotNull(key,
             "The key parameter must be specified when changing an object's storage class");
         assertParameterNotNull(newStorageClass,
             "The newStorageClass parameter must be specified when changing an object's storage class");
 
         copyObject(new CopyObjectRequest(bucketNamekeybucketNamekey)
             .withStorageClass(newStorageClass.toString()));
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#getObject(com.amazonaws.services.s3.model.GetObjectRequest)
      */
     public S3Object getObject(GetObjectRequest getObjectRequest)
             throws AmazonClientExceptionAmazonServiceException {
         assertParameterNotNull(getObjectRequest,
                 "The GetObjectRequest parameter must be specified when requesting an object");
         assertParameterNotNull(getObjectRequest.getBucketName(),
                 "The bucket name parameter must be specified when requesting an object");
         assertParameterNotNull(getObjectRequest.getKey(),
                 "The key parameter must be specified when requesting an object");
 
         String bucketName = getObjectRequest.getBucketName();
         String key = getObjectRequest.getKey();
 
         Request<Voidrequest = createRequest(bucketNamekeygetObjectRequest);
 
         if (getObjectRequest.getVersionId() != null) {
             request.addParameter("versionId"getObjectRequest.getVersionId());
         }
 
         // Range
         if (getObjectRequest.getRange() != null) {
             long[] range = getObjectRequest.getRange();
             request.addHeader(."bytes=" + Long.toString(range[0]) + "-" + Long.toString(range[1]));
         }
 
                 getObjectRequest.getModifiedSinceConstraint());
                 getObjectRequest.getUnmodifiedSinceConstraint());
                 getObjectRequest.getMatchingETagConstraints());
                 getObjectRequest.getNonmatchingETagConstraints());
 
         signRequest(request.bucketNamekey);
         HttpRequest httpRequest = convertToHttpRequest(request.);
 
         try {
             S3ObjectResponseHandler responseHandler = new S3ObjectResponseHandler();
             S3Object s3Object = (S3Object).execute(httpRequestresponseHandler);
 
             /*
              * TODO: For now, it's easiest to set there here in the client, but
              *       we could push this back into the response handler with a
              *       little more work.
              */
             s3Object.setBucketName(bucketName);
             s3Object.setKey(key);
 
             /*
              * TODO: It'd be nice to check the integrity of the data was received from S3,
              *       but we'd have to read off the stream and buffer the contents somewhere
              *       in order to do that.
              *
              *       We could consider adding an option for this in the future, or wrapping
              *       the InputStream in another implementation of FilterInputStream that
              *       would calculate the checksum when the user reads the data and then
              *       notify them somehow if there was a problem.
              */
             return s3Object;
         } catch (AmazonS3Exception ase) {
             /*
              * If the request failed because one of the specified constraints
              * was not met (ex: matching ETag, modified since date, etc.), then
              * return null, so that users don't have to wrap their code in
              * try/catch blocks and check for this status code if they want to
              * use constraints.
              */
             if (ase.getStatusCode() == 412 || ase.getStatusCode() == 304) {
                 return null;
             }
 
             throw ase;
         }
     }
 
 	
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#getObject(com.amazonaws.services.s3.model.GetObjectRequest, java.io.File)
      */
     public ObjectMetadata getObject(GetObjectRequest getObjectRequestFile destinationFile)
             throws AmazonClientExceptionAmazonServiceException {
         assertParameterNotNull(destinationFile,
                 "The destination file parameter must be specified when downloading an object directly to a file");
 
         S3Object s3Object = getObject(getObjectRequest);
         // getObject can return null if constraints were specified but not met
         if (s3Object == nullreturn null;
 
         OutputStream outputStream = null;
         try {
             outputStream = new BufferedOutputStream(new FileOutputStream(destinationFile));
             byte[] buffer = new byte[1024*10];
             int bytesRead;
             while ((bytesRead = s3Object.getObjectContent().read(buffer)) > -1) {
                 outputStream.write(buffer, 0, bytesRead);
             }
         } catch (IOException e) {
             throw new AmazonClientException(
                     "Unable to store object contents to disk: " + e.getMessage(), e);
         } finally {
             try {outputStream.close();} catch (Exception e) {}
             try {s3Object.getObjectContent().close();} catch (Exception e) {}
         }
 
         try {
             byte[] clientSideHash = ServiceUtils.computeMD5Hash(new FileInputStream(destinationFile));
             byte[] serverSideHash = ServiceUtils.fromHex(s3Object.getObjectMetadata().getETag());
 
             if (!Arrays.equals(clientSideHashserverSideHash)) {
                 throw new AmazonClientException("Unable to verify integrity of data download.  " +
                         "Client calculated content hash didn't match hash calculated by Amazon S3.  " +
                         "The data stored in '" + destinationFile.getAbsolutePath() + "' may be corrupt.");
             }
         } catch (Exception e) {
             .warn("Unable to calculate MD5 hash to validate download: " + e.getMessage(), e);
         }
 
         return s3Object.getObjectMetadata();
     }
 	
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#deleteBucket(java.lang.String)
      */
     public void deleteBucket(String bucketName)
             throws AmazonClientExceptionAmazonServiceException {
         deleteBucket(new DeleteBucketRequest(bucketName));
     }
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#deleteBucket(com.amazonaws.services.s3.model.DeleteBucketRequest)
      */
     public void deleteBucket(DeleteBucketRequest deleteBucketRequest)
             throws AmazonClientExceptionAmazonServiceException {
         assertParameterNotNull(deleteBucketRequest,
                 "The DeleteBucketRequest parameter must be specified when deleting a bucket");
 
         String bucketName = deleteBucketRequest.getBucketName();
         assertParameterNotNull(bucketName,
                 "The bucket name parameter must be specified when deleting a bucket");
 
         Request<Voidrequest = createRequest(bucketNamenulldeleteBucketRequest);
         signRequest(request.bucketNamenull);
         HttpRequest httpRequest = convertToHttpRequest(request.);
         .execute(httpRequest);
     }
 
 	
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#putObject(java.lang.String, java.lang.String, java.io.File)
      */
     public PutObjectResult putObject(String bucketNameString keyFile file)
             throws AmazonClientExceptionAmazonServiceException {
         return putObject(new PutObjectRequest(bucketNamekeyfile)
             .withMetadata(new ObjectMetadata()));
     }
 	
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#putObject(java.lang.String, java.lang.String, java.io.InputStream, com.amazonaws.services.s3.model.S3ObjectMetadata)
      */
     public PutObjectResult putObject(String bucketNameString keyInputStream inputObjectMetadata metadata)
             throws AmazonClientExceptionAmazonServiceException {
         return putObject(new PutObjectRequest(bucketNamekeyinputmetadata));
     }
 
 
     /* (non-Javadoc)
      * @see com.amazonaws.services.s3.AmazonS3#putObject(com.amazonaws.services.s3.model.PutObjectRequest)
      */
     public PutObjectResult putObject(PutObjectRequest putObjectRequest)
             throws AmazonClientExceptionAmazonServiceException {
         assertParameterNotNull(putObjectRequest"The PutObjectRequest parameter must be specified when uploading an object");
 
         String bucketName = putObjectRequest.getBucketName();
         String key = putObjectRequest.getKey();
         ObjectMetadata metadata = putObjectRequest.getMetadata();
         InputStream input = putObjectRequest.getInputStream();
         ProgressListener progressListener = putObjectRequest.getProgressListener();
         if (metadata == nullmetadata = new ObjectMetadata();
 
         assertParameterNotNull(bucketName"The bucket name parameter must be specified when uploading an object");
         assertParameterNotNull(key"The key parameter must be specified when uploading an object");
 
 		
         // If a file is specified for upload, we need to pull some additional
         // information from it to auto-configure a few options
         if (putObjectRequest.getFile() != null) {
             File file = putObjectRequest.getFile();
 
             // Always set the content length, even if it's already set
             metadata.setContentLength(file.length());
 
             // Only set the content type if it hasn't already been set
             if (metadata.getContentType() == null) {
                 metadata.setContentType(Mimetypes.getInstance().getMimetype(file));
             }
 
             FileInputStream fileInputStream = null;
             try {
                 fileInputStream = new FileInputStream(file);
                 byte[] md5Hash = ServiceUtils.computeMD5Hash(fileInputStream);
                 metadata.setContentMD5(ServiceUtils.toBase64(md5Hash));
             } catch (Exception e) {
                 throw new AmazonClientException(
                         "Unable to calculate MD5 hash: " + e.getMessage(), e);
             } finally {
                 try {fileInputStream.close();} catch (Exception e) {}
             }
 
             try {
                 input = new RepeatableFileInputStream(file);
             } catch (FileNotFoundException fnfe) {
                 throw new AmazonClientException("Unable to find file to upload"fnfe);
             }
         }
 		
 
         Request<Voidrequest = createRequest(bucketNamekeyputObjectRequest);
 
         if (putObjectRequest.getCannedAcl() != null) {
             request.addHeader(.putObjectRequest.getCannedAcl().toString());
         }
 
         if (putObjectRequest.getStorageClass() != null) {
             request.addHeader(.putObjectRequest.getStorageClass());
         }
 
         if (metadata.getContentLength() <= 0) {
             /*
              * There's nothing we can do except for let the HTTP client buffer
              * the input stream contents if the caller doesn't tell us how much
              * data to expect in a stream since we have to explicitly tell
              * Amazon S3 how much we're sending before we start sending any of
              * it.
              */
             .warn("No content length specified for stream data.  " +
                      "Stream contents will be buffered in memory and could result in " +
                      "out of memory errors.");
         }
 
         if (progressListener != null) {
             input = new ProgressReportingInputStream(inputprogressListener);
             fireProgressEvent(progressListener.);
         }
 
         if (!input.markSupported()) {
             input = new RepeatableInputStream(input.);
         }
 
         MD5DigestCalculatingInputStream md5DigestStream = null;
         if (metadata.getContentMD5() == null) {
             /*
              * If the user hasn't set the content MD5, then we don't want to
              * buffer the whole stream in memory just to calculate it. Instead,
              * we can calculate it on the fly and validate it with the returned
              * ETag from the object upload.
              */
             try {
                 md5DigestStream = new MD5DigestCalculatingInputStream(input);
                 input = md5DigestStream;
             } catch (NoSuchAlgorithmException e) {
                 .warn("No MD5 digest algorithm available.  Unable to calculate " +
                          "checksum and verify data integrity."e);
             }
         }
 
         if (metadata.getContentType() == null) {
             /*
              * Default to the "application/octet-stream" if the user hasn't
              * specified a content type.
              */
             metadata.setContentType(.);
         }
 
        populateRequestMetadata(requestmetadata);
        signRequest(request.bucketNamekey);
        HttpRequest httpRequest = convertToHttpRequest(request.);
        httpRequest.setContent(input);
        ObjectMetadata returnedMetadata = null;
        try {
            S3MetadataResponseHandler responseHandler = new S3MetadataResponseHandler();
            returnedMetadata = (ObjectMetadata).execute(httpRequestresponseHandler);
        } catch (AmazonClientException ace) {
            fireProgressEvent(progressListener.);
            throw ace;
        } finally {
            try {input.close();} catch (Exception e) {
                .warn("Unable to cleanly close input stream: " + e.getMessage(), e);
            }
        }
        String contentMd5 = metadata.getContentMD5();
        if (md5DigestStream != null) {
            contentMd5 = ServiceUtils.toBase64(md5DigestStream.getMd5Digest());
        }
        if (returnedMetadata != null && contentMd5 != null) {
            byte[] clientSideHash = ServiceUtils.fromBase64(contentMd5);
            byte[] serverSideHash = ServiceUtils.fromHex(returnedMetadata.getETag());
            if (!Arrays.equals(clientSideHashserverSideHash)) {
                fireProgressEvent(progressListener.);
                throw new AmazonClientException("Unable to verify integrity of data upload.  " +
                        "Client calculated content hash didn't match hash calculated by Amazon S3.  " +
                        "You may need to delete the data stored in Amazon S3.");
            }
        }
        fireProgressEvent(progressListener.);
        
        PutObjectResult result = new PutObjectResult();
        result.setETag(returnedMetadata.getETag());
        result.setVersionId(returnedMetadata.getVersionId());
        return result;
    }
    
    /* (non-Javadoc)
     * @see com.amazonaws.services.s3.AmazonS3#copyObject(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     */
    public CopyObjectResult copyObject(String sourceBucketNameString sourceKey,
                                       String destinationBucketNameString destinationKey)
            throws AmazonClientExceptionAmazonServiceException {
        return copyObject(new CopyObjectRequest(sourceBucketNamesourceKey,
                                                destinationBucketNamedestinationKey));
    }
    /* (non-Javadoc)
     * @see com.amazonaws.services.s3.AmazonS3#copyObject(com.amazonaws.services.s3.model.CopyObjectRequest)
     */
    public CopyObjectResult copyObject(CopyObjectRequest copyObjectRequest)
            throws AmazonClientExceptionAmazonServiceException {
        assertParameterNotNull(copyObjectRequest.getSourceBucketName(),
                "The source bucket name must be specified when copying an object");
        assertParameterNotNull(copyObjectRequest.getSourceKey(),
                "The source object key must be specified when copying an object");
        assertParameterNotNull(copyObjectRequest.getDestinationBucketName(),
                "The destination bucket name must be specified when copying an object");
        assertParameterNotNull(copyObjectRequest.getDestinationKey(),
                "The destination object key must be specified when copying an object");
        String destinationKey = copyObjectRequest.getDestinationKey();
        String destinationBucketName = copyObjectRequest.getDestinationBucketName();
        Request<Voidrequest = createRequest(destinationBucketNamedestinationKeycopyObjectRequest);
        populateRequestWithCopyObjectParameters(requestcopyObjectRequest);
        /*
         * We can't send the Content-Length header if the user specified it,
         * otherwise it messes up the HTTP connection when the remote server
         * thinks there's more data to pull.
         */
        request.getHeaders().remove(.);
        signRequest(request.destinationBucketNamedestinationKey);
        HttpRequest httpRequest = convertToHttpRequest(request.);