Cron task does not create images I've made a module which will parse a few RSS feeds, and automatically creates new content. But I've got an issue creating images.

Since D7, cron are run anonymously, so I think my problem comes from here.
Is there a way to allow anonymous user to create images?

Here's the way I'm doing it. It works if I call FeedController::import via a route, and connected ; but not if I call it through Drupal's cron task.

rssfeed.module

&lt;?php
use Drupal\rssfeed\Controller\FeedController;

function rssfeed_cron() {
  FeedController::import();
}


src/Controller/FeedController.php::import

public function import()
{
  $params = array();
  // some stuff to fill $params array

  $hash = ...;
  $src  = ...;
  $alt  = ...;

  $data = file_get_contents($src);
  $size = getimagesize($src);
  $extension = image_type_to_extension($size[2]);
  $file = file_save_data(
    $data, 
    sprintf('public://rss/%s.%s', $hash, $extension), 
    FILE_EXISTS_REPLACE
  );

  if ($file) {
    $params['field_image_main'] = array(
      'target_id' =&gt; $file-&gt;id(),
      'alt' =&gt; $alt
    );
  } 

  $node = Node::create($params);
  $node-&gt;save();
}
As a possible (but possibly not the best) solution it might be the 'account_switcher' service could help. It would elevate permissions for your cron job. 

use Drupal\Core\Session\UserSession;

// call the account switcher service
$accountSwitcher = \Drupal::service('account_switcher');
// switch to the admin user
$accountSwitcher-&gt;switchTo(new UserSession(['uid' =&gt; 1]));

// run your code here

// switch back to old session
$accountSwitcher-&gt;switchBack();


zessx, please share your solution, if you've found one.

See original Q&amp;A here.