diff --git a/boxsdk/object/file.py b/boxsdk/object/file.py index 96f607915..b0314714d 100644 --- a/boxsdk/object/file.py +++ b/boxsdk/object/file.py @@ -44,20 +44,27 @@ def _get_accelerator_upload_url_for_update(self): """ return self._get_accelerator_upload_url(file_id=self._object_id) - def content(self): + def content(self, byte_range=None): """ Get the content of a file on Box. + :param byte_range: + A list of two ints. These are the range value in bytes. + :type byte_range: + `[int, int]` :returns: File content as bytes. :rtype: `bytes` """ + headers = None + if byte_range and len(byte_range) > 1: + headers = {'Range': 'bytes=%d-%d' % (byte_range[0], byte_range[1])} url = self.get_url('content') - box_response = self._session.get(url, expect_json_response=False) + box_response = self._session.get(url, expect_json_response=False, headers=headers) return box_response.content - def download_to(self, writeable_stream): + def download_to(self, writeable_stream, byte_range=None): """ Download the file; write it to the given stream. @@ -65,9 +72,16 @@ def download_to(self, writeable_stream): A file-like object where bytes can be written into. :type writeable_stream: `file` - """ + :param byte_range: + A list of two ints. These are the range value in bytes. + :type byte_range: + `[int, int]` + """ + headers = None + if byte_range and len(byte_range) > 1: + headers = {'Range': 'bytes=%d-%d' % (byte_range[0], byte_range[1])} url = self.get_url('content') - box_response = self._session.get(url, expect_json_response=False, stream=True) + box_response = self._session.get(url, expect_json_response=False, stream=True, headers=headers) for chunk in box_response.network_response.response_as_stream.stream(decode_content=True): writeable_stream.write(chunk) diff --git a/demo/partial_file_example.py b/demo/partial_file_example.py new file mode 100644 index 000000000..3bc086644 --- /dev/null +++ b/demo/partial_file_example.py @@ -0,0 +1,29 @@ +# Example of the following api call +# curl -L https://api.box.com/2.0/files/FILE_ID/content -H "Authorization: Bearer ACCESS_TOKEN" -H "Range: bytes=0-60" + +from boxsdk import OAuth2 +from boxsdk import Client + + +oauth = OAuth2( + client_id='client_id', + client_secret='client_secret' +) +dev_access_code = 'Developer_token' +oauth._access_token =dev_access_code + +client = Client(oauth) +items = client.folder(folder_id='0').get_items(limit=100, offset=0) + +# prints items for later user +for i in items: + print i.id + +def range_test(id): + bytes = [0,60] + k = client.file(file_id=id).content(bytes=bytes) + print k + + +if __name__ =='__main__': + range_test('FILE_ID')