-
-
Save barduinor/e637417cebe422bb19cb548397d9d715 to your computer and use it in GitHub Desktop.
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
""" | |
Example of how to handle a file upload to Box | |
To get started copy your .config-jwt.json file to the same directory as this script | |
> python3 -m venv venv | |
> pip install "boxsdk[jwt]" | |
> python3 upload_file.py | |
""" | |
from io import StringIO | |
import pandas as pd | |
from boxsdk import Client, JWTAuth, BoxAPIException | |
from boxsdk.object.file import File | |
def jwt_auth() -> JWTAuth: | |
""" | |
Get the auth for the JWT app user | |
""" | |
auth = JWTAuth.from_settings_file("./.config-jwt.json") | |
return auth | |
def jwt_client(auth: JWTAuth) -> Client: | |
""" | |
Get the client for the JWT app user | |
""" | |
client = Client(auth) | |
return client | |
def get_file_content(client:Client,file_id): | |
file_content = client.file(file_id).content(file_version=None, byte_range=None) | |
# file = client.file(file_id).get() | |
# file_content = file.content(file_version=None, byte_range=None) | |
s = str(file_content) | |
data = StringIO(s) | |
df = pd.read_csv(data) | |
print(f"Data frame: {df}") | |
return df | |
def main(): | |
"""main function""" | |
auth = jwt_auth() | |
client = jwt_client(auth) | |
print(f"Current user is: {client.user().get()}") | |
file_id = '1039485444970' | |
file_content = get_file_content(client,file_id) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment