Score:0

Is it possible to generate a link to a private file that can be shared with an anonymous user to download the file?

ng flag

In Drupal 8 we need to generate a link to a private file that can be shared with an anonymous user to access.

In our function we have the file object for the private file and are currently generating the link to the file like this:

   $build['#link'] = Link::fromTextAndUrl(t('Download original file: ' . $file_name), Url::fromUri($url, $options))->toRenderable();

However, the link generated is only accessible to privileged users.

in flag
It is fundamentally opposed for an anonymous user to have access to private files. Files that are intended to be available for anonymous users should be stored in public files. I recommend this approach. if you absolutely must grant private file access to anon users, then you can grant the View private files permission. I do not recommend this approach.
quantumized avatar
ng flag
I understand the concerned but for this use case it's pretty safe as it's a low security site. I ended up using hook_file_download and a query perimeter using the files FID. This combination provides a decent level of security allowing a link to be provided with a unique perimeter to be checked before allowing the download.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.