[API] Upload files in Python

Highlighted
New Contributor

[API] Upload files in Python

Hello everyone.
I'm Japanese.

Honestly, I'm really a poor speaker of English. So please be kind and patient with me.


I try to upload the file while referring to the following URL.

"https://community.box.com/t5/Platform-and-Development-Forum/API-Upload-files-using-API-in-python/td-..."


I can upload files related to the application in this code.(My API)

 

# -*- coding: utf-8 -*-

from boxsdk import JWTAuth
from boxsdk import Client

# Configure JWT auth object
sdk = JWTAuth.from_settings_file('C:/Users/user/Desktop/box_conf/203407836_9cpi2izz_config.json')

# Get auth client
client = Client(sdk)

# Set upload values
file_path = 'C:/Users/user/Desktop/006.png'
file_name = 'photo.png'
folder_id = '0'

box_file = client.folder(folder_id).upload(file_path, file_name)
print(vars(box_file))

 

 

 

but I can't upload box user's folder in this code.

 

# -*- coding: utf-8 -*-

from boxsdk import JWTAuth
from boxsdk import Client

# Configure JWT auth object
sdk = JWTAuth.from_settings_file('C:/Users/user/Desktop/box_conf/203407836_9cpi2izz_config.json')

# Get auth client
client = Client(sdk)

# Set upload values
file_path = 'C:/Users/user/Desktop/006.png'
file_name = 'photo.png'
folder_id = '0'


user_id = '*******'
box_file = client.as_user(user_id).folder(folder_id).upload(file_path, file_name)

 

 

An error has occurred.

##############################################################

Traceback (most recent call last):
File "test_boxsdk3.py", line 29, in <module>
box_file = client.as_user(userid).folder(folder_id).upload(file_path, file_name)
File "C:\Users\user\script\study04\boxsdk\object\cloneable.py", line 21, in as_user
return self.clone(self.session.as_user(user))
File "C:\Users\user\script\study04\boxsdk\session\session.py", line 243, in as_user
kwargs['default_headers']['As-User'] = user.object_id
AttributeError: 'str' object has no attribute 'object_id'

##############################################################


I'd like some advice.
Thanks.

Tags (3)
5 REPLIES 5
Highlighted
New Contributor

Re: [API] Upload files in Python

bump

Highlighted
New Contributor

Re: [API] Upload files in Python

It is solved. Thank you very much

Highlighted
First-time Contributor

Re: [API] Upload files in Python

@KS7315 Can you share how you solved the issue you were having? Thank you.

Highlighted
New Contributor

Re: [API] Upload files in Python

The input tag with the file type creates a "Browse" button.

Highlighted
First-time Contributor

Re: [API] Upload files in Python

user_id = '111111111' #whatever your user id is
user = client.user(user_id)
box_file = client.as_user(user).folder(folder_id).upload(file_path, file_name)