#-*- coding: utf-8 -*-
__author__ = 'LILI'
import requests
CKAN_URL = 'http://default.yourckan.com'
APIkey = 'API Key'
package_id = 'packageID'
resource_name = 'test'
resource_format = 'txt'
resource_address = r'C:\Users\LILI\Desktop\test.txt'
r = requests.post('{ckan}/api/action/resource_create'.format(ckan=CKAN_URL),
data={
"package_id": package_id,
"type": "file.upload",
"url": "",
"name": resource_name,
"format": resource_format
},
headers={"Authorization": APIkey},
files={'upload': (resource_name, file(resource_address))})
print r.status_code, r.text
将代码最前面的CKAN_URL、APIkey、package_id、resource_name、resource_format、resource_address分别置换为自己的CKAN网址、自己的API key、想要上传资源所属的package ID、resource的名字、resource的类型、和resource的来源地址。