LoginSignup
9
1

More than 3 years have passed since last update.

IBM Cloud Object Storage(ICOS)にcurl+HMACでアクセスする

Posted at

はじめに

IBM Cloud Object Storage(ICOS)に対する認証方式にはIAMとHMACの2通りがあることは、ここに記載されているとおりです。

HMACの場合は大抵はなんらかのツール(コマンドラインだとIBM Cloud CLIとかAWS CLIとかs3fsなど。GUI系ツールだとCyberduckとかCloudberryとかWinSCPなど)を使うことが前提となっており、IBM Cloud docsにもHMACでのcurl呼び出しは記載はありませんでした。IAM前提でのcurlの利用方法はあるんですけどね。

というので、HMACを利用したcurlによるICOSへのアクセス方法(とりあえずデータ取得)についてサンプルスクリプトを書いてみました。

結論

こんな感じ。環境変数部分は自分で書き換えてください。

icos_access.sh
#!/bin/sh

#Environment
icosendpoint=s3.private.jp-tok.cloud-object-storage.appdomain.cloud
bucket=xxxxx
filename=xxxxx
ACCESS_KEY=xxxxxxxxx
SECRET_KEY=xxxxxxxxx

#Execution
resource="/${bucket}/${filename}"
contentType="text/html"
dateValue=$(date -R)
stringToSign="GET\n\n${contentType}\n${dateValue}\n${resource}"
signature=$(echo -en ${stringToSign} | openssl sha1 -hmac ${SECRET_KEY} -binary | base64)
curl -sO "https://${icosendpoint}/${bucket}/${filename}" \
     -H "Authorization: AWS ${ACCESS_KEY}:${signature}" \
     -H "Date: ${dateValue}" \
     -H "Content-Type: ${contentType}"
実行例(上記スクリプトのファイル名にindex.htmlを指定)
$ rm index.html
$ ./icos_access.sh
$ cat index.html
Hello World!
9
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
1