0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

bash ( curl + jq ) だけで Azure Blob Storage を扱う

Posted at

bash ( curl + jq ) だけで Azure Blob Storage を扱う

概要

  • コンテナイメージのサイズを減らす目的だった
  • SAS の HMAC-SHA256 が至難の業だった
  • やっと動いた後で気がついたが、rclone をインストールしてもサイズはさほど変わらない

コード

#!/bin/bash

set -eu

# Azure Storage Account の情報
account_name="your_storage_account_name"
account_key="your_storage_account_key"
container_name="your_container_name"
blob_name="your_blob_name"
file_path="local_file_path_to_upload.txt"
destination_path="local_destination_path_to_download.txt"

# 有効期限を計算
expiry=$(date -u -d "30 minutes" '+%Y-%m-%dT%H:%MZ')

# リソースの情報
canonicalizedResource="/blob/${account_name}/${container_name}/${blob_name}"

permission=rcd
version=2018-11-09
resource=b

# 署名文字列を構築
string_to_sign="${permission}\n\n${expiry}\n${canonicalizedResource}\n\n\n\n${version}\n${resource}\n\n\n\n\n\n"

# HMAC-SHA256を計算
signature=$(printf "$string_to_sign" | iconv -t utf8 | openssl dgst -sha256 -hmac $(echo ${account_key} | base64 -d) -binary | base64 -w 0)

# SAS トークンを構築
signature=$(echo -n $signature | jq -Rr '@uri')
sas_token="sv=${version}&se=${expiry}&sr=${resource}&sp=${permission}&sig=${signature}"

# REST API URLを構築
url="https://${account_name}.blob.core.windows.net/${container_name}/${blob_name}?${sas_token}"


# curl を使用してファイルをアップロード
curl -X PUT \
  -H "x-ms-blob-type: BlockBlob" \
  -H "Content-Type: application/octet-stream" \
  -T "${file_path}" "${url}"

# curl を使用してファイルをダウンロード
curl -o "${destination_path}" "${url}"
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?