Apify and Crawlee Official Forum

Updated 8 months ago

Download images and store in apify

Hi, I read the documentation on keyvalue pair storage, but did not quite really fiure out how to use it to store images, videos or audio using python sdk. Is there any example code that I can reference to?
1
A
M
p
7 comments
just advanced to level 1! Thanks for your contributions! πŸŽ‰
Hey

You may want to look at this discussion.

https://discord.com/channels/801163717915574323/1202619785884467231

And so I've prepared a little sample code

Plain Text
async def download_image(url):
    response = await client.get(url)
    key_store = await Actor.open_key_value_store(name='images')
    await key_store.set_value("test", response.content)
    image = await key_store.get_value("test")
    with open("test.jpg", "wb") as f:
        f.write(image)
thank you for the help, I figured it out.
Hello again, I successfully stored image in apify using key value storage, and when I tried to store audio, by sending its base64 value, when I go to the public url of the keyvalue it is not playing
https://api.apify.com/v2/key-value-stores/2E6MJPpFo0qpUEC3v/records/test

This is the code:

async def upload_to_apify(self, value):

key_store = await Actor.open_key_value_store(name='audio')
await key_store.set_value('test',value, content_type='audio/mpeg')
public_url = await key_store._get_public_url_internal('test')

print(public_url)
Hey )

But you encoded it as base64 and specified the data type as audio/mpeg...

If you download your audio and decode Base64 and save it to a file, it will play correctly.

Plain Text
import requests
import base64 

response = requests.get("https://api.apify.com/v2/key-value-stores/2E6MJPpFo0qpUEC3v/records/test")

decoded_bytes = base64.b64decode(response.text)

with open("test.mpeg", "wb") as f:
    f.write(decoded_bytes)
oh I thought we need to send base64 value, but now I sent the whole audio file and it is working properly. Thank you.
Add a reply
Sign up and join the conversation on Discord