[ckan-dev] Upload file through the API.
Richard Gomes
rgomes.info at gmail.com
Fri Sep 13 15:59:58 UTC 2013
hi,
I stumbled with the same difficulty.
What I did is: I've created a wrapper module with exposes the Action API
and some useful methods from the CkanClient.
You can see the module here: http://paste.ubuntu.com/6062158/
The snippet below employs the code I pointed out in order to insert
files under a given package:
from datalayer.action.api import ApiClient3
site_url='http://ckan.localdomain:5000'
base_location='%s/api' % site_url
api_key='fb58102b-a16e-48c2-a3ab-48e11dd0354c' # whatever your
api_key is!
# administrator
uadmin='admin'
padmin='secret'
# regular user
uuser='rgomes'
puser='secret'
def create_package(cli, pkg, title):
print('Create package "%s"' % pkg)
if cli.package_search(q=pkg)['count'] == 0:
cli.package_create(name=pkg, title=title, license_id='other-closed')
cli.bulk_update_private(datasets=[pkg])
def populate_package(cli, pkg, folder, mimetype):
for root, file in filelist(folder):
q='%s:%s' % ( 'name', file )
print(q)
r = cli.resource_search(query=q)
if r['count'] == 0:
path = '%s/%s' % (root, file)
print('create', path)
cli.add_package_resource(pkg, path, resource_type='data',
mimetype=mimetype)
elif r['count'] == 1:
url = r['results'][0]['url']
if url.find(site_url) == -1:
url = url.replace('http:/', site_url)
cli.resource_update(id=r['results'][0]['id'], url=url)
print('update', url)
else:
print('info', url)
else:
raise RuntimeError(file + ' :: too many entries')
cli = ApiClient3(base_location=base_location, api_key=api_key,
http_user=uadmin, http_pass=padmin, verbose=True)
create_package(cli, 'shares', 'Share prices')
populate_package(cli, 'shares', folder='~/tmp/upload/csv',
mimetype='text/csv')
I hope it helps
Cheers
Richard Gomes
http://rgomes.info
http://www.linkedin.com/in/rgomes
mobile: +44(77)9955-6813
inum <http://www.inum.net/>: +883(5100)0800-9804
On 13/09/13 11:29, Henrik Aagaard Sørensen wrote:
>
> I guess this is a question which has been answered before, however, I
> cannot find the solution anywhere.
>
> Is it possible to upload a file (CSV) via CKAN's API to a resource?
>
>
>
> _______________________________________________
> ckan-dev mailing list
> ckan-dev at lists.okfn.org
> http://lists.okfn.org/mailman/listinfo/ckan-dev
> Unsubscribe: http://lists.okfn.org/mailman/options/ckan-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20130913/98d84821/attachment-0001.html>
More information about the ckan-dev
mailing list