Apify Discord Mirror

Updated 10 months ago

Download images and store in apify

At a glance

The community member posted a question about how to use the key-value pair storage in Python SDK to store images, videos, and audio. Another community member provided a sample code snippet for storing an image, and the original poster confirmed that they figured it out.

Later, the original poster had an issue with storing audio files, where the audio was not playing when accessed through the public URL. Another community member suggested that the audio should be stored as the raw file content instead of base64-encoded, and provided sample code to demonstrate how to download and decode the audio file.

The original poster acknowledged that sending the whole audio file instead of base64 worked properly, and thanked the community member for the help.

Useful resources
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
s
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