[ckan-dev] Upload file through the API.

Richard Gomes rgomes.info at gmail.com
Tue Sep 24 11:01:37 UTC 2013


I was using this approach too but I found it a bit inconvenient.
In my case, I have everything running on the same box and I would like 
to attach files "directly" (not via the network), in order to achieve 
better performance.

So, I'm using the OFS API https://github.com/okfn/ofs in order to insert 
files into the file store and the Action API (method resource_create) in 
order to insert a resource in a package.
I'm still working on it. For some unknown reason, despite the attached 
file "is there", I cannot download the file when I click on the 
FileStore URL which is available in CKAN.

I've coded the classes in a ways that everything can be used bot locally 
(for better performance) or remotely.
My focus is on the local calls. In the case of remote calls, it would be 
basically only move code from CkanClient and some adjustments... but I'm 
not working on it.

It would be great if this "uploading files" would be available out of 
the box.

If there's anyone interested on what I'm doing (and would like to help!) 
please let me know.
This would lead me to share the code on github more quickly than I'm 
planning to.

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 24/09/13 08:39, Henrik Aagaard Sørensen wrote:
>
> Just to answer myself and ask for some other help, this code works for 
> uploading a file to a package (not a ressource):
>
> ckan = ckanclient.CkanClient(base_location=CKAN-URL', 
> api_key=CKAN-API-KEY)
>
> ckan.upload_file(FILENAME)
>
> ckan.add_package_resource(DATASET, FILENAME, name=FILENAME, 
> resource_type='data', format='csv')
>
> However, when running the code multiple times, for updating the 
> dataset with a new file, it fails with the following:
>
> Traceback (most recent call last):
>
>   File "oracle_csv_ckan.py", line 27, in <module>
>
>     ckan.add_package_resource(DATASET, FILENAME, name=FILENAME, 
> resource_type='data', format='csv')
>
>   File "ckanclient/ckanclient/__init__.py", line 630, in 
> add_package_resource
>
>     url, msg = self.upload_file(file_path)
>
>   File "ckanclient/ckanclient/__init__.py", line 577, in upload_file
>
>     auth_dict = self.storage_auth_get('/form/'+file_key, {})
>
>   File "ckanclient/ckanclient/__init__.py", line 447, in storage_auth_get
>
>     self.open_url(url, payload, method="POST")
>
>   File "ckanclient/ckanclient/__init__.py", line 203, in open_url
>
>     raise CkanApiConflictError(self.last_status)
>
> ckanclient.CkanApiConflictError: 409
>
> Is this due to the fact that I've already uploaded a file with the 
> same name?
>
> How can I just replace/update a file from a resource?
>
> I would like to replace a specific package resource as developers most 
> likely are using the resource ID for getting data, and then I cannot 
> just delete a file and upload a new. I need to replace the data in an 
> existing 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/20130924/70f2464b/attachment-0001.html>


More information about the ckan-dev mailing list