-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
download_file_to_output_stream
method to `DownloadsManage…
…r` (box/box-codegen#575) (#334)
- Loading branch information
1 parent
8dd8cb7
commit 6820d08
Showing
6 changed files
with
184 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{ "engineHash": "a8a741c", "specHash": "6b64d06", "version": "1.5.1" } | ||
{ "engineHash": "244ba36", "specHash": "6b64d06", "version": "1.5.1" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,81 @@ | ||
# Downloads | ||
# DownloadsManager | ||
|
||
Downloads module is used to download files from Box. | ||
- [Download file](#download-file) | ||
- [Download file](#download-file) | ||
|
||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
## Download file | ||
|
||
- [Download a File](#download-a-file) | ||
Returns the contents of a file in binary format. | ||
|
||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
This operation is performed by calling function `download_file`. | ||
|
||
## Download a File | ||
See the endpoint docs at | ||
[API Reference](https://developer.box.com/reference/get-files-id-content/). | ||
|
||
To get the entire contents of the file as stream of bytes, call `download_file` method. | ||
This method returns a `ByteStream` object, which is an implementation of `io.BufferedIOBase`, what allows | ||
reading downloaded file as a stream. Thanks to that, the file parts will be downloaded just when needed and | ||
it won;t be necessary to store the entire file content in-memory. | ||
|
||
To get the full content of the file in `bytes` format just call `read()` method on returned object. | ||
<!-- sample get_files_id_content --> | ||
|
||
```python | ||
from io import BufferedIOBase | ||
|
||
file_content_stream: BufferedIOBase = client.downloads.download_file( | ||
file_id="123456789" | ||
) | ||
print("File content: ", file_content_stream.read()) | ||
client.downloads.download_file(uploaded_file.id) | ||
``` | ||
|
||
To save downloaded file to your local disk you can use e.g. `shutil.copyfileobj()` method: | ||
### Arguments | ||
|
||
<!-- sample get_files_id_content --> | ||
- file_id `str` | ||
- The unique identifier that represents a file. The ID for any file can be determined by visiting a file in the web application and copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the `file_id` is `123`. Example: "12345" | ||
- version `Optional[str]` | ||
- The file version to download | ||
- access_token `Optional[str]` | ||
- An optional access token that can be used to pre-authenticate this request, which means that a download link can be shared with a browser or a third party service without them needing to know how to handle the authentication. When using this parameter, please make sure that the access token is sufficiently scoped down to only allow read access to that file and no other files or folders. | ||
- range `Optional[str]` | ||
- The byte range of the content to download. The format `bytes={start_byte}-{end_byte}` can be used to specify what section of the file to download. | ||
- boxapi `Optional[str]` | ||
- The URL, and optional password, for the shared link of this item. This header can be used to access items that have not been explicitly shared with a user. Use the format `shared_link=[link]` or if a password is required then use `shared_link=[link]&shared_link_password=[password]`. This header can be used on the file or folder shared, as well as on any files or folders nested within the item. | ||
- extra_headers `Optional[Dict[str, Optional[str]]]` | ||
- Extra headers that will be included in the HTTP request. | ||
|
||
### Returns | ||
|
||
This function returns a value of type `ByteStream`. | ||
|
||
Returns the requested file if the client has the **follow | ||
redirects** setting enabled to automatically | ||
follow HTTP `3xx` responses as redirects. If not, the request | ||
will return `302` instead. | ||
For details, see | ||
the [download file guide](g://downloads/file#download-url).If the file is not ready to be downloaded yet `Retry-After` header will | ||
be returned indicating the time in seconds after which the file will | ||
be available for the client to download. | ||
|
||
This response can occur when the file was uploaded immediately before the | ||
download request. | ||
|
||
## Download file | ||
|
||
Download file to a given output stream | ||
|
||
This operation is performed by calling function `download_file_to_output_stream`. | ||
|
||
```python | ||
import shutil | ||
from io import BufferedIOBase | ||
|
||
file_content_stream: BufferedIOBase = client.downloads.download_file( | ||
file_id="123456789" | ||
) | ||
with open("file.pdf", "wb") as f: | ||
shutil.copyfileobj(file_content_stream, f) | ||
print('File was successfully downloaded as "file.pdf"') | ||
client.downloads.download_file_to_output_stream(uploaded_file.id, file_output_stream) | ||
``` | ||
|
||
### Arguments | ||
|
||
- file_id `str` | ||
- The unique identifier that represents a file. The ID for any file can be determined by visiting a file in the web application and copying the ID from the URL. For example, for the URL `https://*.app.box.com/files/123` the `file_id` is `123`. Example: "12345" | ||
- output_stream `OutputStream` | ||
- Download file to a given output stream | ||
- version `Optional[str]` | ||
- The file version to download | ||
- access_token `Optional[str]` | ||
- An optional access token that can be used to pre-authenticate this request, which means that a download link can be shared with a browser or a third party service without them needing to know how to handle the authentication. When using this parameter, please make sure that the access token is sufficiently scoped down to only allow read access to that file and no other files or folders. | ||
- range `Optional[str]` | ||
- The byte range of the content to download. The format `bytes={start_byte}-{end_byte}` can be used to specify what section of the file to download. | ||
- boxapi `Optional[str]` | ||
- The URL, and optional password, for the shared link of this item. This header can be used to access items that have not been explicitly shared with a user. Use the format `shared_link=[link]` or if a password is required then use `shared_link=[link]&shared_link_password=[password]`. This header can be used on the file or folder shared, as well as on any files or folders nested within the item. | ||
- extra_headers `Optional[Dict[str, Optional[str]]]` | ||
- Extra headers that will be included in the HTTP request. | ||
|
||
### Returns | ||
|
||
This function returns a value of type `None`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters