Utilizar variáveis de ambiente com serviços de IA do Azure
Artigo
Este guia mostra-lhe como definir e obter variáveis de ambiente para processar as credenciais de subscrição dos serviços de IA do Azure de uma forma mais segura quando testa aplicações.
Definir uma variável de ambiente
Para definir variáveis de ambiente, utilize um dos seguintes comandos, em que é a ENVIRONMENT_VARIABLE_KEY chave nomeada e value é o valor armazenado na variável de ambiente.
Utilize o seguinte comando para criar e atribuir uma variável de ambiente persistente, dado o valor de entrada.
:: Assigns the env var to the value
setx ENVIRONMENT_VARIABLE_KEY "value"
Numa nova instância da Linha de Comandos, utilize o seguinte comando para ler a variável de ambiente.
:: Prints the env var value
echo %ENVIRONMENT_VARIABLE_KEY%
Utilize o seguinte comando para criar e atribuir uma variável de ambiente persistente, dado o valor de entrada.
# Assigns the env var to the value
[System.Environment]::SetEnvironmentVariable('ENVIRONMENT_VARIABLE_KEY', 'value', 'User')
Numa nova instância do Windows PowerShell, utilize o seguinte comando para ler a variável de ambiente.
# Prints the env var value
[System.Environment]::GetEnvironmentVariable('ENVIRONMENT_VARIABLE_KEY')
Utilize o seguinte comando para criar e atribuir uma variável de ambiente persistente, dado o valor de entrada.
# Assigns the env var to the value
echo export ENVIRONMENT_VARIABLE_KEY="value" >> /etc/environment && source /etc/environment
Numa nova instância do Bash, utilize o seguinte comando para ler a variável de ambiente.
# Prints the env var value
echo "${ENVIRONMENT_VARIABLE_KEY}"
# Or use printenv:
# printenv ENVIRONMENT_VARIABLE_KEY
Dica
Depois de definir uma variável de ambiente, reinicie o ambiente de desenvolvimento integrado (IDE) para garantir que as variáveis de ambiente adicionadas recentemente estão disponíveis.
Obter uma variável de ambiente
Para utilizar uma variável de ambiente no seu código, tem de ser lida na memória. Utilize um dos seguintes fragmentos de código, consoante o idioma que estiver a utilizar. Estes fragmentos de código demonstram como obter uma variável de ambiente dada e ENVIRONMENT_VARIABLE_KEY atribuir o valor a uma variável de programa chamada value.
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");
}
}
Para obter mais informações, consulte getenv_s e getenv.
#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
}
Para obter mais informações, consulte System.getenv.
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")
}
}
Para obter mais informações, consulte process.env.
// Get the named env var, and assign it to the value variable
const value =
process.env.ENVIRONMENT_VARIABLE_KEY;
import os
# Get the named env var, and assign it to the value variable
value = os.environ['ENVIRONMENT_VARIABLE_KEY']
Para obter mais informações, consulte environment.
// Get the named env var, and assign it to the value variable
NSString* value =
[[[NSProcessInfo processInfo]environment]objectForKey:@"ENVIRONMENT_VARIABLE_KEY"];