Skip to content

Commit

Permalink
Merge pull request #167 from NeowayLabs/addContainerList
Browse files Browse the repository at this point in the history
Add functions to list available blobs
  • Loading branch information
katcipis authored Nov 28, 2017
2 parents bc4dab5 + 8e0119a commit 9d16a83
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
56 changes: 56 additions & 0 deletions azure/storage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,62 @@ fn azure_storage_container_exists_by_resgroup(containername, accountname, resgro
return azure_storage_container_exists($containername, $accountname, $accountkey)
}

# azure_storage_blob_list lists all blobs in a container.
# Returns a list and an empty error string on success, otherwise
# it will return an empty list and a non empty error string.
fn azure_storage_blob_list(
containername,
accountname,
accountkey,
numresults
) {

output, status <= (
az storage blob list
--container-name $containername
--account-name $accountname
--account-key $accountkey
--num-results $numresults
>[2=1]
)

if $status != "0" {
return (), format("error[%s] listing blobs", $output)
}

namesraw, status <= echo $output | jq -r ".[].name" >[2=1]
if $status != "0" {
return (), format("error[%s] parsing[%s]", $namesraw, $output)
}

names <= split($namesraw, "\n")

return $names, ""
}

# azure_storage_blob_list_by_resgroup does the same azure_storage_blob_list
# but using the first account key available.
fn azure_storage_blob_list_by_resgroup(
containername,
accountname,
resgroup,
numresults
) {
accountkey, err <= _azure_storage_account_get_key_value($accountname, $resgroup)
if $err != "" {
return (), $err
}

res, err <= azure_storage_blob_list(
$containername,
$accountname,
$accountkey,
$numresults
)

return $res, $err
}

# azure_storage_blob_exists checks if a blob exists.
# Returns "0" if it already exists (success), "1" otherwise.
fn azure_storage_blob_exists(containername, accountname, accountkey, blobpath) {
Expand Down
80 changes: 80 additions & 0 deletions examples/azure/blob/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env nash

import io

import klb/azure/login
import klb/azure/group
import klb/azure/storage

import config

azure_login()

fn aborterr(err, details) {
if $err != "" {
io_println("error[%s] %s", $err, $details)
exit("1")
}
}

status <= azure_group_exists($group)
if $status != "0" {
io_println("no resgroup[%s], creating", $group)
azure_group_create($group, $location)
}

status <= azure_storage_account_exists($account, $group)
if $status != "0" {
io_println("no storage account[%s], creating", $account)
err <= azure_storage_account_create_blob(
$account,
$group,
$location,
$sku,
$tier
)
aborterr($err, "creating storage account")
}

status <= azure_storage_container_exists_by_resgroup(
$container,
$account,
$group
)

if $status != "0" {
io_println("no storage container[%s], creating", $container)
err <= azure_storage_container_create_by_resgroup(
$container,
$account,
$group,
)
aborterr($err, "creating storage container")
}

filename <= mktemp

echo "klb example tests" > $filename

echo "uploading file"
err <= azure_storage_blob_upload_by_resgroup(
$container,
$account,
$group,
$filename,
$filename
)

aborterr($err, "uploading file[%s]")
rm -f $filename

echo "uploaded file, listing files"
res, err <= azure_storage_blob_list_by_resgroup(
$container,
$account,
$group,
"100"
)
aborterr($err, "listing files")

io_println("listed files: %s", $res)
9 changes: 9 additions & 0 deletions examples/azure/blob/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env nash

import klb/azure/login
import klb/azure/group

import config

azure_login()
azure_group_delete($group)
6 changes: 6 additions & 0 deletions examples/azure/blob/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
group = "klb-examples-blob"
account = "klbexamplesblob"
container = "klb-examples-blob-container"
location = "eastus2"
sku = "Standard_LRS"
tier = "Cool"

0 comments on commit 9d16a83

Please sign in to comment.