Recursive Folder and files Upload and Download from Box to N/w and Vice Versa using SDK
Hi,
I am new to Box SDK. Does anyone implemented or Have Sample Code to Upload and Download folders/sub folders and its content from Box.com to N/w and Vice versa?
Thanks
-
Hi ,
These guides should help you get there:
- Upload all files from a folder: https://developer.box.com/docs/upload-all-files-from-a-folder
- Download all files from a folder: https://developer.box.com/docs/download-all-files-from-a-folder-1
Let us know if you have any questions along the way,
Jon
-
Hi Jcleblanc,
Thanks for you reply. This code is failing if i have sub folders inside parent folder .When it encounter another folder inside parent folder code is failing
test
Exception in thread "main" java.nio.file.AccessDeniedException: C:\Hr_Test_Data\test\test
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source) -
Hi Jcleblanc,
Hi
Thanks for you reply. This code is failing if i have sub folders inside parent folder .When it encounter another folder inside parent folder code is failing. This code only works for root folder and documents inside that folder.
Is it possible to release another version which will handle both folders ,sub folders and its contents?
test
Exception in thread "main" java.nio.file.AccessDeniedException: C:\Hr_Test_Data\test\test
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source) -
Hi
Thanks for you reply. This code is failing if i have sub folders inside parent folder .When it encounter another folder inside parent folder code is failing. This code only works for root folder and documents inside that folder.
Is it possible to release another version which will handle both folders ,sub folders and its contents?
test
Exception in thread "main" java.nio.file.AccessDeniedException: C:\Hr_Test_Data\test\test
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source) -
This is what I wrote for recursive download. It took me quite a little while to figure out, so I thought it might be of use to someone else.
package com.MyMethods.MyBoxMethods; import com.box.sdk.*; import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import static com.MyMethods.MyBoxMethods.BoxAuthentication.secondaryAppToken; import org.apache.commons.lang3.StringUtils; import static org.apache.commons.lang3.StringUtils.substringAfter; import static org.apache.commons.lang3.StringUtils.substringBeforeLast; public class BoxDownloadAllFilesRecursive { public static String currentPath; public static String relativePath; public static String mainParentFolder; public static BoxAPIConnection client3 = new BoxAPIConnection(secondaryAppToken); public static BoxAPIConnection api3 = new BoxAPIConnection(secondaryAppToken); private static final String DEVELOPER_TOKEN = "CdvvlkAIzMuzb81GzFKNQRnz1d67Eml9"; public static BoxAPIConnection client1 = new BoxAPIConnection(DEVELOPER_TOKEN); private static BoxAPIConnection api1 = new BoxAPIConnection(DEVELOPER_TOKEN); public static final String baseUrl = MyAuthMethods.getBaseUrlStatic(); public static String localRoot = MyAuthMethods.getVideoPathStatic(); public static void main(String[] args) throws Exception { Path configPath = Paths.get("config.json"); Path currentDir = Paths.get("").toAbsolutePath(); try (BufferedReader reader = Files.newBufferedReader(configPath, Charset.forName("UTF-8"))) { String folderId = "***number removed for privacy***32"; //folderId of folder for which I want to download files (easiest to get this from the URL in box.com web UI) BoxFolder folder = new BoxFolder(client1, folderId); String folderName = folder.getInfo().getName(); Path localFolderPath = currentDir.resolve(Paths.get(folderName)); System.out.println("going to list the contents of the folder with ID of: " + folderId); mainParentFolder = folder.getInfo().getName(); System.out.println("going to list the contents of the folder with name of: " + mainParentFolder); //make the "All Files" folder in the root of wherever you are going to download the files String allFiles = "All Files"; String rootPlusAllFiles = (localRoot + "/" + allFiles); //if folder doesn't exist then create it Path allFilesPath = Paths.get(rootPlusAllFiles); if (!Files.exists(allFilesPath)) { System.out.println("Doesn't exist so I'll create it!"); allFilesPath = Files.createDirectory(allFilesPath); } //create the parent directory (whatever the String folderId = is set to) String mainRoot = mainParentFolder; String rootPlusAllFilesPlusMainRoot = (rootPlusAllFiles + "/" + mainRoot); //if folder doesn't exist then create it Path rootPlusAllFilesPlusMainRootPath = Paths.get(rootPlusAllFilesPlusMainRoot); if (!Files.exists(rootPlusAllFilesPlusMainRootPath)) { System.out.println("Doesn't exist so I'll create it!"); rootPlusAllFilesPlusMainRootPath = Files.createDirectory(rootPlusAllFilesPlusMainRootPath); } listFolder(folder, 4); String indent = ""; for (int i = 0; i < 10; i++) { indent += " "; } for (BoxItem.Info itemInfo : folder) { if (itemInfo instanceof BoxFile.Info) { BoxFile.Info fileInfo = (BoxFile.Info) itemInfo; BoxFile file = new BoxFile(client1, fileInfo.getID()); String localFilePath = localFolderPath.resolve(Paths.get(fileInfo.getName())).toAbsolutePath().toString(); FileOutputStream stream = new FileOutputStream(localFilePath); file.download(stream); stream.close(); } } } } public static void listFolder(BoxFolder folder, int depth) throws IOException, FileAlreadyExistsException { for (BoxItem.Info itemInfo : folder) { String indent = ""; for (int i = 0; i < depth; i++) { indent += " "; } System.out.println(); System.out.println(); System.out.println(indent + itemInfo.getName() + " [" + itemInfo.getID()+ "]"); /**List & Download Files*/ if (itemInfo instanceof BoxFile.Info) { BoxFile file = (BoxFile)itemInfo.getResource(); String folderID = itemInfo.getID(); String path = ""; BoxFile.Info file1 = new BoxFile(api1, folderID).getInfo(); // BoxFile file2 = (BoxFile) itemInfo.getResource(); for (BoxFolder.Info folder1 : file1.getPathCollection()) { path += "/" + folder1.getName(); } System.out.println("Path to file on box.com is: " + path); //set relativePath to be == path (since that was a way better way to handle this!) relativePath = path; //take the relative path of the file and add a slash plus the file name String relativePathWithFile = relativePath + "/" + itemInfo.getName(); System.out.println("relativePathWithFile: " + relativePathWithFile); String localPathWithFileImproved = (localRoot + relativePathWithFile); Path fileToDownloadTestToPath = Paths.get(localPathWithFileImproved); String fileToDownloadTestToPathBackToPathImproved = fileToDownloadTestToPath.toString(); System.out.println("fileToDownloadTestToPathBackToPathImproved: " + fileToDownloadTestToPathBackToPathImproved); //if the file doesn't exist then download it if (!Files.exists(fileToDownloadTestToPath)) { System.out.println("File: " + fileToDownloadTestToPathBackToPathImproved + " doesn't exist; we need to create it!\n"); FileOutputStream stream = new FileOutputStream(fileToDownloadTestToPathBackToPathImproved); file.download(stream); stream.close(); } else{ System.out.println("The local file exists: " + fileToDownloadTestToPathBackToPathImproved); } try { Metadata metadata = file.getMetadata(); System.out.println(indent + "metadata: " + metadata.toString()); System.out.println("folder of file is: " + folder.getInfo().getName()); } catch (BoxAPIException ae) { // 404 - you can't do anything about for now } } /**List & Create Folders*/ else if (itemInfo instanceof BoxFolder.Info) { BoxFolder childFolder = (BoxFolder) itemInfo.getResource(); String path = ""; BoxFolder.Info folder1 = new BoxFolder(client1, itemInfo.getID()).getInfo(); for (BoxFolder.Info folder2 : folder1.getPathCollection()) { path += "/" + folder2.getName(); } path += "/" + folder1.getName(); System.out.println("path of folder on box.com: " + path); Path currentDir = Paths.get("").toAbsolutePath(); String localRootSimpler = localRoot + path; Path localSubFolderPath = currentDir.resolve(Paths.get(localRootSimpler)); //if the folder doesn't exist then create it if (!Files.exists(localSubFolderPath)) { currentPath = localSubFolderPath.toString(); System.out.println("currentPath:" + currentPath); Files.createDirectory(localSubFolderPath); } else { System.out.println("The local directory exists: " + localSubFolderPath.toString()); } if (depth < 100) { listFolder(childFolder, depth + 1); } } } } // if you are insane and want to delete the files if they already exist... // I guess this could be modified so that you would delete only the files that are of a different size... static Path resetLocalFolder(Path localFolderPath) throws IOException { Files.list(localFolderPath).forEach(file -> { System.out.println(file.getFileName()); try { Files.delete(file.toAbsolutePath()); } catch (IOException e) { } }); Files.delete(localFolderPath); localFolderPath = Files.createDirectory(localFolderPath); return localFolderPath; } }
サインインしてコメントを残してください。
コメント
5件のコメント