box / box-windows-sdk-v2

Windows SDK for v2 of the Box API. The SDK is built upon .NET Framework 4.5

Home Page:https://developer.box.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

404 error when upload file

nikita-ivanov-siliconmint opened this issue · comments

Description of the Issue

I get this exception when I try to upload file. I found similar issue on forum, and it says that this exception about user doesn't have enough permissions. But it's not my case, because I am able to upload file to the same folder with same credentials (sometimes it works, sometimes it produces an error). I attached screenshot of our application logs. We haven't changed our code for several months and started to gain this error since August of 14

Expected Behavior

File uploads successfully in all cases.

Error Message, Including Stack Trace

An unhandled exception has occurred while executing the request.
      Box.V2.Exceptions.BoxException: The API returned an error [NotFound | dotupjgtk5kx4fxn.] not_found - Not Found
         at Box.V2.Extensions.BoxResponseExtensions.ParseResults[T](IBoxResponse`1 response, IBoxConverter converter)
         at Box.V2.Managers.BoxResourceManager.ToResponseAsync[T](IBoxRequest request, Boolean queueRequest)
         at Box.V2.Managers.BoxFilesManager.UploadAsync(BoxFileRequest fileRequest, Stream stream, IEnumerable`1 fields, Nullable`1 timeout, Byte[] contentMD5, Boolean setStreamPositionToZero, Uri uploadUri)
         at MerchantDocumentsService.BoxClient.BoxClient.UploadDocumentAsync(String folderId, String fileName, Stream contentStream) in /app/MerchantDocumentsService.BoxClient/BoxClient.cs:line 116
         at MerchantDocumentsService.Domain.MerchantDocumentsService.UploadDocument(String advanceRequestId, String documentName, Stream stream) in /app/MerchantDocumentsService.Domain/MerchantDocumentsService.cs:line 62
         at MerchantDocumentsService.Api.Controllers.DocumentController.UploadDocument(String merchantAdvanceRequestId, IFormFile file) in /app/MerchantDocumentsService.Api/Controllers/DocumentController.cs:line 40
         at MerchantDocumentsService.Api.Controllers.DocumentController.UploadDocument(String merchantAdvanceRequestId, IFormFile file) in /app/MerchantDocumentsService.Api/Controllers/DocumentController.cs:line 40

Code

Method we use to upload file:

public async Task<string> UploadDocumentAsync(string folderId, string fileName, Stream contentStream)
{
    var adminClient = GetAdminClient();
    _logger.LogInformation($"{nameof(UploadDocumentAsync)}: Upload file to Box, folderId='{folderId}', fileName='{fileName}'");
    BoxFile boxFile;

    try
    {
        boxFile = await adminClient.FilesManager.UploadAsync(new BoxFileRequest
        {
            Name = fileName,
            Parent = new BoxRequestEntity
            {
                Id = folderId
            },
        }, contentStream);

        return boxFile.Id;
    }
    catch (BoxPreflightCheckConflictException<BoxFile> boxException)
    {
        var existingFileId = boxException.ConflictingItem?.Id;

        if (existingFileId == null)
        {
            _logger.LogWarning($"{nameof(UploadDocumentAsync)}: File conflict, but Id not provided");
            throw;
        }

        _logger.LogInformation($"{nameof(UploadDocumentAsync)}: File conflict, conflict file id='{existingFileId}'");
        await adminClient.FilesManager.DeleteAsync(existingFileId);

        _logger.LogInformation($"{nameof(UploadDocumentAsync)}: File conflict, file deleted, start to load new file");
        boxFile = await adminClient.FilesManager.UploadAsync(new BoxFileRequest
        {
            Name = fileName,
            Parent = new BoxRequestEntity
            {
                Id = folderId
            },
        }, contentStream);
    }

    _logger.LogInformation($"{nameof(UploadDocumentAsync)}: Result file Id ='{boxFile.Id}'");
    return boxFile.Id;
}

Method we use to get client:

private Box.V2.BoxClient GetAdminClient()
{
    _logger.LogInformation($"{nameof(GetAdminClient)}: Generate Admin Client");

    var boxConfig = new BoxConfig(_options.ClientId,
        _options.ClientSecret,
        _options.EnterpriseId,
        _options.PrivateKey,
        _options.PrivateKeyPass,
        _options.PublicKeyId);
    var boxJWT = new BoxJWTAuth(boxConfig);

    var adminToken = boxJWT.AdminToken();
    return boxJWT.AdminClient(adminToken, _options.AsUserId);
}

Screenshots

image

Versions Used

.Net SDK: .NET Standard 2.1
Windows: Windows 10

commented

This issue has been automatically marked as stale because it has not been updated in the last 30 days. It will be closed if no further activity occurs within the next 7 days. Feel free to reach out or mention Box SDK team member for further help and resources if they are needed.

commented

This issue has been automatically closed due to maximum period of being stale. Thank you for your contribution to Box .NET SDK and feel free to open another PR/issue at any time.