Utils#
Small helpers used across unicloud. The encode / decode pair is the recommended way to pass a GCS service-account JSON through an environment variable (for example SERVICE_KEY_CONTENT in CI), since the raw JSON contains characters that do not round-trip through most shells safely.
unicloud.utils
#
Small helpers for moving credential payloads through environment variables.
Google Cloud service-account JSON files do not round-trip cleanly through most shells or CI secret
stores, so unicloud ships :func:encode and :func:decode — a complementary base64 pair that lets
you stash the full JSON inside a single environment variable (commonly SERVICE_KEY_CONTENT, which
:class:unicloud.google_cloud.gcs.GCS reads out of os.environ) and decode it back into a dict
at runtime.
encode(secret_file)
#
Encode a service-account payload to a base64 bytestring.
Accepts three equivalent inputs so you can produce the encoded value from whatever form of the
service-account JSON you have handy: a file path on disk, an in-memory dict, or a JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secret_file
|
Union[str, Any]
|
One of:
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
The base64-encoded JSON, suitable for storing in an environment variable. |
Examples:
- Encode an in-memory dict directly:
- Encode from a JSON string with the same content:
- Encode from a file path on disk:
See Also
decode: Inverse operation that turns the base64 payload back into a dict.
Source code in src/unicloud/utils.py
decode(string)
#
Decode a base64 bytestring back into a service-account dict.
This is the inverse of :func:encode — given the bytes that :func:encode produced (or the
same bytes pulled out of os.environ["SERVICE_KEY_CONTENT"]), return the parsed JSON as a
plain dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
bytes
|
The base64-encoded JSON payload, typically read from an environment variable. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, str]
|
Dict[str, str]: The decoded service-account content. |
Examples:
- Round-trip a base64 payload back into a dict:
See Also
encode: Produces the base64 bytestring that this function decodes.