You may want to save data produced by your tasks. Beam provides an abstraction called Output
, which allows you to save files or directories and generate public URLs to access them.
Saving Files
To save an Output
, you can write any filetype to Beam’s /tmp
directory.
Here’s what your code might look like:
from beam import function, Output
@function()
def save_output():
file_name = "/tmp/my_output.txt"
with open(file_name, "w") as f:
f.write("This is an output, a glorious text file.")
output_file = Output(path=file_name)
output_file.save()
public_url = output_file.public_url(expires=400)
return {"output_url": public_url}
Directories
You can also create public URLs for directories, by passing in a directory path:
file_path = "./tmp/waveforms"
output = Output(path=file_path)
output.save()
output_url = output.public_url()
PIL Images
If your app uses PIL, Output
includes a wrapper around PIL to simplify the process of generating a public URL for the PIL image file:
image = pipe(...)
output = Output.from_pil_image(image).save()
Here’s a full example:
from beam import Image as BeamImage, Output, function
@function(
image=BeamImage(
python_packages=[
"pillow",
],
),
)
def save_image():
from PIL import Image as PILImage
pil_image = PILImage.new(
"RGB", (100, 100), color="white"
)
output = Output.from_pil_image(pil_image)
output.save()
url = output.public_url(expires=400)
print(url)
print(f"Output ID: {output.id}")
print(f"Output Path: {output.path}")
print(f"Output Stats: {output.stat()}")
print(f"Output Exists: {output.exists()}")
return {"image": url}
if __name__ == "__main__":
save_image()
When you run this function, it will return a pre-signed URL to the image:
https://app.beam.cloud/output/id/abe0c95a-2cd1-40b3-bace-9225f2c79c6d
Generating Public URLs
Your app might return files from the API, such as images or MP3s. You can use Output
to generate a public URL to access the content.
Expiring Public URLs
You can pass an optional expires
parameter to output.public_url
to control how long to persist the file before it is deleted.
By default, public URLs are automatically deleted after 1 hour.
output.public_url(expires=300)