Upload and download files#

This guide shows you how to move files and images in and out of a site, from Python and from the command line.

Upload a file#

image = client.files.upload("/images", "photo.jpg", title="A photo")
document = client.files.upload("/files", "report.pdf")

upload() creates an Image for image files and a File for anything else. The file travels base64-encoded inside one JSON request.

From the command line, upload several files at once.

plone-client upload /images photo.jpg logo.png

Upload a large file#

For large files, upload in chunks with TUS.

video_url = client.files.tus_upload("/files", "talk.mp4")

tus_upload() returns the URL of the new item.

plone-client upload /files talk.mp4 --tus

Replace the file of an item#

client.files.replace(document["@id"], "report-2.pdf")
client.files.replace(image["@id"], "photo-2.jpg", field="image")
client.files.tus_replace(video_url, "talk-2.mp4")

replace() writes the file field unless given another one, so pass field="image" for images.

Set a file field on another content type#

file_data() builds the value plone.restapi expects for any file or image field. This example sets the image field of a news item on a Classic UI site.

from plone_client import file_data

client.content.update("/news/hello", image=file_data("lead.jpg"))

plone.restapi ignores a field that the content type does not have, without reporting an error. Check the type’s schema with client.types.get("News Item")["properties"] first, as Publish a news item with a lead image does.

Download a file#

pdf = client.files.download("/files/report.pdf")
photo = client.files.download("/images/photo.jpg", field="image")

The command line picks the image field for Image items by itself.

plone-client download /images/photo.jpg -o photo.jpg