Java AWS S3: Copy S3 File to a Different Bucket

Back to Index

Demonstrates how to copy an S3 file from one bucket to another. The file can also be renamed if desired. (The file already exists in S3 in one bucket, and is copied without downloading/uploading to another S3 bucket.) This example copies from /chilkat.qa/starfish.jpg to /chilkat.ocean/starfishCopy.jpg

Documentation: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html

CURL Command

curl -X PUT https://chilkat.ocean.s3.us-west-2.amazonaws.com/starfishCopy.jpg \
    -H "x-amz-copy-source: /chilkat.qa/starfish.jpg"

Java Example

import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    CkRest rest = new CkRest();
    boolean success;

    CkAuthAws authAws = new CkAuthAws();
    authAws.put_AccessKey("AWS_ACCESS_KEY");
    authAws.put_SecretKey("AWS_SECRET_KEY");
    authAws.put_Region("us-west-2");
    authAws.put_ServiceName("s3");
    rest.SetAuthAws(authAws);

    //  URL: https://chilkat.ocean.s3.us-west-2.amazonaws.com/starfishCopy.jpg
    boolean bTls = true;
    int port = 443;
    boolean bAutoReconnect = true;
    success = rest.Connect("chilkat.ocean.s3.us-west-2.amazonaws.com",port,bTls,bAutoReconnect);
    if (success != true) {
        System.out.println("ConnectFailReason: " + rest.get_ConnectFailReason());
        System.out.println(rest.lastErrorText());
        return;
        }

    rest.AddHeader("x-amz-copy-source","/chilkat.qa/starfish.jpg");

    CkStringBuilder sbResponseBody = new CkStringBuilder();
    success = rest.FullRequestNoBodySb("PUT","/starfishCopy.jpg",sbResponseBody);
    if (success != true) {
        System.out.println(rest.lastErrorText());
        return;
        }

    int respStatusCode = rest.get_ResponseStatusCode();
    if (respStatusCode >= 400) {
        System.out.println("Response Status Code = " + respStatusCode);
        System.out.println("Response Header:");
        System.out.println(rest.responseHeader());
        System.out.println("Response Body:");
        System.out.println(sbResponseBody.getAsString());
        return;
        }

    CkXml xmlResponse = new CkXml();
    xmlResponse.LoadSb(sbResponseBody,true);

    //  See the Online Tool for Generating XML Parse Code
    String CopyObjectResult_xmlns;
    String tagPath;
    String LastModified;
    String ETag;

    CopyObjectResult_xmlns = xmlResponse.getAttrValue("xmlns");
    LastModified = xmlResponse.getChildContent("LastModified");
    ETag = xmlResponse.getChildContent("ETag");
  }
}

Sample XML Response Body

<?xml version="1.0" encoding="UTF-8"?>
<CopyObjectResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <LastModified>2018-12-18T15:53:32.000Z</LastModified>
    <ETag>"2e9c59dbf2662367dc97dfdda85da048"</ETag>
</CopyObjectResult>