Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Upload a File

The following tasks guide you through using the low-level Java classes to upload a file.

Low-Level API File Uploading Process

1

Create an instance of the AmazonS3Client class, by providing your AWS credentials.

2

Initiate multipart upload by executing the AmazonS3Client.initiateMultipartUpload method. You will need to provide the required information, i.e., bucket name and key name, to initiate the multipart upload by creating an instance of the InitiateMultipartUploadRequest class.

3

Save the upload ID that the AmazonS3Client.initiateMultipartUpload method returns. You will need to provide this upload ID for each subsequent multipart upload operation.

4

Upload parts. For each part upload, execute the AmazonS3Client.uploadPart method. You need to provide part upload information, such as upload ID, bucket name, and the part number. You provide this information by creating an instance of the UploadPartRequest class.

5

Save the response of the AmazonS3Client.uploadPart method in a list. This response includes the ETag value and the part number you will need to complete the multipart upload.

6

Repeat tasks 4 and 5 for each part.

7

Execute the AmazonS3Client.completeMultipartUpload method to complete the multipart upload.


The following Java code sample demonstrates the preceding tasks.

AmazonS3 s3Client = new AmazonS3Client(new PropertiesCredentials(
		            LowLevelJavaUploadFile.class.getResourceAsStream(
                                             "AwsCredentials.properties")));

// Create a list of UploadPartResponse objects. You get one of these for
// each part upload.
List<PartETag> partETags = new ArrayList<PartETag>();

// Step 1: Initialize.
InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(
                                                    existingBucketName, keyName);
InitiateMultipartUploadResult initResponse = 
                              s3Client.initiateMultipartUpload(initRequest);

File file = new File(filePath);
long contentLength = file.length();
long partSize = 5 * 1024 * 1024; // Set part size to 5 MB.

try {
    // Step 2: Upload parts.
    long filePosition = 0;
    for (int i = 1; filePosition < contentLength; i++) {
        // Last part can be less than 5 MB. Adjust part size.
    	partSize = Math.min(partSize, (contentLength - filePosition));
    	
        // Create request to upload a part.
        UploadPartRequest uploadRequest = new UploadPartRequest()
            .withBucketName(existingBucketName).withKey(keyName)
            .withUploadId(initResponse.getUploadId()).withPartNumber(i)
            .withFileOffset(filePosition)
            .withFile(file)
            .withPartSize(partSize);

        // Upload part and add response to our list.
        partETags.add(s3Client.uploadPart(uploadRequest).getPartETag());

        filePosition += partSize;
    }

    // Step 3: Complete.
    CompleteMultipartUploadRequest compRequest = new 
                CompleteMultipartUploadRequest(existingBucketName, 
                                               keyName, 
                                               initResponse.getUploadId(), 
                                               partETags);

    s3Client.completeMultipartUpload(compRequest);
} catch (Exception e) {
    s3Client.abortMultipartUpload(new AbortMultipartUploadRequest(
              existingBucketName, keyName, initResponse.getUploadId()));
}

Example

The following Java code example uploads a file to an Amazon S3 bucket. For instructions on how to create and test a working sample, see Testing the Java Code Examples.

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.AbortMultipartUploadRequest;
import com.amazonaws.services.s3.model.CompleteMultipartUploadRequest;
import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest;
import com.amazonaws.services.s3.model.InitiateMultipartUploadResult;
import com.amazonaws.services.s3.model.ListMultipartUploadsRequest;
import com.amazonaws.services.s3.model.ListPartsRequest;
import com.amazonaws.services.s3.model.MultipartUploadListing;
import com.amazonaws.services.s3.model.PartETag;
import com.amazonaws.services.s3.model.PartListing;
import com.amazonaws.services.s3.model.UploadPartRequest;

public class LowLevelJavaUploadFile {

    public static void main(String[] args) throws IOException {
        String existingBucketName="*** Provide-Your-Existing-BucketName ***"; 
        String keyName             = "*** Provide-Key-Name ***";
        String filePath            = "*** Provide-File-Path ***";   
        
        AmazonS3 s3Client = new AmazonS3Client(new PropertiesCredentials(
        		LowLevelJavaUploadFile.class.getResourceAsStream(
        				                  "AwsCredentials.properties")));        

        // Create a list of UploadPartResponse objects. You get one of these
        // for each part upload.
        List<PartETag> partETags = new ArrayList<PartETag>();

        // Step 1: Initialize.
        InitiateMultipartUploadRequest initRequest = new 
             InitiateMultipartUploadRequest(existingBucketName, keyName);
        InitiateMultipartUploadResult initResponse = 
        	                   s3Client.initiateMultipartUpload(initRequest);

        File file = new File(filePath);
        long contentLength = file.length();
        long partSize = 5242880; // Set part size to 5 MB.

        try {
            // Step 2: Upload parts.
            long filePosition = 0;
            for (int i = 1; filePosition < contentLength; i++) {
                // Last part can be less than 5 MB. Adjust part size.
            	partSize = Math.min(partSize, (contentLength - filePosition));
            	
                // Create request to upload a part.
                UploadPartRequest uploadRequest = new UploadPartRequest()
                    .withBucketName(existingBucketName).withKey(keyName)
                    .withUploadId(initResponse.getUploadId()).withPartNumber(i)
                    .withFileOffset(filePosition)
                    .withFile(file)
                    .withPartSize(partSize);

                // Upload part and add response to our list.
                partETags.add(
                		s3Client.uploadPart(uploadRequest).getPartETag());

                filePosition += partSize;
            }

            // Step 3: Complete.
            CompleteMultipartUploadRequest compRequest = new 
                         CompleteMultipartUploadRequest(
                                    existingBucketName, 
                                    keyName, 
                                    initResponse.getUploadId(), 
                                    partETags);

            s3Client.completeMultipartUpload(compRequest);
        } catch (Exception e) {
            s3Client.abortMultipartUpload(new AbortMultipartUploadRequest(
                    existingBucketName, keyName, initResponse.getUploadId()));
        }
    }
}