Skip to main content
Question

[API] Upload files in Python

  • May 22, 2025
  • 5 replies
  • 43 views

Forum|alt.badge.img

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-p/67262"


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
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.

5 replies

Forum|alt.badge.img

bump


Forum|alt.badge.img

It is solved. Thank you very much


Forum|alt.badge.img

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


Forum|alt.badge.img

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


Forum|alt.badge.img
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)