Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Objective-C) Google Cloud Storage List Bucket ObjectsSee more Google Cloud Storage ExamplesList the objects in a Google Cloud Storage bucket. For more information, see https://cloud.google.com/storage/docs/json_api/v1/objects/list
#import <CkoStringBuilder.h> #import <CkoHttp.h> #import <CkoHttpResponse.h> #import <CkoJsonObject.h> #import <NSString.h> // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // This example uses a previously obtained access token having permission for the // scope "https://www.googleapis.com/auth/cloud-platform" // In this example, Get Google Cloud Storage OAuth2 Access Token, // the service account access token was saved to a text file. This example fetches the access token from the file.. CkoStringBuilder *sbToken = [[CkoStringBuilder alloc] init]; [sbToken LoadFile: @"qa_data/tokens/googleCloudStorageAccessToken.txt" charset: @"utf-8"]; CkoHttp *http = [[CkoHttp alloc] init]; http.AuthToken = [sbToken GetAsString]; // For more info see Cloud Storage Documentation - Objects: list // BOOL success = [http SetUrlVar: @"bucket" value: @"chilkat-ocean"]; success = [http SetUrlVar: @"PROJECT_NAME" value: @"ChilkatCloud"]; CkoHttpResponse *resp = [http QuickGetObj: @"https://www.googleapis.com/storage/v1/b/{$bucket}/o?project={$ChilkatCloud}"]; if (http.LastMethodSuccess == NO) { NSLog(@"%@",http.LastErrorText); return; } int responseCode = [resp.StatusCode intValue]; if (responseCode == 401) { NSLog(@"%@",resp.BodyStr); NSLog(@"%@",@"If invalid credentials, then it is likely the access token expired."); NSLog(@"%@",@"Your app should automatically fetch a new access token and re-try."); return; } NSLog(@"%@%d",@"Response code: ",responseCode); NSLog(@"%@",@"Response body"); CkoJsonObject *json = [[CkoJsonObject alloc] init]; success = [json Load: resp.BodyStr]; json.EmitCompact = NO; NSLog(@"%@",[json Emit]); // A response code = 200 indicates success, and the response body contains JSON such as this: // { // "kind": "storage#objects", // "items": [ // { // "kind": "storage#object", // "id": "chilkat-ocean/file with space char.jpg/1555776690752366", // "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/file%20with%20space%20char.jpg", // "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/file%20with%20space%20char.jpg?generation=1555776690752366&alt=media", // "name": "file with space char.jpg", // "bucket": "chilkat-ocean", // "generation": "1555776690752366", // "metageneration": "1", // "contentType": "image/jpeg", // "storageClass": "MULTI_REGIONAL", // "size": "6229", // "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==", // "crc32c": "9RjgwQ==", // "etag": "CO6WguiH3+ECEAE=", // "timeCreated": "2019-04-20T16:11:30.752Z", // "updated": "2019-04-20T16:11:30.752Z", // "timeStorageClassUpdated": "2019-04-20T16:11:30.752Z" // }, // { // "kind": "storage#object", // // "id": "chilkat-ocean/penguins.jpg/1555775749593296", // "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/penguins.jpg", // "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/penguins.jpg?generation=1555775749593296&alt=media", // "name": "penguins.jpg", // "bucket": "chilkat-ocean", // "generation": "1555775749593296", // "metageneration": "1", // "contentType": "image/jpeg", // "storageClass": "MULTI_REGIONAL", // "size": "777835", // "md5Hash": "nTd7EM53jEk4s8fixjoimg==", // "crc32c": "ixxYVw==", // "etag": "CNCxnqeE3+ECEAE=", // "timeCreated": "2019-04-20T15:55:49.593Z", // "updated": "2019-04-20T15:55:49.593Z", // "timeStorageClassUpdated": "2019-04-20T15:55:49.593Z" // }, // { // "kind": "storage#object", // "id": "chilkat-ocean/starfish.jpg/1628958192639968", // "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/starfish.jpg", // "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/starfish.jpg?generation=1628958192639968&alt=media", // "name": "starfish.jpg", // "bucket": "chilkat-ocean", // "generation": "1628958192639968", // "metageneration": "1", // "contentType": "image/jpeg", // "storageClass": "MULTI_REGIONAL", // "size": "6229", // "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==", // "crc32c": "9RjgwQ==", // "etag": "COC/tJP2sPICEAE=", // "timeCreated": "2021-08-14T16:23:12.660Z", // "updated": "2021-08-14T16:23:12.660Z", // "timeStorageClassUpdated": "2021-08-14T16:23:12.660Z" // } // ] // } // Use this online tool to generate parsing code from sample JSON: // Generate Parsing Code from JSON NSString *id = 0; NSString *selfLink = 0; NSString *mediaLink = 0; NSString *name = 0; NSString *bucket = 0; NSString *generation = 0; NSString *metageneration = 0; NSString *contentType = 0; NSString *storageClass = 0; NSString *size = 0; NSString *md5Hash = 0; NSString *crc32c = 0; NSString *etag = 0; NSString *timeCreated = 0; NSString *updated = 0; NSString *timeStorageClassUpdated = 0; NSString *kind = [json StringOf: @"kind"]; int i = 0; int count_i = [[json SizeOfArray: @"items"] intValue]; while (i < count_i) { json.I = [NSNumber numberWithInt: i]; kind = [json StringOf: @"items[i].kind"]; id = [json StringOf: @"items[i].id"]; selfLink = [json StringOf: @"items[i].selfLink"]; mediaLink = [json StringOf: @"items[i].mediaLink"]; name = [json StringOf: @"items[i].name"]; bucket = [json StringOf: @"items[i].bucket"]; generation = [json StringOf: @"items[i].generation"]; metageneration = [json StringOf: @"items[i].metageneration"]; contentType = [json StringOf: @"items[i].contentType"]; storageClass = [json StringOf: @"items[i].storageClass"]; size = [json StringOf: @"items[i].size"]; md5Hash = [json StringOf: @"items[i].md5Hash"]; crc32c = [json StringOf: @"items[i].crc32c"]; etag = [json StringOf: @"items[i].etag"]; timeCreated = [json StringOf: @"items[i].timeCreated"]; updated = [json StringOf: @"items[i].updated"]; timeStorageClassUpdated = [json StringOf: @"items[i].timeStorageClassUpdated"]; i = i + 1; } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.