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?
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'),
)
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?
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.