Java Google Cloud Storage: Copy File (Object) to another Bucket

Back to Index

Copies a fie (also known as an object) to another Google Cloud Storage bucket. This example copies the file "starfish.jpg" from the "chilkat-test" bucket to the "chilkat-images" bucket. In Google Cloud Storage, moving a file to a new bucket is a 2-step operation: First copy the file, then delete the original.

Documentation: https://cloud.google.com/storage/docs/renaming-copying-moving-objects

CURL Command

curl -X POST https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/starfish.jpg/rewriteTo/b/chilkat-images/o/starfish.jpg?project=MY_CLOUD_STORAGE_PROJECT \
    --header "Authorization: Bearer CLOUD_STORAGE_TOKEN"

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;

    //  URL: https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/starfish.jpg/rewriteTo/b/chilkat-images/o/starfish.jpg?project=MY_CLOUD_STORAGE_PROJECT
    boolean bTls = true;
    int port = 443;
    boolean bAutoReconnect = true;
    success = rest.Connect("www.googleapis.com",port,bTls,bAutoReconnect);
    if (success != true) {
        System.out.println("ConnectFailReason: " + rest.get_ConnectFailReason());
        System.out.println(rest.lastErrorText());
        return;
        }

    rest.AddHeader("Authorization","Bearer CLOUD_STORAGE_TOKEN");

    CkStringBuilder sbResponseBody = new CkStringBuilder();
    success = rest.FullRequestNoBodySb("POST","/storage/v1/b/chilkat-bucket/o/starfish.jpg/rewriteTo/b/chilkat-images/o/starfish.jpg?project=MY_CLOUD_STORAGE_PROJECT",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;
        }

    CkJsonObject jsonResponse = new CkJsonObject();
    jsonResponse.LoadSb(sbResponseBody);

    //  See the Online Tool for Generating JSON Parse Code

    String kind = jsonResponse.stringOf("kind");
    String totalBytesRewritten = jsonResponse.stringOf("totalBytesRewritten");
    String objectSize = jsonResponse.stringOf("objectSize");
    boolean done = jsonResponse.BoolOf("done");
    String resourceKind = jsonResponse.stringOf("resource.kind");
    String resourceId = jsonResponse.stringOf("resource.id");
    String resourceSelfLink = jsonResponse.stringOf("resource.selfLink");
    String resourceName = jsonResponse.stringOf("resource.name");
    String resourceBucket = jsonResponse.stringOf("resource.bucket");
    String resourceGeneration = jsonResponse.stringOf("resource.generation");
    String resourceMetageneration = jsonResponse.stringOf("resource.metageneration");
    String resourceContentType = jsonResponse.stringOf("resource.contentType");
    String resourceTimeCreated = jsonResponse.stringOf("resource.timeCreated");
    String resourceUpdated = jsonResponse.stringOf("resource.updated");
    String resourceStorageClass = jsonResponse.stringOf("resource.storageClass");
    String resourceTimeStorageClassUpdated = jsonResponse.stringOf("resource.timeStorageClassUpdated");
    String resourceSize = jsonResponse.stringOf("resource.size");
    String resourceMd5Hash = jsonResponse.stringOf("resource.md5Hash");
    String resourceMediaLink = jsonResponse.stringOf("resource.mediaLink");
    String resourceCrc32c = jsonResponse.stringOf("resource.crc32c");
    String resourceEtag = jsonResponse.stringOf("resource.etag");
  }
}

Sample JSON Response Body

{
  "kind": "storage#rewriteResponse",
  "totalBytesRewritten": "6229",
  "objectSize": "6229",
  "done": true,
  "resource": {
    "kind": "storage#object",
    "id": "chilkat-images/starfish.jpg/1540298057547474",
    "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-images/o/starfish.jpg",
    "name": "starfish.jpg",
    "bucket": "chilkat-images",
    "generation": "1540298057547474",
    "metageneration": "1",
    "contentType": "image/jpeg",
    "timeCreated": "2018-10-23T12:34:17.547Z",
    "updated": "2018-10-23T12:34:17.547Z",
    "storageClass": "MULTI_REGIONAL",
    "timeStorageClassUpdated": "2018-10-23T12:34:17.547Z",
    "size": "6229",
    "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==",
    "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-images/o/starfish.jpg?generation=1540298057547474&alt=media",
    "crc32c": "9RjgwQ==",
    "etag": "CNL9xbTJnN4CEAE="
  }
}