I'm making migration plugin which I'm using in first migration and it is creating private file like this:

if ($final_destination) {
  // Create a file entity.
  $file = File::create([
    'uri' => $final_destination,
    'uid' => $uid,
    'status' => FILE_STATUS_PERMANENT,

  $uri = $file->getFileUri();

  $stream = new S3fsStream();

  if ($stream->waitUntilFileExists($uri)) {
    return $file->getFileUri();

Then, second migration should create media entities, by using those files (with migration lookup plugin).

However, when I run second migration I get error message:

media: field_image.0=You do not have access to the referenced entity file: 46

If file is public then it works, but how can I reference private files? The error is triggered in ReferenceAccessConstraintValidator.php.

  // We check that the current user had access to view any newly added
  // referenced entity.
  if ($check_permission && !$referenced_entity->access('view')) {
    $type = $value->getFieldDefinition()->getSetting('target_type');
    $this->context->addViolation($constraint->message, ['%type' => $type, '%id' => $id]);
  • Make sure that your user has access to that private file, otherwise you can't refer nor access it. Dec 14, 2021 at 23:42
  • On the other hand, when I did the migration, I never had to create plugin to create media. But you have to set the media field to save files to the private directory (obviously). If you have an URL (even with external) you just need to set it to the file filed and the provided plugin will download it for you - if I remember well as it was long time ago. Dec 14, 2021 at 23:47

1 Answer 1


Found out that this was the known issue and is fixed in Drupal 9.3. With previous versions patch should be applied:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.