From ea9a06dc837325b10e0658050e3c0f51a6039d32 Mon Sep 17 00:00:00 2001 From: kd2718 Date: Wed, 8 Jul 2015 23:59:01 -0700 Subject: [PATCH 1/4] updated gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9018d7dfa..6f4e4fa81 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ # Backup files *.~ - +kory_test.py # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] From 0a32f72726bc6d8790851c049293217fcfe3699b Mon Sep 17 00:00:00 2001 From: kd2718 Date: Thu, 9 Jul 2015 01:08:58 -0700 Subject: [PATCH 2/4] added bytes option to content to pull file in chunks. --- .gitignore | 2 +- boxsdk/object/file.py | 11 +++++++++-- demo/partial_file_example.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 demo/partial_file_example.py diff --git a/.gitignore b/.gitignore index 6f4e4fa81..9018d7dfa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ # Backup files *.~ -kory_test.py + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/boxsdk/object/file.py b/boxsdk/object/file.py index 96f607915..c9f67128e 100644 --- a/boxsdk/object/file.py +++ b/boxsdk/object/file.py @@ -44,17 +44,24 @@ 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, bytes=[]): """ Get the content of a file on Box. + :peram bytes: + A list of two ints. These are the range value in bytes. + :type bytes: + `[int, int]` :returns: File content as bytes. :rtype: `bytes` """ + headers = None + if bytes: + headers = {'Range': 'bytes=%d-%d' % (bytes[0], bytes[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): 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') From da6f9d8a111800c5d741492566378b62ce9ef0ed Mon Sep 17 00:00:00 2001 From: kd2718 Date: Thu, 9 Jul 2015 22:41:59 -0700 Subject: [PATCH 3/4] updated Download_to --- boxsdk/object/file.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/boxsdk/object/file.py b/boxsdk/object/file.py index c9f67128e..022c29732 100644 --- a/boxsdk/object/file.py +++ b/boxsdk/object/file.py @@ -44,7 +44,7 @@ def _get_accelerator_upload_url_for_update(self): """ return self._get_accelerator_upload_url(file_id=self._object_id) - def content(self, bytes=[]): + def content(self, bytes=None): """ Get the content of a file on Box. @@ -64,7 +64,7 @@ def content(self, bytes=[]): 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, bytes=None): """ Download the file; write it to the given stream. @@ -73,8 +73,11 @@ def download_to(self, writeable_stream): :type writeable_stream: `file` """ + headers = None + if bytes: + headers = {'Range': 'bytes=%d-%d' % (bytes[0], bytes[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) From 4cd98d4fa4f61a1d1d86ce5f36299008e27e5fbd Mon Sep 17 00:00:00 2001 From: kd2718 Date: Mon, 13 Jul 2015 23:16:37 -0700 Subject: [PATCH 4/4] fixed a few spelling mistakes and implement check on array size --- boxsdk/object/file.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/boxsdk/object/file.py b/boxsdk/object/file.py index 022c29732..b0314714d 100644 --- a/boxsdk/object/file.py +++ b/boxsdk/object/file.py @@ -44,13 +44,13 @@ def _get_accelerator_upload_url_for_update(self): """ return self._get_accelerator_upload_url(file_id=self._object_id) - def content(self, bytes=None): + def content(self, byte_range=None): """ Get the content of a file on Box. - :peram bytes: + :param byte_range: A list of two ints. These are the range value in bytes. - :type bytes: + :type byte_range: `[int, int]` :returns: File content as bytes. @@ -58,13 +58,13 @@ def content(self, bytes=None): `bytes` """ headers = None - if bytes: - headers = {'Range': 'bytes=%d-%d' % (bytes[0], bytes[1])} + 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, headers=headers) return box_response.content - def download_to(self, writeable_stream, bytes=None): + def download_to(self, writeable_stream, byte_range=None): """ Download the file; write it to the given stream. @@ -72,10 +72,14 @@ def download_to(self, writeable_stream, bytes=None): 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 bytes: - headers = {'Range': 'bytes=%d-%d' % (bytes[0], bytes[1])} + 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, headers=headers) for chunk in box_response.network_response.response_as_stream.stream(decode_content=True):