Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)
Print this pageEmail this pageGo to the ForumsView the PDFShare this page on TwitterShare this page on FacebookBookmark this page on DeliciousSubmit this page to RedditSubmit this page to DiggDid this page help you?  Yes  No   Tell us about it...

Upload a File

The following tasks guide you through using the high-level Java classes to upload a file. The API provides several variations, called overloads, of the upload method to easily upload your data.

High-Level API File Uploading Process

1

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

2

Execute one of the TransferManager.upload overloads depending on whether you are uploading data from a file, or a stream.


The following Java code sample demonstrates the preceding tasks.

AWSCredentials myCredentials = new BasicAWSCredentials(myAccessKeyID, mySecretKey);

TransferManager tm = new TransferManager(myCredentials);        
// Asynchronous call.
Upload upload = tm.upload(existingBucketName, keyName, new File(filePath));		

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 com.amazonaws.AmazonClientException;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.Upload;

public class HighLevel_Java_UploadFile {

    public static void main(String[] args) throws Exception {
        String existingBucketName = "*** Provide existing bucket name ***";
        String keyName            = "*** Provide object key ***";
        String filePath           = "*** Provide file to upload ***";  
        
        TransferManager tm = new TransferManager(new PropertiesCredentials(
        		HighLevel_Java_UploadFile.class.getResourceAsStream(
        				                   "AwsCredentials.properties")));        

        // TransferManager processes all transfers asynchronously, 
        // so this call will return immediately.
        Upload upload = tm.upload(
        		existingBucketName, keyName, new File(filePath));

        try {
        	// Or you can block and wait for the upload to finish
        	upload.waitForCompletion();
        } catch (AmazonClientException amazonClientException) {
        	System.out.println("Unable to upload file, upload was aborted.");
        	amazonClientException.printStackTrace();
        }
    }
}