How to select a folder to upload file to? (plone 5)

I want to upload a file to a specific directory using shell...
Here is what I know how to do:
I can upload a file using container.manage_addFile.
I cannot seem to figure out how to put that file into the correct folder.

Does manage_addFile take a container? If so how do you get the "foo" folder and pass it to it? If not, is there another way to do that?

You can use plone.api: https://docs.plone.org/develop/plone.api/docs/api/content.html

Here is a example of something I did recently, the difference is that I had a base64 encoded image that I was saving as a new Image. But your process will be similar to this. Since it's a file, you'll need to save it as a NamedBlobFile or NamedBlobImage

from plone import api
from plone.namedfile.file import NamedBlobImage

portal = api.portal.get()
container = portal['files-folder']
blob = NamedBlobImage(
    data=base64.b64decode(img['data']),
    contentType=img.get('content_type'),
    filename=img.get('filename', 'Image'))
api.content.create(
    type='Image',
    container=container,
    image=blob,
    title=img.get('filename', 'Image'),
)
1 Like

Just a note: you can construct content like this 'for everything'.
If you have workflow for the content type, you will need to add

api.content.transition(obj=my_content, transition='publish')

Some examples, if they should be of interest to someone:

This may be a stupid question, but why can't I use container.manage_addImage and upload the image file instead of creating a namedblobimage and use create instaed?

Can you provide more of your code? We don't know what container is without context.

Technically, manage_addFile is a method of the container. You can tell Plone which folder you want the file to be added to by setting the container variable to that folder. That is what @cdw9 did with this line:

 container = portal['files-folder']

In her example, there is a folder at the top of the Plone site; its ID is files-folder.

because you're using Plone and not Zope; Plone content types are completely different from Zope content types, and manage_addImage is not supposed to be used in Plone.

that's why you have to use something like the snippet posted by @cdw9.

1 Like

Thank you that makes sense.

What is data's format? Do I just read the image as binary and take that as data to feed into the NamedBlobImage?

here you have another example:

data is the image itself in binary format; the second parameter is the MIME type and the third the filename.

1 Like