Skip to main content
Question

Upload file in Subfolder

  • May 22, 2025
  • 3 replies
  • 29 views

Forum|alt.badge.img

Hi,

I have created a main folder from box UI(Browser) so that all the files need to store in that folder for my use case.

 

In API, I have created root folder and iterating children from the root. As I have created Folder from the browser, I am not finding the same folder in root's children.

    BoxFolder rootFolder = BoxFolder.getRootFolder(serviceAccountClient);

   listFolder(rootFolder, 0);

 

    private void listFolder(BoxFolder folder, int depth) {

        for (BoxItem.Info itemInfo : folder) {

            String indent = "";

            for (int i = 0; i < depth; i++) {

                indent += "    ";

            }

 

            System.out.println(indent + itemInfo.getName());

            if (itemInfo instanceof BoxFolder.Info) {

                BoxFolder childFolder = (BoxFolder) itemInfo.getResource();

                if (depth < 1) {

                    listFolder(childFolder, depth + 1);

                }

            }

        }

    }

 

 What is the approach if I want to store the file in a specific folder.

3 replies

Forum|alt.badge.img

Hi  — this is due to the fact that your Box account that you access through the browser (our web application UI) is actually a totally different user account from the service account associated with your application.  The folders in one account are not automatically shared between these two accounts — you'll need to either invite the service account to collaborate on the folder in your own account (to give it access to that folder), or create a new folder in the service account's root folder to store files in and then invite your personal account to collaborate on that folder.  I personally think the second option is a bit easier, since you already have API access to the service account — the code to create and collaborate the folder from the service account would look something like this:

 

// Create a new subfolder in the service account's root folder
BoxFolder rootFolder = BoxFolder.getRootFolder(serviceAccountClient);
BoxFolder.Info subfolder = rootFolder.createFolder("Shared With Service Account");

// Collaborate your personal account into the new subfolder
// and give your personal account co-owner permissions on the folder
// NOTE: REPLACE THE EMAIL ADDRESS BELOW WITH YOUR ACCOUNT LOGIN EMAIL String myLoginEmail = "yourAccountLoginEmailAddress@example.com"; subfolder.getResource().collaborate(myLoginEmail, BoxCollaboration.Role.CO_OWNER);

You should then see a folder in your personal account called "Shared With Service Account" that both your personal account and the service account have access to!


Forum|alt.badge.img

Nice explanation !


Forum|alt.badge.img

My dev application and my folder both created by with my user but I am receiving below error.

 

boxsdk.exception.BoxAPIException: Message: Not Found
Status: 404
Code: not_found
Request ID: 9mgf8eg0qy9jc8nf
Headers: {'content-length': '322', 'age': '2', 'strict-transport-security': 'max-age=31536000', 'vary': 'Accept-Encoding', 'connection': 'keep-alive', 'cache-control': 'no-cache, no-store', 'date': 'Mon, 04 Mar 2019 10:42:49 GMT', 'content-type': 'application/json'}
URL: https://upload.box.com/api/2.0/files/content
Method: POST
Context Info: {u'errors': [{u'reason': u'invalid_parameter', u'message': u"Invalid value 'd_6***phone number removed for privacy***'. 'parent' with value 'd_6***phone number removed for privacy***' not found", u'name': u'parent'}]}

 

Please help me on this.