Kaleb King
03/18/2025, 2:52 PM{
"client_id": "<my-client-id>",
"client_secret": "<my-client-secret>"
}
Can I use a single storage block to recall the values and pass them to a function like this:
get_data(
client_id=<my-client-id>,
client_secret=<my-client-secret>
)
Marvin
03/18/2025, 2:53 PMBlock
from Prefect, which builds on Pydantic's BaseModel
. This way, each field in your JSON can be declared as a field in the block.
2. Store the JSON in a Block Document: Once you have defined the block type, you can create a block document to store your JSON values. This document can be created and updated via Prefect's UI or API.
3. Retrieve and Use the Values: You can then retrieve the block document in your flow or task, and use the stored values to call your get_data
function.
Here's a basic outline of how you might define a custom block and use it:
python
from prefect.blocks.core import Block
from pydantic import BaseModel
class MyCredentialsBlock(Block):
client_id: str
client_secret: str
# Save the block document
my_creds = MyCredentialsBlock(client_id="<my-client-id>", client_secret="<my-client-secret>")
my_creds.save(name="my_credentials")
# Later, retrieve and use the block
retrieved_creds = MyCredentialsBlock.load("my_credentials")
get_data(client_id=retrieved_creds.client_id, client_secret=retrieved_creds.client_secret)
For more detailed information on blocks, you can refer to the Prefect Blocks Documentation.Kaleb King
03/18/2025, 2:57 PMMarvin
03/18/2025, 2:57 PMSecretDict
field type to store a JSON object within a Secret block. The SecretDict
allows you to add a dictionary field to your block that automatically obfuscates values at all levels in the UI or in logs. This is particularly useful for storing sensitive information like credentials.
Here's a basic example of how you might define and use a Secret block with a JSON payload:
python
from prefect.blocks.system import Secret
from prefect.blocks.core import Block
from pydantic import BaseModel, SecretDict
class MySecretBlock(Block):
secrets: SecretDict
# Save the block document with your JSON
my_secret_block = MySecretBlock(secrets={"client_id": "<my-client-id>", "client_secret": "<my-client-secret>"})
my_secret_block.save(name="my_json_secrets")
# Retrieve and use the block
retrieved_secrets = MySecretBlock.load("my_json_secrets")
get_data(client_id=retrieved_secrets.secrets["client_id"], client_secret=retrieved_secrets.secrets["client_secret"])
This approach ensures that your JSON values are stored securely and can be easily retrieved for use in your flows or tasks. For more details, you can refer to the Prefect Blocks Documentation.