主要内容

importSecrets

Import secrets from file into MATLAB vault

Since R2026a

Description

importSecrets(filename) imports secrets from the specified file into the MATLAB® vault.

example

v = importSecrets(filename, Name=Value) specifies options using one or more name-value arguments. For example, importSecrets(filename,Overwrite=true) overwrites any existing secrets that have the same name as secrets in the specified file.

example

Examples

collapse all

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

collapse all

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"
The .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 filename.

Example: "myFile.env"

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 filename.

Example: "C:\myFolder\myFile.env"

Example: "dataDir\myFile.env"

Internet URL

If the file is specified as an internet uniform resource locator (URL), then filename must contain the protocol type "http://" or "https://".

Example: "http://hostname/path_to_file/my_data.env"

Remote Location

If the file is stored at a remote location, then filename must contain the full path of the file specified with the form:

scheme_name://path_to_file/my_file.ext

Based on the remote location, scheme_name can be one of the values in this table.

Remote Locationscheme_name
Amazon S3™s3
Windows Azure® Blob Storagewasb, wasbs
HDFS™hdfs

For more information, see Work with Remote Data.

Example: "s3://bucketname/path_to_file/my_file.env"

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, importSecrets will 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 .env file containing your credentials.

  • Do not check your .env files into source code repositories. For example, in your repositories, include the .env file extension in your .gitignore_global file to exclude .env files from all your repositories. For more information on how to configure Git to ignore files, see the GitHub page Ignoring Files.

Name-Value Arguments

collapse all

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 in filename.

  • "env" — Read the contents of the input file as .env. If the file extension in filename is not .env, you can specify the value of FileType as "env" to read the contents of the input file as a .env file.

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", importSecrets detects encoding from the file.

  • If specified as "system", importSecrets uses 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

collapse all

Extended Capabilities

expand all

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2026a