Azure Blob Storage を試した。
ライブラリは Microsoft Azure Client Library For Blob Storage を使う。
この Quick Start に従ってやってみた。
Azure Blob Storage を使うには az login してから、次のようなシェルスクリプトで resource group と storage account を用意します。
#!/bin/bash
RS=your-resource-group-name
L=eastus
SA=yourstoragename
az group create \
--name ${RS} \
--location ${L}
az storage account create \
--name ${SA} \
--resource-group ${RS} \
--location ${L}
--sku Standard_ZRS \
--encryption-services blob
sku の Standard_ZRS というのが --sku: not found と言われた。何か間違っているのだろう。
sku に対して次の文字列が使えるらしい:
--sku {Premium_LRS, Premium_ZRS, Standard_GRS, Standard_GZRS, Standard_LRS, Standard_RAGRS, Standard_RAGZRS, Standard_ZRS}
使う location によって使える使えない、とかがあるのだろうか?(不明)
とりあえず storage account 自体の作成には成功したので先に進む。
account name は使える文字数や文字の種類に制限があるので注意!
- Storage account name must be between 3 and 24 characters in length and use numbers and lower-case letters only.
location はもちろん別に eastus である必要はない。
使用した kotlin version:
$ kotlin -version
Kotlin version 2.0.10-release-540 (JRE 17.0.6+10-LTS)
それではコードを書いていきます。
// storage.main.kts
@file:Repository("https://repo1.maven.org/maven2/")
@file:DependsOn("com.azure:azure-storage-blob:12.29.0")
import java.io.File
import com.azure.storage.blob.BlobServiceClientBuilder
import com.azure.storage.common.StorageSharedKeyCredential
val storageAccountName = "yourstoragename"
val storageAccountKey = "xxx..."
val endpoint = "https://${storageAccountName}.blob.core.windows.net/"
val client = BlobServiceClientBuilder()
.endpoint(endpoint)
.credential(StorageSharedKeyCredential(storageAccountName, storageAccountKey))
.buildClient()
println(client)
storageAccountName と storageAccountKey は適切に設定します。
storageAccountKey は Azure Portal の 今作成した storage account の Overview から 左側のサイドバーの security + networking の Access Keys の画面で取得できます。
この段階でいったん実行してみます。
$ kotlin storage.main.kts
com.azure.storage.blob.BlobServiceClient@7be3baf2
BlobServiceClient インスタンスを得ることができました。
まずファイルを入れる前に blob storage に名前付きのコンテナをつくる必要があります。 ここではコンテナ名を your-container としたことにします。
val containerName = "your-container"
val containerClient = client.createBlobContainerIfNotExists(containerName)
先に用意した client に対して createBlobContainerIfNotExists をコンテナ名付きで呼ぶと containerClient を得ることができます。 メソッド名から想像がつきますが、該当コンテナが存在していない場合は生成してそのインスタンスを返してくれるメソッドです。
containerClient は com.azure.storage.blob.BlobContainerClient のインスタンスです。
名前付きの blob を作成して、その blob へファイルの内容をアップロードします。
事前に hello-world.txt を作成しておきましょう。
$ echo "Hello, World!" > hello-world.txt
ファイルをコンテナにアップロードするコードはこれ:
val blobName = "hello-world.txt"
val blobClient = containerClient.getBlobClient(blobName)
File("hello-world.txt").inputStream().buffered().use {
blobClient.upload(it, true)
}
blobClient は com.azure.storage.blob.BlobClient のインスタンス。
ここでは汎用性を考えて upload: (InputStream, Boolean): Unit に相当するメソッドを使っていますが、 ファイルをアップロードしたいだけならば uploadFromFile があります。ただし、試していない。
BlobClient の詳細はドキュメントを参照: https://learn.microsoft.com/en-us/java/api/com.azure.storage.blob.blobclient?view=azure-java-stable
これだけです。
File("downloaded_hello-world.txt").outputStream().buffered().use {
blobClient.downloadStream(it)
}
本当にアップロードしたのと同じ内容のファイルがダウンロードできたか確かめます。
$ cat downloaded_hello-world.txt
Hello, World!
できました。
実験が終わったらリソースを削除します。
#!/bin/bash
RS=your-resource-group-name
az group delete --name ${RS} --yes
以上です。