<@ULVA73B9P>: I’m trying to delete assets via <htt...
# marvin-ai
j
@Marvin: I’m trying to delete assets via https://app.prefect.cloud/api/docs#tag/Assets/operation/delete_asset_api_accounts__account_id__workspaces__workspace_id__assets_key_delete. What’s the right way to encode the
key
in the query?
m
thought for 32.1 seconds
Based on the research, here's how you should handle the asset key encoding when using the DELETE endpoint: 1. The asset key should be URL-encoded (URI-encoded) before being used in the endpoint path. 2. For example, if your asset key is `my/asset/key`: - It should be encoded as
my%2Fasset%2Fkey
- The full endpoint would look like:
.../assets/my%2Fasset%2Fkey
Here's a Python example of how to properly encode the key:
Copy code
python
from urllib.parse import quote

asset_key = "my/asset/key"
encoded_key = quote(asset_key)
# encoded_key will be "my%2Fasset%2Fkey"
If you're using a REST client or making HTTP requests directly, make sure to properly URL-encode the key portion of the path. Most HTTP client libraries will handle this automatically if you provide the unencoded key as a path parameter rather than constructing the URL manually. Note that this follows standard URL encoding practices where: - Forward slashes (
/
) become
%2F
- Spaces become
%20
- Other special characters are similarly encoded If you're encountering any specific errors with certain characters in your asset keys, please let me know and I can provide more targeted guidance.
j
@Marvin: How do I correctly encode an asset key like
<s3://results/data/n0000.json>
?
m
thought for 10.3 seconds
Ah, a more complex asset key with special characters and brackets! Let me help you with the correct encoding for this specific case. The key
<s3://results/data/n0000.json>
contains several characters that need special encoding: - The angle brackets
< >
- The forward slashes
/
- The colon
:
Here's how to properly encode this in Python:
Copy code
python
from urllib.parse import quote

asset_key = "<s3://results/data/n0000.json>"
encoded_key = quote(asset_key)
print(encoded_key)
# Output: %3Cs3%3A%2F%2Fresults%2Fdata%2Fn0000.json%3E
The encoded string breaks down as follows: -
<
becomes
%3C
-
s3:
becomes
s3%3A
-
//
becomes
%2F%2F
- Additional
/
becomes
%2F
-
>
becomes
%3E
So when making the DELETE request to the Prefect Cloud API, your final URL would end with:
.../assets/%3Cs3%3A%2F%2Fresults%2Fdata%2Fn0000.json%3E
If you're using a REST client library like
requests
, you can let it handle the encoding by passing the unencoded key as a parameter:
Copy code
python
import requests

asset_key = "<s3://results/data/n0000.json>"
response = requests.delete(
    f"<https://api.prefect.cloud/api/accounts/{account_id}/workspaces/{workspace_id}/assets/{asset_key}>",
    headers={"Authorization": f"Bearer {api_key}"}
)
Most HTTP client libraries will handle the URL encoding automatically when the key is passed as part of the path parameters rather than being manually concatenated into the URL string. Let me know if you encounter any specific errors with this encoding or if you need help with a different HTTP client!
j
Found an example of what it looks like, by looking at a network request when using the Prefect Cloud UI. Needed to have
../assets/key?key=X
.
we should formalize something like this (cc @Kevin Grismore)
j
That’s a very nice script!
n
catjam
j
(sorry, was a bit slow on this thread, after I sorted it haha)
n
no worries, my favorite part is that you can just do
Copy code
uv run <https://gist.githubusercontent.com/zzstoatzz/c8a4ad709876c44cefd40e65ac983485/raw/5dcdd854799b5f42558514b9a6870fda221aa735/asset_manager.py> list
kinda clunky with the long url but you can alias ofc
j
Oh any clunkiness with the URL is outweighed by how quick you can run it like that! Versioned by the SHA, too
💪 1