LoginSignup
0
0

More than 3 years have passed since last update.

Azure Storage にストレージされているコンテナをAzure Functionsを使って取得する

Posted at

コンテナの状態をみて最終更新日時から1週間たっていたら定期削除を行いたい。
まずはコンテナを取得してみる。
自分の備忘録として残す。

Functions

Azure PortalからAzure Storageを作成しておく(後で使うので接続文字列コピーしておく)。
Azure Portalから発行先のAzure Functionsを作成しておく。
新規プロジェクト作成する。

ソリューションのパッケージの監理

以下インストールしておく。

  • Microsoft.Azure.Storage.Blob

変数用意

        private readonly string storageConnectionString;
        private CloudStorageAccount storageAccount;
        private CloudBlobClient blobClient;

環境変数をセット。

今回はAzure Blob Storageに接続したいので、作成したBlob Storageの接続文字列を設定する。

環境変数はAzure Portalから作成したFunctionsのアプリケーション設定に値をセットすることで設定できる。

public Function1()
        {
            storageConnectionString = Environment.GetEnvironmentVariable("ConnectionStrings:StorageConnectionString", EnvironmentVariableTarget.Process);
        }
local.setting.json
{
    "ConnectionStrings": {
        "StorageConnectionString": "接続文字列"
    }
}

デプロイ先のFunction Appのアプリケーション設定で接続文字列の設定をしておく

Blobに接続する

セットした接続文字列を使用して接続する関数を用意。

public void SetUp()
        {
            storageAccount = CloudStorageAccount.Parse(storageConnectionString);
            blobClient = storageAccount.CreateCloudBlobClient();
        }

コンテナリストを取得する

コンテナリストを取得する。

private static async Task<IEnumerable<CloudBlobContainer>> GetListContainersAsync(CloudBlobClient blobClient)
        {
            BlobContinuationToken continuationToken = null;
            var containers = new List<CloudBlobContainer>();

            do
            {
                ContainerResultSegment response = await BlobClient.ListContainersSegmentedAsync(continuationToken);
                continuationToken = response.ContinuationToken;
                containers.AddRange(response.Results);

            } while (continuationToken != null);

            return containers;
        }

実際のコード

下記コードによりコンテナリストを取得できる。
これにより、Storage BlobにストレージされているBlobの状態を参照できる。

状態を参照することにより、Blobの定期削除などが行えるようになる。

public class Function1
    {
        private readonly string storageConnectionString;
        private CloudStorageAccount storageAccount;
        private CloudBlobClient blobClient;

        public Function1()
        {
            storageConnectionString = Environment.GetEnvironmentVariable("ConnectionStrings:StorageConnectionString", EnvironmentVariableTarget.Process);
        }

        [FunctionName("Function1")]
        public async Task RunAsync([TimerTrigger("0 0 0 * * *")]TimerInfo myTimer, ILogger log)
        {
            SetUp();

            //コンテナリストを取得
            var containers = await GetListContainersAsync(blobClient);
        }

        public void SetUp()
        {
            storageAccount = CloudStorageAccount.Parse(storageConnectionString);
            blobClient = storageAccount.CreateCloudBlobClient();
        }

        private static async Task<IEnumerable<CloudBlobContainer>> GetListContainersAsync(CloudBlobClient blobClient)
        {
            BlobContinuationToken continuationToken = null;
            var containers = new List<CloudBlobContainer>();

            do
            {
                ContainerResultSegment response = await blobClient.ListContainersSegmentedAsync(continuationToken);
                continuationToken = response.ContinuationToken;
                containers.AddRange(response.Results);

            } while (continuationToken != null);

            return containers;
        }
    }

最後

次回、コンテナの削除処理のほうををやってみる。

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