-
Notifications
You must be signed in to change notification settings - Fork 52
Add loadTextContent()
and loadFileAsBase64()
functions
#1191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Gijsreyn
wants to merge
4
commits into
PowerShell:main
Choose a base branch
from
Gijsreyn:gh-57/main/add-loadFile-functions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
234 changes: 234 additions & 0 deletions
234
docs/reference/schemas/config/functions/loadFileAsBase64.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
--- | ||
description: Reference for the 'loadFileAsBase64' DSC configuration document function | ||
ms.date: 10/17/2025 | ||
ms.topic: reference | ||
title: loadFileAsBase64 | ||
--- | ||
|
||
## Synopsis | ||
|
||
Loads a file and encodes its content as a base64 string. | ||
|
||
## Syntax | ||
|
||
```Syntax | ||
loadFileAsBase64(filePath) | ||
``` | ||
|
||
## Description | ||
|
||
The `loadFileAsBase64()` function reads a file and returns its content encoded | ||
as a base64 string. File loading occurs during compilation, not at runtime. | ||
|
||
- The file path can be absolute or relative to the configuration document. | ||
- The maximum allowed file size is 96 KB (98,304 bytes). | ||
- Both text and binary files are supported. | ||
- The encoding is suitable for embedding binary data in text-based formats. | ||
|
||
> [!IMPORTANT] | ||
> This function loads file content at compile time. The file must be accessible | ||
> when the configuration is compiled and executed. For runtime file operations, use an | ||
> appropriate resource. | ||
|
||
## Examples | ||
|
||
### Example 1 - Embed a binary certificate file | ||
|
||
Load a certificate file and encode it as base64 for transmission or storage. | ||
This is useful for embedding certificates in configuration that will be decoded | ||
at deployment time. | ||
|
||
```yaml | ||
# loadFileAsBase64.example.1.dsc.config.yaml | ||
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json | ||
resources: | ||
- name: Echo certificate | ||
type: Microsoft.DSC.Debug/Echo | ||
properties: | ||
output: "[loadFileAsBase64('./certs/server.crt')]" | ||
``` | ||
|
||
```bash | ||
dsc config get --file loadFileAsBase64.example.1.dsc.config.yaml | ||
``` | ||
|
||
```yaml | ||
results: | ||
- name: Echo certificate | ||
type: Microsoft.DSC.Debug/Echo | ||
result: | ||
actualState: | ||
output: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURYVEN... | ||
messages: [] | ||
hadErrors: false | ||
``` | ||
|
||
### Example 2 - Load an image for API deployment | ||
|
||
Encode an image file to send via an API that accepts base64-encoded images. | ||
This is common with container registries, cloud APIs, and configuration | ||
management systems. | ||
|
||
```yaml | ||
# loadFileAsBase64.example.2.dsc.config.yaml | ||
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json | ||
resources: | ||
- name: Echo logo image | ||
type: Microsoft.DSC.Debug/Echo | ||
properties: | ||
output: "[loadFileAsBase64('./assets/logo.png')]" | ||
``` | ||
|
||
```bash | ||
dsc config get --file loadFileAsBase64.example.2.dsc.config.yaml | ||
``` | ||
|
||
```yaml | ||
results: | ||
- name: Echo logo image | ||
type: Microsoft.DSC.Debug/Echo | ||
result: | ||
actualState: | ||
output: iVBORw0KGgoAAAANSUhEUgAAAAUA... | ||
messages: [] | ||
hadErrors: false | ||
``` | ||
|
||
### Example 3 - Embed encryption key | ||
|
||
Load a binary encryption key file for secure configuration. Base64 encoding | ||
makes binary data safe for JSON/YAML while preserving all bytes exactly. | ||
|
||
```yaml | ||
# loadFileAsBase64.example.3.dsc.config.yaml | ||
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json | ||
resources: | ||
- name: Echo encryption key | ||
type: Microsoft.DSC.Debug/Echo | ||
properties: | ||
output: "[loadFileAsBase64('./secrets/encryption.key')]" | ||
``` | ||
|
||
```bash | ||
dsc config get --file loadFileAsBase64.example.3.dsc.config.yaml | ||
``` | ||
|
||
```yaml | ||
results: | ||
- name: Echo encryption key | ||
type: Microsoft.DSC.Debug/Echo | ||
result: | ||
actualState: | ||
output: a2V5X2RhdGFfaGVyZQ== | ||
messages: [] | ||
hadErrors: false | ||
``` | ||
|
||
### Example 4 - Round-trip encoding and decoding | ||
|
||
Load a text file as base64 and decode it using [`base64ToString()`][00]. This | ||
demonstrates how to verify the encoding or process file content through base64. | ||
|
||
```yaml | ||
# loadFileAsBase64.example.4.dsc.config.yaml | ||
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json | ||
resources: | ||
- name: Echo decoded content | ||
type: Microsoft.DSC.Debug/Echo | ||
properties: | ||
output: "[base64ToString(loadFileAsBase64('./README.md'))]" | ||
``` | ||
|
||
```bash | ||
dsc config get --file loadFileAsBase64.example.4.dsc.config.yaml | ||
``` | ||
|
||
```yaml | ||
results: | ||
- name: Echo decoded content | ||
type: Microsoft.DSC.Debug/Echo | ||
result: | ||
actualState: | ||
output: | | ||
# My Application | ||
This is a sample README file. | ||
messages: [] | ||
hadErrors: false | ||
``` | ||
|
||
### Example 5 - Create data URI for embedding | ||
|
||
Use [`concat()`][01] to create a data URI from an image file. This is useful | ||
for embedding small images directly in HTML, CSS, or configuration files. | ||
|
||
```yaml | ||
# loadFileAsBase64.example.5.dsc.config.yaml | ||
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json | ||
resources: | ||
- name: Echo data URI | ||
type: Microsoft.DSC.Debug/Echo | ||
properties: | ||
output: "[concat('data:image/png;base64,', | ||
loadFileAsBase64('./icon.png'))]" | ||
``` | ||
|
||
```bash | ||
dsc config get --file loadFileAsBase64.example.5.dsc.config.yaml | ||
``` | ||
|
||
```yaml | ||
results: | ||
- name: Echo data URI | ||
type: Microsoft.DSC.Debug/Echo | ||
result: | ||
actualState: | ||
output: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA... | ||
messages: [] | ||
hadErrors: false | ||
``` | ||
|
||
## Parameters | ||
|
||
### filePath | ||
|
||
The path to the file to load. Can be an absolute path or relative to the | ||
configuration document location. Both text and binary files are supported. | ||
|
||
```yaml | ||
Type: string | ||
Required: true | ||
Position: 1 | ||
``` | ||
|
||
## Output | ||
|
||
Returns the file content encoded as a base64 string. The maximum file size is | ||
96 KB (98,304 bytes). | ||
|
||
```yaml | ||
Type: string | ||
``` | ||
|
||
## Errors | ||
|
||
The function returns an error when: | ||
|
||
- The file path is not a string | ||
- The file does not exist | ||
- The path points to a directory instead of a file | ||
- The file metadata cannot be read (permissions, etc.) | ||
- The file size exceeds 96 KB (98,304 bytes) | ||
- The file content cannot be read | ||
|
||
## Related functions | ||
|
||
- [`loadTextContent()`][02] - Loads text files with encoding support | ||
- [`base64ToString()`][00] - Decodes base64 strings to text | ||
- [`concat()`][01] - Concatenates strings together | ||
- [`base64()`][03] - Encodes strings to base64 | ||
|
||
<!-- Link reference definitions --> | ||
[00]: ./base64ToString.md | ||
[01]: ./concat.md | ||
[02]: ./loadTextContent.md | ||
[03]: ./base64.md |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is true for bicep, but not true for DSC as we don't have a "compile" step and this function will be used at runtime. However, this does bring up an interesting problem. If this function only exists in bicep and not ARM, then a DSC config using this function would be executed at bicep compile time and thus DSC will never see the function call.
I wonder if it makes sense then to call this something else to avoid the confusion as the DSC function would execute at runtime. This is a good topic to discuss at next WG meeting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep the PR open and put the other functions on hold as mentioned in the issue.