Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uploading a file from url #102

Closed
LekanAnni opened this issue Dec 1, 2016 · 7 comments
Closed

Uploading a file from url #102

LekanAnni opened this issue Dec 1, 2016 · 7 comments

Comments

@LekanAnni
Copy link

I get errors trying to upload file into CKAN using resource_create from URL with the below

from ckanapi import RemoteCKAN
ua = 'ckanapiexample/1.0 (+http://example.com/my/website)'
fileurl = "http://goog.com/go/file.csv"
mysite = RemoteCKAN('http://myckan.example.com', apikey='real-key', user_agent=ua)
mysite.action.resource_create(
package_id='my-dataset-with-files',
url='dummy-value', # ignored but required by CKAN<=2.5.x
upload=open(fileurl, 'rb'))

@wardi
Copy link
Contributor

wardi commented Dec 1, 2016

That's not a file, it's a url. Try urlopen() instead.

@wardi
Copy link
Contributor

wardi commented Dec 1, 2016

(or just set the url on the resource instead of uploading the file if that's what you prefer)

@LekanAnni
Copy link
Author

LekanAnni commented Dec 1, 2016

Thanks Wardi, i tried the urllib2.urlopen(file), this works fine but the problem here is that its not getting the filename so CKAN cant view the resource.

@wardi
Copy link
Contributor

wardi commented Dec 1, 2016

You can set a file name explicitly by passing a tuple, e.g. upload=('myfilename.csv', urlopen(url)) This is borrowed from requests http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file but works even if you're using LocalCKAN

@LekanAnni
Copy link
Author

	 r = requests.post('http://ckan.org/api/action/resource_create',
                            data={'package_id': package['id'],
                            'name': resourcename,
                            'description': 'some text',
                            'format': extension,
                            'url': 'upload',  # Needed to pass validation
                             },
                      headers={'Authorization': APIKEY},
                     files = {'file': (filename,urllib2.urlopen(pkg_url) )})  
                 # This makes ckan url shows `http://upload` but when i change the filename to "upload", it shows URL: http://ckan.org/dataset/b9f4caa1-be5e-4a02-939e-282acce47306/resource/87211539-e543-412e-bf9a-90084f3a55cb/download/upload 

(without linkage to the file)

image

@wardi
Copy link
Contributor

wardi commented Dec 1, 2016

your code looks wrong.

files = {'file': (filename,urllib2.urlopen(pkg_url) )}
          ^ this should say 'upload' because it's the field name

filename is where you put your file's name

@LekanAnni
Copy link
Author

Thanks wardi, i got it working now.

@wardi wardi closed this as completed Dec 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants