civis.io.file_to_civis

civis.io.file_to_civis(buf, name, api_key=None, client=None, **kwargs)[source]

Upload a file to Civis.

Parameters:

buf : file-like object

The file or other buffer that you wish to upload.

name : str

The name you wish to give the file.

api_key : DEPRECATED str, optional

Your Civis API key. If not given, the CIVIS_API_KEY environment variable will be used.

client : civis.APIClient, optional

If not provided, an civis.APIClient object will be created from the CIVIS_API_KEY.

**kwargs : kwargs

Extra keyword arguments will be passed to the file creation endpoint. See post().

Returns:

file_id : int

The new Civis file ID.

Notes

If you are opening a binary file (e.g., a compressed archive) to pass to this function, do so using the 'rb' (read binary) mode (e.g., open('myfile.zip', 'rb')).

If you have the requests-toolbelt package installed (pip install requests-toolbelt) and the file-like object is seekable, then this function will stream from the open file pointer into Platform. If requests-toolbelt is not installed or the file-like object is not seekable, then it will need to read the entire buffer into memory before writing.

Examples

>>> # Upload file which expires in 30 days
>>> with open("my_data.csv", "r") as f:
...     file_id = file_to_civis(f, 'my_data')
>>> # Upload file which never expires
>>> with open("my_data.csv", "r") as f:
...     file_id = file_to_civis(f, 'my_data', expires_at=None)