このガイドでは、アプリケーションをテストするときに、Azure AI サービスの資格情報について環境変数を設定し、それを取得する方法について説明します。
重要
Microsoft Entra 認証と Azure リソースのマネージド ID を併用して、クラウドで実行されるアプリケーションに資格情報を格納しないようにすることをお勧めします。
API キーは慎重に使用してください。 API キーは、コード内に直接含めないようにし、絶対に公開しないでください。 API キーを使用する場合は、Azure Key Vault に安全に格納し、キーを定期的にローテーションし、ロールベースのアクセス制御とネットワーク アクセス制限を使用して Azure Key Vault へのアクセスを制限してください。 アプリで API キーを安全に使用する方法の詳細については、Azure Key Vault を使用した API キーに関する記事を参照してください。
using static System.Environment;
class Program
{
static void Main()
{
// Get the named env var, and assign it to the value variable
var value =
GetEnvironmentVariable(
"ENVIRONMENT_VARIABLE_KEY");
}
}
#include <iostream>
#include <stdlib.h>
std::string GetEnvironmentVariable(const char* name);
int main()
{
// Get the named env var, and assign it to the value variable
auto value = GetEnvironmentVariable("ENVIRONMENT_VARIABLE_KEY");
}
std::string GetEnvironmentVariable(const char* name)
{
#if defined(_MSC_VER)
size_t requiredSize = 0;
(void)getenv_s(&requiredSize, nullptr, 0, name);
if (requiredSize == 0)
{
return "";
}
auto buffer = std::make_unique<char[]>(requiredSize);
(void)getenv_s(&requiredSize, buffer.get(), requiredSize, name);
return buffer.get();
#else
auto value = getenv(name);
return value ? value : "";
#endif
}
import java.lang.*;
public class Program {
public static void main(String[] args) throws Exception {
// Get the named env var, and assign it to the value variable
String value =
System.getenv(
"ENVIRONMENT_VARIABLE_KEY")
}
}
// Get the named env var, and assign it to the value variable
NSString* value =
[[[NSProcessInfo processInfo]environment]objectForKey:@"ENVIRONMENT_VARIABLE_KEY"];