Lua
Lua
Google Cloud Storage List Buckets
See more Google Cloud Storage Examples
Demonstrates how to retrieve a list of buckets for a given project.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- 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"
local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/googleCloudStorage.json")
if not success then
print(jsonToken.last_error_text)
return
end
local http = chilkat.Http.new()
http.auth_token = jsonToken:string_of("access_token")
-- For more info see Cloud Storage Documentation - Buckets: list
--
success = http:set_url_var("PROJECT_ID", "chilkattest-1050")
local resp = chilkat.HttpResponse.new()
success = http:http_no_body("GET", "https://www.googleapis.com/storage/v1/b?project={$PROJECT_ID}", resp)
if not success then
print(http.last_error_text)
return
end
local responseCode = resp.status_code
if responseCode == 401 then
print(resp.body_str)
print("If invalid credentials, then it is likely the access token expired.")
print("Your app should automatically fetch a new access token and re-try.")
return
end
print("Response code: " .. responseCode)
print("Response body")
local json = chilkat.JsonObject.new()
success = json:load(resp.body_str)
json.emit_compact = false
print(json:emit())
-- A response code = 200 indicates success, and the response body contains JSON such as this:
-- {
-- "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",
-- "iamConfiguration": {
-- "bucketPolicyOnly": {
-- "enabled": false
-- }
-- },
-- "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",
-- "iamConfiguration": {
-- "bucketPolicyOnly": {
-- "enabled": false
-- }
-- },
-- "location": "US",
-- "storageClass": "MULTI_REGIONAL",
-- "etag": "CAE="
-- }
-- ]
-- }
-- Use this online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
local id = ""
local selfLink = ""
local projectNumber = ""
local name = ""
local timeCreated = ""
local updated = ""
local metageneration = ""
local iamConfigurationBucketPolicyOnlyEnabled = false
local location = ""
local storageClass = ""
local etag = ""
local kind = json:string_of("kind")
local i = 0
local count_i = json:size_of_array("items")
while i < count_i do
json.i = i
kind = json:string_of("items[i].kind")
id = json:string_of("items[i].id")
selfLink = json:string_of("items[i].selfLink")
projectNumber = json:string_of("items[i].projectNumber")
name = json:string_of("items[i].name")
timeCreated = json:string_of("items[i].timeCreated")
updated = json:string_of("items[i].updated")
metageneration = json:string_of("items[i].metageneration")
iamConfigurationBucketPolicyOnlyEnabled = json:bool_of("items[i].iamConfiguration.bucketPolicyOnly.enabled")
location = json:string_of("items[i].location")
storageClass = json:string_of("items[i].storageClass")
etag = json:string_of("items[i].etag")
i = i + 1
end