Skip to content

Instantly share code, notes, and snippets.

@reubenmiller
Last active May 6, 2024 07:13
Show Gist options
  • Save reubenmiller/d71cd6c6ec7ff38a4f46872a8bb611fa to your computer and use it in GitHub Desktop.
Save reubenmiller/d71cd6c6ec7ff38a4f46872a8bb611fa to your computer and use it in GitHub Desktop.
thin-edge.io upload file to Cumulocity IoT via the local c8y proxy

README

Summary

Upload files to Cumulocity IoT by creating an event and uploading a given file to it.

Usage

USAGE
    ./c8y_upload_file.sh [OPTIONS] <FILE...>

FLAGS
    --identity <name>           External identity. Defaults to the thin-edge.io device.id (e.g. the main device)
    --help, -h                  Show this help

Examples

./c8y_upload_file.sh ./mylocalfile.txt
# Upload mylocalfile.txt to the main device

./c8y_upload_file.sh ./mylocalfile.txt ./otherfile.zip
# Upload multiple files to the main device (one event per file)

./c8y_upload_file.sh ./mylocalfile.txt --identity "my_child_device01"
# Upload mylocalfile.txt to a child device, though you need to know the full c8y child external identity
#!/usr/bin/env bash
set -e
################################################################################
# Cumulocity Proxy service setup
# Auto detects the thin-edge.io proxy settings to use the http or https endpoint
################################################################################
DEVICE_ID=$(tedge config get device.id)
C8Y_PROXY_HOST=$(tedge config get c8y.proxy.client.host)
C8Y_PROXY_PORT=$(tedge config get c8y.proxy.client.port)
C8Y_PROXY_CERT_PATH=$(tedge config get c8y.proxy.cert_path)
C8Y_PROXY_KEY_PATH=$(tedge config get c8y.proxy.key_path)
C8Y_PROXY_CA_PATH=$(tedge config get c8y.proxy.ca_path)
_curl_via_http() {
API_URL="${1#/}"
shift
curl -f -H "Accept: application/json" "http://${C8Y_PROXY_HOST}:${C8Y_PROXY_PORT}/c8y/${API_URL}" "$@"
}
_curl_via_https() {
API_URL="${1#/}"
shift
curl -f -H "Accept: application/json" "https://${C8Y_PROXY_HOST}:${C8Y_PROXY_PORT}/c8y/${API_URL}" --capath "$C8Y_PROXY_CA_PATH" --key "$C8Y_PROXY_KEY_PATH" --cert "$C8Y_PROXY_CERT_PATH" "$@"
}
c8y_api() {
"$_curl_cmd" "$@"
}
# Activate which curl command will be used for the c8y api calls (e.g. with or without local cert authentication)
_curl_cmd=_curl_via_http
if [ -n "$C8Y_PROXY_KEY_PATH" ] && [ -n "$C8Y_PROXY_CERT_PATH" ] && [ -n "$C8Y_PROXY_CA_PATH" ]; then
_curl_cmd=_curl_via_https
fi
################################
# API Commands
################################
# Get the managed object id from a given external identity
get_id_from_external_identity() {
EXTERNAL_IDENTITY="$1"
RESPONSE=$(c8y_api "identity/externalIds/c8y_Serial/$EXTERNAL_IDENTITY" --silent)
echo "$RESPONSE" | jq -r '.managedObject.id'
}
# Upload file by first creating an event, then attaching the given binary to it
upload_file() {
FILE="$1"
SOURCE_EXTERNAL_IDENTITY="$2"
echo "Uploading file: file=$FILE, external_id=$SOURCE_EXTERNAL_IDENTITY" >&2
FILE_NAME=$(basename "$FILE")
SOURCE_ID=$(get_id_from_external_identity "$SOURCE_EXTERNAL_IDENTITY")
TIME=$(date -Iseconds)
# Create c8y event (and get the id of the created event)
RESPONSE=$(
c8y_api "event/events" \
-XPOST \
--silent \
-H "Content-Type: application/json" \
-d "{
\"source\": {
\"id\": \"$SOURCE_ID\"
},
\"text\":\"Custom file upload: $FILE_NAME\",
\"type\":\"file_upload_$FILE_NAME\",
\"time\":\"$TIME\"
}"
)
EVENT_ID=$(echo "$RESPONSE" | jq -r '.id')
# Upload/attached binary to an event
RESPONSE=$(c8y_api "event/events/$EVENT_ID/binaries" -XPOST -F file="@$FILE")
echo "$RESPONSE"
echo "Successfully uploaded file" >&2
}
################################
# main
################################
help() {
cat << EOT
Upload files to Cumulocity IoT by creating an event and uploading a given file to it.
USAGE
$0 [OPTIONS] <FILE...>
FLAGS
--identity <name> External identity. Defaults to the thin-edge.io device.id (e.g. the main device)
--help, -h Show this help
EXAMPLES
$0 ./mylocalfile.txt
# Upload mylocalfile.txt to the main device
$0 ./mylocalfile.txt ./otherfile.zip
# Upload multiple files to the main device (one event per file)
$0 ./mylocalfile.txt --identity "my_child_device01"
# Upload mylocalfile.txt to a child device, though you need to know the full c8y child external identity
EOT
}
TARGET_IDENTITY="$DEVICE_ID"
FILES=()
while [ $# -gt 0 ]; do
case "$1" in
--identity)
TARGET_IDENTITY="$2"
shift
;;
--help|-h)
help
exit 0
;;
--*|-*)
echo "Unknown flag. $1" >&2
exit 1
;;
*)
FILES+=(
"$1"
)
;;
esac
shift
done
if [ "${#FILES[@]}" -lt 1 ]; then
echo "Missing file/s. You need to provided at least 1 file path" >&2
help
exit 1
fi
for file in "${FILES[@]}"; do
upload_file "$file" "$TARGET_IDENTITY"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment