Hi - how do I do this, but with a block? <https://...
# prefect-aws
d
all the examples apparently want you to just have the secrets right there in the code https://prefecthq.github.io/prefect-aws/examples_catalog/
none of these work with an AwsCredentials block
n
hi @Drew Hibbard - what are you trying to do?
it seems like the example is using an aws creds block to get an s3 client?
d
so if I just straight type my key and secret in there it works as a
prefect_aws.credentials.AwsCredentials
type object
Copy code
aws_credentials = AwsCredentials(
    aws_access_key_id = "access_key_id",
    aws_secret_access_key = "secret_access_key"
    )
s3_client = aws_credentials.get_boto3_session().client("s3")
but like this is a coroutine object from which you can't start a client session or anything
aws_credentials = AwsCredentials.load("aws-drew")
n
aha
so this is a quirk, are you calling load in an async context?
if so you have to await the load call
d
well I'm not trying to haha. that seems to be the default block state. is there a way to load a block not async?
I'm just copying code from the docs
n
can you show what you're trying?
d
Copy code
aws_credentials = AwsCredentials.load("aws-drew")
sagemaker_client = aws_credentials.get_boto3_session().client("runtime.sagemaker",region_name='us-west-2')
trying to start a session but it doesn't work with the block. it only works like this
Copy code
aws_credentials = AwsCredentials(
    aws_access_key_id = "key",
    aws_secret_access_key = "secret"
    )
sagemaker_client = aws_credentials.get_boto3_session().client("runtime.sagemaker",region_name='us-west-2')
n
sorry, are you in a normal sync
def
function or an
async def
function
all blocks have a
load
method which you need to
await
in an async context, but can call normally in a sync context
Copy code
In [1]: from prefect.blocks.system import Secret

In [2]: secret = Secret(value="marvin")

In [3]: secret.save("test", overwrite=True)
Out[3]: UUID('82fa3411-c385-4fa4-a678-34033909ad49')

In [4]: Secret.load("test").value
Out[4]: SecretStr('**********')

In [5]: async def f():
   ...:     print(await Secret.load("test"))
   ...:

In [6]: f()
Out[6]: <coroutine object f at 0x107daa5a0>

In [7]: await _ # _ is the previous result in ipython
Secret(value=SecretStr('**********'))
d
I was being an idiot and trying to use it outside the task/flow context. once I put the code inside a task it works fine
my bad, thanks!
n
no worries! fwiw the above is outside a flow/task context, where you should be free to call block methods. its just that
sync_compatible
nature of
load
and
save
is a common gotcha