Working with Files¶
It is possible to use Python-Redmine to upload/download files to/from Redmine. This document
describes low-level interfaces that Python-Redmine provides, in most cases they shouldn’t be
used directly and high-level interfaces, e.g. uploads
parameter in Issue
resource or download()
method in Attachment resource should be used
instead. To get access to these low-level interfaces you have to call either redmine.upload()
or redmine.download()
where redmine
is a configured redmine object. See the
Configuration about how to configure redmine object.
Upload¶
- redminelib.Redmine.upload(f)
Uploads file from file path / file stream to Redmine and returns an assigned token.
- Parameters:
f (string or file-like object) – (required). File path / stream that will be uploaded.
filename – (optional). Filename for the file that will be uploaded.
- Returns:
dict with id and token string (Redmine >= 3.4.0) or dict with token string only (Redmine < 3.4.0)
>>> data = redmine.upload('/usr/local/image.jpg', filename='beauty.jpg')
>>> data
{'id': 7167, 'token': '7167.ed1ccdb093229ca1bd0b043618d88743'}
If a filename isn’t specified, Python-Redmine will use the original filename from a path, if any. If a
file-like object is provided, be sure that it contains bytes
and not str
, otherwise Python-Redmine
will have to make additional conversion, which will affect performance.
Download¶
- redminelib.Redmine.download(url, savepath=None, filename=None, params=None)
Downloads file from Redmine and saves it to savepath or returns a response directly for maximum control over file processing.
- Parameters:
url (string) – (required). URL of the file that will be downloaded.
savepath (string) – (optional). Path where to save the file.
filename (string) – (optional). Name that will be used for the file.
params (dict) – (optional). Params to send in the query string.
- Returns:
string or requests.Response object
If a savepath
argument is provided, then a file will be saved into the provided path with
its own name, if a filename
argument is provided together with the savepath
argument,
then a file will be saved into the provided path under the provided name and the resulting path
to the file will be returned.
>>> filepath = redmine.download('https://redmine.url/foobar.jpg', savepath='/usr/local', filename='image.jpg')
>>> filepath
'/usr/local/image.jpg'
If only a url
argument is provided, then a requests.Response object will be returned which can be used for a maximum control over
file processing. For example, you can call a iter_content() method with the needed arguments to have full control over the
content reading process:
>>> response = redmine.download('https://redmine.url/foobar.jpg')
>>> for chunk in response.iter_content(chunk_size=1024):
# do something with chunk