【问题标题】:Uploading file with Python and Alfresco API使用 Python 和 Alfresco API 上传文件
【发布时间】:2015-05-21 09:31:40
【问题描述】:

我在使用 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."

有人知道如何实现吗?

【问题讨论】:

  • edit 您的问题并包括您使用的curl 命令。还请包括失败的 Python 代码。 Alfresco 日志是否包含错误消息?
  • 谢谢,已编辑!日志中没有错误消息,只是: ::1 - - [21/May/2015:10:08:49 +0200] "POST /alfresco/service/api/upload HTTP/1.1" 500 6580
  • 这看起来像是访问日志中的一行。查看catalina.out 日志文件。

标签: python curl upload alfresco


【解决方案1】:

您可以使用非常简单的库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'}}

【讨论】:

  • 好的,它工作,感谢代码。你知道为什么它不能与 pycurl 一起工作吗?
  • 没有。如果没有 Alfresco 日志文件中的错误消息,就很难分辨。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
  • 1970-01-01
  • 2017-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多