importSecrets
Description
Examples
Import secrets from a file into your MATLAB vault.
importSecrets("samplefile.env");Import secrets from a file. Read the contents of the input text file as a .env file.
importSecrets("samplefile.txt",FileType="env");
Input Arguments
Name of the file to read, specified as a string scalar or character vector. The file must contain the key-value pairs to set as variables using the key=value format, such as:
AWS_ACCESS_KEY_ID="my-aws-access-key-id-goes-here" AWS_SECRET_ACCESS_KEY="my-aws-secret-access-key-goes-here" username="your-username-goes-here"
.env file
format is the recommended plain text file format for importSecrets. A
.env file (dotenv) is a plain text file containing keys and their
corresponding values. By using a .env file you can separate sensitive
configuration data from code and provide different sets of configurations for different
workflows.Depending on the location of your file, filename can take on one
of these forms.
Location | Form | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| Current folder or folder on the MATLAB path | Specify the name of the file in
Example:
| ||||||||
File in a folder | If the file is not in the current folder or in a folder on the
MATLAB path, then specify the full or relative path name in
Example:
Example:
| ||||||||
| Internet URL | If the file is specified as an internet uniform resource locator
(URL), then Example:
| ||||||||
Remote Location | If the file is stored at a remote location, then
Based on the remote location,
For more information, see Work with Remote Data. Example:
|
importSecrets follows these rules when parsing the input file:
Read one key-value pair per line.
Skip empty lines and treat all contents of a line after a # symbol as a comment.
Allow key names that consist only of letter, number, underscore, dot, and hyphen characters.
Preserve text in quotes without adjustment, trim white space outside of quotes, and expand new lines from double-quoted values.
If the .env file contains duplicate entries,
importSecretswill import the last valid value.
Note
Security Considerations: Because
.env files are plain text, ensure the location and permissions of
your .env file reflect the security level of your workflow:
Your local machine is often the most secure place to store your file.
During code deployment, do not deploy a
.envfile containing your credentials.Do not check your
.envfiles into source code repositories. For example, in your repositories, include the.envfile extension in your.gitignore_globalfile to exclude.envfiles from all your repositories. For more information on how to configure Git to ignore files, see the GitHub page Ignoring Files.
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN, where Name is
the argument name and Value is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name in quotes.
Example: loadenv(filename,Overwrite=true) overwrites existing
secrets.
Overwrite existing secrets in your MATLAB vault with the same name, specified as a numeric or logical 1 (true) or 0 (false).
By default, importSecrets returns an error if the input file contains a secret name that already exists.
Example: Overwrite=true
Type of file, specified as one of these values:
"auto"— Automatically detect the file format of the input file from the extension specified infilename."env"— Read the contents of the input file as.env. If the file extension infilenameis not.env, you can specify the value ofFileTypeas"env"to read the contents of the input file as a.envfile.
Example: FileType="env"
Character encoding scheme associated with the file, specified as
"auto", "system", or a standard character
encoding scheme name.
If specified as
"auto",importSecretsdetects encoding from the file.If specified as
"system",importSecretsuses the default encoding of your system.
Example: Encoding="UTF-8" uses UTF-8 as the
encoding.
Example: Encoding="system" uses the system default
encoding.
Data Types: char | string
Expand variables, specified as a numeric or logical 1
(true) or 0 (false). By
default, importSecrets performs variable expansion in the form
${VAR}, a variable name. If you specify
ExpandVariables as false, importSecrets does
not perform any variable expansion.
When expanding variables, importSecrets looks for the variable
value first in the input .env file and then in the user
environment. If the variable value is not found in either location, the function looks
for a default value, which you can specify using the form
${VAR:-Default}. If none of these apply,
importSecrets reads the variable value as empty.
Example: ExpandVariables=true
More About
Your secrets are stored in your MATLAB vault. Your vault and secrets persist across MATLAB sessions.
Each secret consists of a name, value, and optional metadata.
Secret name – A unique case-sensitive text identifier for the secret. The secret name is stored unencrypted in your vault as a string scalar.
Secret value – A text value associated with the secret. The Secret Prompt dialog box, where you enter the secret value, supports copy-paste functionality. The secret value is stored encrypted in your vault using industry standard AES-256 encryption. The secret value is returned as a string scalar.
Secret metadata – A dictionary containing additional information associated with the secret. The optional secret metadata is stored unencrypted in your vault.
On an installed version of MATLAB, your vault is accessible only from the exact combination of your operating system account and local machine, and your vault and secrets do not leave your local machine. With MATLAB Online™, your vault is accessible with your MathWorks Account, and your vault and secrets are stored on a secure database on MathWorks® servers. For increased security when you use MATLAB Online, enable two-step verification for your MathWorks Account.
To set an individual secret, use setSecret. The
Secret Prompt dialog box, where you enter the secret value, supports copy-paste
functionality. To import secrets from a file, use importSecrets.
Extended Capabilities
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.
Version History
Introduced in R2026a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)