使用Python和Alfresco API上传文件

问题描述 投票:0回答:2

我面临 Alfresco 和 Python 的问题:我想直接使用 Python 上传新文件。每次我尝试时都会收到 HTTP 500 错误,但没有详细信息...

我首先尝试使用 CURL 只是为了确保它可以正常工作并且文件上传没有问题。

我使用了以下curl命令行:

url -X POST -uadmin:admin "http://localhost:8080/alfresco/service/api/upload" -F filedata=@/tmp/eeeeee.pdf -F siteid=test -F containerid=documentLibrary

在我的 Python 脚本中,我尝试了 PyCurl,现在又尝试了简单的 urllib。我认为问题出在文件作为参数给出的方式上。

使用 PyCurl 的 Python 代码:

c = pycurl.Curl()
params = {'containerid': 'documentLibrary', 'siteid': 'test', 'filedata': open('/tmp/eeeeee.pdf', 'rb')}
c.setopt(c.URL, 'http://localhost:8080/alfresco/service/api/upload')
c.setopt(c.POST, 1)
c.setopt(c.POSTFIELDS, urllib.urlencode(params))
c.setopt(c.USERPWD, 'admin:admin')
c.perform()

此代码示例导致:

 {
    "code" : 500,
    "name" : "Internal Error",
    "description" : "An error inside the HTTP server which prevented it from fulfilling the request."
  },  

  "message" : "04200017 Unexpected error occurred during upload of new content."

有人知道如何实现这一目标吗?

python curl upload alfresco
2个回答
8
投票

您可以使用非常简单的库Requests

import json
import requests

url = "http://localhost:8080/alfresco/service/api/upload"
auth = ("admin", "admin")
files = {"filedata": open("/tmp/foo.txt", "rb")}
data = {"siteid": "test", "containerid": "documentLibrary"}
r = requests.post(url, files=files, data=data, auth=auth)
print(r.status_code)
print(json.loads(r.text))

输出:

200
{'fileName': 'foo.txt',
 'nodeRef': 'workspace://SpacesStore/37a96447-44b0-4aaa-b6ff-98dae1f12a73',
 'status': {'code': 200,
  'description': 'File uploaded successfully',
  'name': 'OK'}}

0
投票

我正在尝试这个,它就像一个魅力,文件被上传到根文件夹。

但是我如何将其上传到特定文件夹,如何定义它以及在哪里?

© www.soinside.com 2019 - 2024. All rights reserved.