コード例 #1
0
        /// <inheritdoc/>
        public async Task <BlobContainerClient> GetBlobClient(string org, string environment)
        {
            string key = $"{org}-{environment}";

            if (_clients.TryGetValue(key, out BlobContainerClient client))
            {
                return(client);
            }

            try
            {
                _accountConfig.TryGetValue(key, out StorageAccountConfig config);

                if (string.IsNullOrEmpty(config.AccountKey))
                {
                    StorageManagementClient      storageManagementClient = new StorageManagementClient(config.SubscriptionId, _accessTokenService.GetCredential());
                    StorageAccountListKeysResult res = await storageManagementClient.StorageAccounts.ListKeysAsync(config.ResourceGroup, config.AccountName);

                    config.AccountKey = res.Keys.Where(k => k.KeyName == "key1").FirstOrDefault().Value;
                    _accountConfig[key].AccountKey = config.AccountKey;
                }

                string blobEndpoint = $"https://{config.AccountName}.blob.core.windows.net/";

                BlobServiceClient commonBlobClient = new BlobServiceClient(new Uri(blobEndpoint), new StorageSharedKeyCredential(config.AccountName, config.AccountKey));
                client = commonBlobClient.GetBlobContainerClient(config.Container);

                _clients.TryAdd(key, client);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return(client);
        }