Level up your Box knowledge with brand new learning paths on Box University. Visit training.box.com to get started

Comments

1 comment

  • Solve-X

    Here's my implementation for uploading a file.

    ```

            private async Task<bool> CallUploadApi(string year, int numberOfTry)
            {
                logger.LogInformation("Uploading to Box (try: {numberOfTry}).\n Filename: {year} {filename}", numberOfTry, year, filename);

                Thread.Sleep(1000);

                string uploadFileName = $"{year} {filename}";
                string filePath = Path.Combine(Directory.GetCurrentDirectory(), uploadFileName);

                using (var file = File.Open(filePath, FileMode.Open))
                {
                    if (file == null)
                    {
                        logger.LogInformation("File {year} {filename} does not exist.", year, filename);
                        return true;
                    }
                }

                using FileStream fileStream = new(filePath, FileMode.Open);

                BoxFile boxFile = new();
                string? fileId = await GetFileId(year);
                if (fileId == null)
                {
                    BoxFileRequest requestParams = new()
                    {
                        Name = uploadFileName,
                        Parent = new BoxRequestEntity() { Id = boxAuth.DesignatedFolderId },
                        Type = BoxType.file,
                    };
                    boxFile = await client.FilesManager.UploadAsync(requestParams, fileStream);
                    return true;
                }

                boxFile = await client.FilesManager.UploadNewVersionAsync(uploadFileName, fileId, fileStream);
                return true;
            }

    ```

    Filename is dependant on current year, ie. the same file should be overwritten (updated) with a new version of itself every week throughout the year. After NYE, the filename changes of course, so that one must be uploaded.

    Why would one method work, but not the other?

    My app (ClientId: g6pnmu4us74v9sk7b3ttl5v07yodyzf6) is set for Enterprise Access.

    The client is generated like so:

    ```
            private async Task<IBoxClient> GetBoxServiceAccountClient()
            {
                IBoxConfig boxConfig = new BoxConfigBuilder(boxAuth.ClientId, boxAuth.ClientSecret)
                    // .SetEnterpriseId(boxAuth.EnterpriseId)
                    .SetBoxApiHostUri(new Uri(boxAuth.ApiHostUri))
                    .SetBoxUploadApiUri(new Uri(boxAuth.UploadApiUri))
                    .SetBoxAccountApiHostUri(new Uri(boxAuth.AccountApiHostUri))
                    .Build();

                BoxCCGAuth boxCCG = new(boxConfig);

                string? userToken = cache.Get("boxAccessToken") as string;
                if (userToken == null)
                {
                    userToken = await boxCCG.UserTokenAsync("22776968333"); // Valid for 60 minutes
                    TimeSpan ttl = new(0, 50, 0);
                    cache.Set("boxAccessToken", userToken, ttl);
                }

                IBoxClient userClient = boxCCG.UserClient(userToken, "22776968333");

                userClient.Auth.SessionAuthenticated += (object o, SessionAuthenticatedEventArgs e) =>
                {
                    string newAccessToken = e.Session.AccessToken;
                };

                return userClient;
            }
    ```
    1
    Comment actions Permalink

Please sign in to leave a comment.