Python使用Ckan API创建并上传resource

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/coffee_cream/article/details/52062923
#-*- 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_URLAPIkeypackage_idresource_nameresource_formatresource_address分别置换为自己的CKAN网址、自己的API key、想要上传资源所属的package ID、resource的名字、resource的类型、和resource的来源地址。

猜你喜欢

转载自blog.csdn.net/coffee_cream/article/details/52062923