Unable to access private file? I had this issue before, and last time the problem was that node was unpublished. The private file became accessible when the node was published. 

This time, however, I'm not using a node. I'm just using Drupals file manage system to store my own files for my own custom module. SO I generated a url for an image file like so:

$imageFile = file_load($userInput['fid']);
$style = \Drupal::entityTypeManager()-&gt;getStorage('image_style')-&gt;load('thumbnail');
$url = $style-&gt;buildUrl($imageFile-&gt;getFileUri());


If I print out the URL, I get:

http://localhost/system/files/styles/thumbnail/private/cfiles/test.png?itok=Le14o4Zs


But when I access that, i get "access denied".

What am I missing?
In Drupal 8, private file permissions by default are given according to the entity to which they are attached. If the user has permission to view the entity, they will have permission to view the file.

If you are not attaching the file to an entity, then you will need to implement hook_file_download(), where you will determine whether or not the user has permission to access the file, and return a value accordingly. Currently, because you have no implemented this, access is denied by default.

Note that this hook can be implemented even when the file is attached to an entity to provide additional access checks and/or additional headers.