首页 文章

使用 Python 和 Alfresco API 上传文件

提问于
浏览
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."

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

1 回答

  • 7

    您可以使用非常简单的库要求

    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'}}
    

相关问题