Java Google Cloud Storage: List Objects in a Bucket

Back to Index

Retrieves a list of objects in the Google Cloud Storage bucket. The name of the bucket is specified in the URL's path. This example retrieves a listing of the objects in the "chilkat-test" bucket.

Documentation: https://cloud.google.com/storage/docs/json_api/v1/objects/list

CURL Command

curl -X GET https://www.googleapis.com/storage/v1/b/chilkat-bucket/o?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?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("GET","/storage/v1/b/chilkat-bucket/o?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
    int i;
    int count_i;

    String kind = jsonResponse.stringOf("kind");
    i = 0;
    count_i = jsonResponse.SizeOfArray("items");
    while (i < count_i) {
        jsonResponse.put_I(i);
        kind = jsonResponse.stringOf("items[i].kind");
        String id = jsonResponse.stringOf("items[i].id");
        String selfLink = jsonResponse.stringOf("items[i].selfLink");
        String name = jsonResponse.stringOf("items[i].name");
        String bucket = jsonResponse.stringOf("items[i].bucket");
        String generation = jsonResponse.stringOf("items[i].generation");
        String metageneration = jsonResponse.stringOf("items[i].metageneration");
        String contentType = jsonResponse.stringOf("items[i].contentType");
        String timeCreated = jsonResponse.stringOf("items[i].timeCreated");
        String updated = jsonResponse.stringOf("items[i].updated");
        String storageClass = jsonResponse.stringOf("items[i].storageClass");
        String timeStorageClassUpdated = jsonResponse.stringOf("items[i].timeStorageClassUpdated");
        String size = jsonResponse.stringOf("items[i].size");
        String md5Hash = jsonResponse.stringOf("items[i].md5Hash");
        String mediaLink = jsonResponse.stringOf("items[i].mediaLink");
        String crc32c = jsonResponse.stringOf("items[i].crc32c");
        String etag = jsonResponse.stringOf("items[i].etag");
        i = i+1;
        }
  }
}

Sample JSON Response Body

{
  "kind": "storage#objects",
  "items": [
    {
      "kind": "storage#object",
      "id": "chilkat-bucket/hedgehogs.jpg/1540253106638630",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/hedgehogs.jpg",
      "name": "hedgehogs.jpg",
      "bucket": "chilkat-bucket",
      "generation": "1540253106638630",
      "metageneration": "1",
      "contentType": "image/jpeg",
      "timeCreated": "2018-10-23T00:05:06.638Z",
      "updated": "2018-10-23T00:05:06.638Z",
      "storageClass": "MULTI_REGIONAL",
      "timeStorageClassUpdated": "2018-10-23T00:05:06.638Z",
      "size": "48573",
      "md5Hash": "TE1U6fQlD6MOVGaQbwGinQ==",
      "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-bucket/o/hedgehogs.jpg?generation=1540253106638630&alt=media",
      "crc32c": "1B2d6g==",
      "etag": "CKaepPqhm94CEAE="
    },
    {
      "kind": "storage#object",
      "id": "chilkat-bucket/penguins.jpg/1540253106941673",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/penguins.jpg",
      "name": "penguins.jpg",
      "bucket": "chilkat-bucket",
      "generation": "1540253106941673",
      "metageneration": "1",
      "contentType": "image/jpeg",
      "timeCreated": "2018-10-23T00:05:06.941Z",
      "updated": "2018-10-23T00:05:06.941Z",
      "storageClass": "MULTI_REGIONAL",
      "timeStorageClassUpdated": "2018-10-23T00:05:06.941Z",
      "size": "777835",
      "md5Hash": "nTd7EM53jEk4s8fixjoimg==",
      "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-bucket/o/penguins.jpg?generation=1540253106941673&alt=media",
      "crc32c": "ixxYVw==",
      "etag": "COndtvqhm94CEAE="
    },
    {
      "kind": "storage#object",
      "id": "chilkat-bucket/starfish.jpg/1540253106637454",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/starfish.jpg",
      "name": "starfish.jpg",
      "bucket": "chilkat-bucket",
      "generation": "1540253106637454",
      "metageneration": "1",
      "contentType": "image/jpeg",
      "timeCreated": "2018-10-23T00:05:06.636Z",
      "updated": "2018-10-23T00:05:06.636Z",
      "storageClass": "MULTI_REGIONAL",
      "timeStorageClassUpdated": "2018-10-23T00:05:06.636Z",
      "size": "6229",
      "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==",
      "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-bucket/o/starfish.jpg?generation=1540253106637454&alt=media",
      "crc32c": "9RjgwQ==",
      "etag": "CI6VpPqhm94CEAE="
    }
  ]
}