Bash(shell)

データストレージ API

Bash(Shell)
sample.sh
#!/bin/bash

# This is a sample shell(bash) script code for IIJ IoT Data Storage Web API.
# For more information please refer to the manual.

readonly BASE_URL='https://s3api.iot.iij.jp'  # not edit
readonly HOST='s3api.iot.iij.jp'              # not edit

access_key=''                                  # your access key
secret_key=''                                  # your secret key
content_md5=''                                 # if you upload file, you may write.
content_type=''                                # not edit
_date=$(date -u +'%a, %d %b %Y %H:%M:%S GMT')  # now date
payload=''                                     # if you needed. We expect xml.
content_length=${#payload}                     # request body length
http_verb='GET'                                # or POST, PUT, DELETE, HEAD
bucket_name='/sample'                          # specify your bucket
query=''                                       # if you needed. e.g. ?id=hoge
canonicalized_headers=''                       # if you use optional headers, you may write.
canonicalized_resource=${bucket_name}${query}

string_to_sign="${http_verb}
${content_md5}
${content_type}
${_date}
${canonicalized_headers}${bucket_name}"

# create signature
signature=$(echo -en "${string_to_sign}" | openssl dgst -sha1 -binary -hmac "${secret_key}" | base64)

headers=(
  "Authorization: IIJGIO ${access_key}:${signature}"
  "Content-Type: ${content_type}"
  "Content-Length: ${content_length}"
  "Content-MD5: ${content_md5}"
  "Date: ${_date}"
  "Host: ${HOST}"
)
str_headers=""
for h in "${headers[@]}"; do
  str_headers+="-H \"${h}\" "
done

eval curl -v -X ${http_verb} ${str_headers} ${BASE_URL}${canonicalized_resource}
# eval curl -i -X ${http_verb} ${str_headers} ${BASE_URL}${canonicalized_resource} -d "${payload}"