Created
January 3, 2018 01:02
-
-
Save allenmichael/8cd6754270fa5796e3049f4f1fa60a37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Box.V2.Config; | |
using Box.V2.JWTAuth; | |
namespace BoxGetAllUsers | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
ExecuteMainAsync().Wait(); | |
} | |
private static async Task ExecuteMainAsync() | |
{ | |
using (FileStream fs = new FileStream($"./config.json", FileMode.Open)) | |
{ | |
var session = new BoxJWTAuth(BoxConfig.CreateFromJsonFile(fs)); | |
var client = session.AdminClient(session.AdminToken()); | |
var folderId = "43491738095"; | |
var folder = await client.FoldersManager.GetInformationAsync(folderId); | |
var folderName = folder.Name; | |
var localFolderPath = Path.Combine(Directory.GetCurrentDirectory(), folderName); | |
ResetLocalFolder(localFolderPath); | |
var items = await client.FoldersManager.GetFolderItemsAsync(folderId, 1000, autoPaginate: true); | |
var fileDownloadTasks = new List<Task>(); | |
var files = items.Entries.Where(i => i.Type == "file"); | |
foreach (var file in files) | |
{ | |
fileDownloadTasks.Add(client.FilesManager.DownloadStreamAsync(file.Id).ContinueWith((t) => | |
{ | |
var localFile = File.Create(Path.Combine(localFolderPath, file.Name)); | |
return t.Result.CopyToAsync(localFile); | |
})); | |
} | |
await Task.WhenAll(fileDownloadTasks); | |
} | |
} | |
private static void ResetLocalFolder(string localFolderPath) | |
{ | |
if (!Directory.Exists(localFolderPath)) | |
{ | |
Directory.CreateDirectory(localFolderPath); | |
} | |
else | |
{ | |
foreach (var file in Directory.EnumerateFiles(localFolderPath)) | |
{ | |
File.Delete(Path.Combine(localFolderPath, file)); | |
} | |
Directory.Delete(localFolderPath); | |
Directory.CreateDirectory(localFolderPath); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment