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

Content bytes option #56

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 19 additions & 5 deletions boxsdk/object/file.py
Expand Up @@ -44,30 +44,44 @@ 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.

:param 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)

Expand Down
29 changes: 29 additions & 0 deletions 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')