使用Python更新Alfresco中的现有文件

时间:2018-08-20 06:58:45

标签: python alfresco cmis

如何使用CMis通过Python在露天环境中更新现有文件

下面是我用来上传文件的方法

def alfresco_post_file(url, user, pwd, root_folder, post_folder, post_file, file_name, logger):
        client = CmisClient(url, user, pwd)
        repo = client.defaultRepository    
        try:
            folder = repo.getObjectByPath('/'+root_folder)
            if post_folder:
                folder = repo.getObjectByPath('/'+post_folder)
            contents = open(post_file, 'r')
            file = folder.createDocument(file_name, contentFile = contents)
        except Exception as e:
            sys.exc_clear()
            if type(e).__name__ == 'ObjectNotFoundException':
                folder = repo.getObjectByPath('/'+root_folder)
                createfolder = folder.createFolder(post_folder.replace(root_folder+'/',''))
                folder = repo.getObjectByPath('/'+post_folder)
                contents = open(post_file, 'r')
                file = folder.createDocument(file_name, contentFile = contents)
            #elif type(e).__name__ == 'UpdateConflictException':
                #folder = repo.getObjectByPath('/'+post_folder)
                #contents = open(post_file, 'r')
                #file = folder.updateDocument(file_name, contentFile = contents)
            else:
                print("(alfresco_post_file) Execution failed:", e)
                logger.info('(alfresco_post_file) Execution failed:' + str(e));

1 个答案:

答案 0 :(得分:0)

源代码通常是示例的好来源。在这种情况下,我从cmislibtest.py中的一项测试中抓取了几行:

# check out the document to get its Private Working Copy (PWC):
pwc = newDoc.checkout()

# update the PWC with a new file
f = open(testFile2, 'rb')
pwc.setContentStream(f)
f.close()

# checkin the PWC
newDoc = pwc.checkin()