Sample code for 30+ languages & platforms
Java

S3 - Upload to Bucket in a Region

See more Amazon S3 (new) Examples

Demonstrates how to upload a file to an S3 bucket that was created in a particular region.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.AuthAws;
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // When usig the REST class to access an S3 bucket that has been created in a particular region,
        // there are three important things to remember:
        //
        // 1) Connect to the domain that includes the region.  For example, instead of connecting to
        //    "s3.amazonaws.com", connect to "s3.eu-central-1.amazonaws.com" if your bucket is in the EU Frankfurt region.
        //    See http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region   for a list of S3 region domains (endpoints)
        //
        // 2) Set the AuthAws.Region property equal to the region, such as "eu-central-1".
        //
        // 3) Include the region in request's Host header.
        //

        // This example will upload to a bucket in the eu-central-1 region.
        //
        Rest rest = new Rest();

        // Connect to the Amazon AWS REST server.
        boolean bTls = true;
        int port = 443;
        boolean bAutoReconnect = true;
        rest.connect("s3.eu-central-1.amazonaws.com", port, bTls, bAutoReconnect);

        // Provide AWS credentials for the REST call.
        AuthAws authAws = new AuthAws();
        authAws.setAccessKey("AWS_ACCESS_KEY");
        authAws.setSecretKey("AWS_SECRET_KEY");
        authAws.setServiceName("s3");
        authAws.setRegion("eu-central-1");
        rest.setAuthAws(authAws);

        // Set the bucket name via the HOST header.
        // In this case, the bucket name is "chilkateufrankfurt" (which was created in the eu-central-1 region)
        rest.setHost("chilkateufrankfurt.s3.eu-central-1.amazonaws.com");

        BinData fileData = new BinData();
        fileData.loadFile("qa_data/jpg/penguins.jpg");

        // Upload the file to Amazon S3.
        StringBuilder responseBody = new StringBuilder();
        try {
            rest.fullRequestBd("PUT", "/penguins.jpg", fileData, responseBody);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Did we get a 200 response indicating success?
        int statusCode = rest.getResponseStatusCode();
        if (statusCode != 200) {
            System.out.println("Error response: " + responseBody.getAsString());
            System.out.println("Status code: " + statusCode + ", Status text: " + rest.getResponseStatusText());
            return;
        }

        System.out.println("File successfully uploaded.");
    }
}