Objective-C Google Cloud Storage: List Buckets

Back to Index

Retrieves a list of buckets for a given project.

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

CURL Command

curl -X GET https://www.googleapis.com/storage/v1/b \
    --header "Authorization: Bearer CLOUD_STORAGE_TOKEN" \
    --data "project=MY_CLOUD_STORAGE_PROJECT"

Objective-C Example

#import <CkoRest.h>
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <NSString.h>

CkoRest *rest = [[CkoRest alloc] init];
BOOL success;

//  URL: https://www.googleapis.com/storage/v1/b
BOOL bTls = YES;
int port = 443;
BOOL bAutoReconnect = YES;
success = [rest Connect: @"www.googleapis.com" port: [NSNumber numberWithInt: port] tls: bTls autoReconnect: bAutoReconnect];
if (success != YES) {
    NSLog(@"%@%d",@"ConnectFailReason: ",[rest.ConnectFailReason intValue]);
    NSLog(@"%@",rest.LastErrorText);
    return;
}

[rest AddQueryParam: @"project" value: @"MY_CLOUD_STORAGE_PROJECT"];

[rest AddHeader: @"Authorization" value: @"Bearer CLOUD_STORAGE_TOKEN"];

CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
success = [rest FullRequestNoBodySb: @"GET" uriPath: @"/storage/v1/b" sb: sbResponseBody];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

int respStatusCode = [rest.ResponseStatusCode intValue];
if (respStatusCode >= 400) {
    NSLog(@"%@%d",@"Response Status Code = ",respStatusCode);
    NSLog(@"%@",@"Response Header:");
    NSLog(@"%@",rest.ResponseHeader);
    NSLog(@"%@",@"Response Body:");
    NSLog(@"%@",[sbResponseBody GetAsString]);
    return;
}

CkoJsonObject *jsonResponse = [[CkoJsonObject alloc] init];
[jsonResponse LoadSb: sbResponseBody];

//  See the Online Tool for Generating JSON Parse Code
int i;
int count_i;

NSString *kind = [jsonResponse StringOf: @"kind"];
i = 0;
count_i = [[jsonResponse SizeOfArray: @"items"] intValue];
while (i < count_i) {
    jsonResponse.I = [NSNumber numberWithInt: i];
    kind = [jsonResponse StringOf: @"items[i].kind"];
    NSString *id = [jsonResponse StringOf: @"items[i].id"];
    NSString *selfLink = [jsonResponse StringOf: @"items[i].selfLink"];
    NSString *projectNumber = [jsonResponse StringOf: @"items[i].projectNumber"];
    NSString *name = [jsonResponse StringOf: @"items[i].name"];
    NSString *timeCreated = [jsonResponse StringOf: @"items[i].timeCreated"];
    NSString *updated = [jsonResponse StringOf: @"items[i].updated"];
    NSString *metageneration = [jsonResponse StringOf: @"items[i].metageneration"];
    NSString *location = [jsonResponse StringOf: @"items[i].location"];
    NSString *storageClass = [jsonResponse StringOf: @"items[i].storageClass"];
    NSString *etag = [jsonResponse StringOf: @"items[i].etag"];
    i = i + 1;
}

Sample JSON Response Body

{
  "kind": "storage#buckets",
  "items": [
    {
      "kind": "storage#bucket",
      "id": "chilkat-bucket",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket",
      "projectNumber": "5332332985",
      "name": "chilkat-bucket",
      "timeCreated": "2018-10-23T00:04:44.507Z",
      "updated": "2018-10-23T00:04:44.507Z",
      "metageneration": "1",
      "location": "US",
      "storageClass": "MULTI_REGIONAL",
      "etag": "CAE="
    },
    {
      "kind": "storage#bucket",
      "id": "chilkat-images",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-images",
      "projectNumber": "5332332985",
      "name": "chilkat-images",
      "timeCreated": "2018-10-23T11:24:43.000Z",
      "updated": "2018-10-23T11:24:43.000Z",
      "metageneration": "1",
      "location": "US",
      "storageClass": "MULTI_REGIONAL",
      "etag": "CAE="
    }
  ]
}