Welcome to the new Box Support website. Check out all the details here on what’s changed.

Java Box SDK - Thumbnail Not Retrieved On First Request

Answered
New post

Comments

4 comments

  • sgarlanka

    There should no reason this is failing on the first attempt. Your code looks right. If there is any other relevant information you can give that you think may be the source of the issue, I can look at it further.

    0
    Comment actions Permalink
  • mbrooks11

    Hey, thanks for the reply. I really don't have any ideas what is causing this but I can share a little more about how we are using the API.

     

    We have a web application where users can upload files and we do some editing to the file. The edited file ends up as a .TIFF but the same behavior can be seen with a .PNG. The files are uploaded via the API like this (some error checking and a call to our database removed for brevity);

     

    public String uploadFile(String parentFolderId, String fileName, byte[] fileBytes, String username){
    	String resultingId = null;
    	BoxFolder myFolder = new BoxFolder(this.api, parentFolderId);
    	InputStream fileInput = new ByteArrayInputStream(fileBytes);
    	BoxFile.Info fileInfo = myFolder.uploadFile(fileInput, fileName); 
    	resultingId = fileInfo.getID();
    	return resultingId;
    }

     

    parentFolderId is the box ID for the folder we are storing these files in. The files upload correctly and are visible in box's UI. I don't think how we upload the file is an issue but I'm trying to give you any helpful context I can.

     

    Here is a main method that also demonstrates the behavior for me. This approach uses a developer token instead of the config.json. If I debug this and sit on the "waiting..." statement for a few seconds (maybe 5 seconds), the subsequent thumbnail request always works. It doesn't matter how long I wait between the uploadFile() and the first getThumbnail() call - it always returns me an empty array. Box folder ID, developer token and test image path have been masked.

     

    	public static void main(String[] args) throws IOException{
    		System.out.println("starting...");
    		String parentFolderId = "###########";
    		String filePath = "C:/path/to/an/image.png";
    		File file = new File(filePath);
    		FileInputStream fis = new FileInputStream(file);
    		String filename = "mytestfile.png";
    		
    		BoxAPIConnection api = new BoxAPIConnection("################################");
    		
    		BoxFolder myFolder = new BoxFolder(api, parentFolderId);
    		BoxFile.Info fileInfo = myFolder.uploadFile(fis, filename);
    		String resultingId = fileInfo.getID();
    		
    		BoxFile boxfile = new BoxFile(api, resultingId);
    		byte[] thumbnailBytes = boxfile.getThumbnail(BoxFile.ThumbnailFileType.PNG, 32, 32, 32, 32);
    		
    		System.out.println("waiting...");
    		thumbnailBytes = boxfile.getThumbnail(BoxFile.ThumbnailFileType.PNG, 32, 32, 32, 32);
    		System.out.println("done");
    	}

     

    Other requests using the API on this file work on the first request (e.g. getting the full size image or the file name in box) but only the thumbnail is giving us trouble.

    0
    Comment actions Permalink
  • sgarlanka

    Hello,

    Sorry for the delayed response. Take a look at the thumbnail docs at https://developer.box.com/reference. In the case of a 202 status code a thumbnail is not yet generated and you would have to retry the request after a certain amount of time. I would check if a 202 status code is returned from the call. If this is the case, then what is happening is expected behavior. One way to check is to manually make a Postman request. This is a collection of Box API Endpoints for postman: https://developer.box.com/docs/box-postman-collection. The other option is to go into the getThumbnail() method in your project (https://github.com/box/box-java-sdk/blob/master/src/main/java/com/box/sdk/BoxFile.java) and print out the response to check the status code.

    0
    Comment actions Permalink
  • mbrooks11

    Yes, this was the case. Thanks for the help! 

    0
    Comment actions Permalink

Please sign in to leave a comment.