Upload file in Subfolder

SOLVED
Go to solution
Highlighted
New Member

Upload file in Subfolder

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.

2 REPLIES
Box Employee

Re: Upload file in Subfolder

Hi @mchowdappa — 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!

Box Employee

Re: Upload file in Subfolder

Nice explanation @mwiller!

Users online (1,325)