Adding a new AWS profile:
```bash
aws-vault add my-profile
```
Executing an AWS CLI command with a specific profile:
```bash
aws-vault exec my-profile -- aws s3 ls
```
Listing stored profiles:
```bash
aws-vault list
```
Using aws-vault in a Python script:
```python
import subprocess
def aws_vault_exec(profile, command):
result = subprocess.run(['aws-vault', 'exec', profile, '--'] + command, capture_output=True, text=True)
print(result.stdout)
if result.stderr:
print(f"Error: {result.stderr}")
# Example usage: list S3 buckets with a specific profile
aws_vault_exec('my-profile', ['aws', 's3', 'ls'])
```
Using aws-vault in a Java program:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class AwsVaultExample {
public static void awsVaultExec(String profile, String[] command) {
String[] execCommand = new String[command.length + 3];
execCommand[0] = "aws-vault";
execCommand[1] = "exec";
execCommand[2] = profile;
execCommand[3] = "--";
System.arraycopy(command, 0, execCommand, 4, command.length);
try {
Process process = new ProcessBuilder(execCommand).start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
int exitCode = process.waitFor();
if (exitCode != 0) {
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while ((line = errorReader.readLine()) != null) {
System.err.println("Error: " + line);
}
errorReader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// Example usage: list S3 buckets with a specific profile
awsVaultExec("my-profile", new String[]{"aws", "s3", "ls"});
}
}
```