magento2: Product import fails when external image urls are present

Preconditions (*)

  1. Magento Community 2.3.3 & 2.3-develop
  2. PHP 7.2.23

Steps to reproduce (*)

  1. Create a CSV product import file with 2 examples, one has external images (https://example.com/image.jpg), the other locally stored images
  2. Go to System > Data Transfer > Import and do the following:
  • Products (Entity Type)
  • Add/Update (Import Behavior)
  • Select the csv file to import
  • Leave ‘Images File Directory’ empty
  1. Import the file

Expected result (*)

  1. Products should be created, and the one having external images should have the image saved as well.

Actual result (*)

  1. Import fails for products with external images image

Reason of failure

After some debugging I found that the default product import path is being set on Magento\CatalogImportExport\Model\Import\Product:_getUploader() line 2043 and below.

When running the import, another method is called: Magento\CatalogImportExport\Model\Import\Uploader:move() which checks if the file is coming from an URL or from a local path. Regardless of where the image is coming from, the tmp path is set to the absolute path of the default import image directory <Magento installation>/var/import/images and the downloaded image is stored in var/tmp. When checking for the file permission before saving, the path that is checked is <Magento installation>/var/import/images/var/tmp/image_randstring.jpg, and since the file doesn’t exist in that path, the import fails. The correct path for the downloaded file should be <Magento installation>/var/tmp/image_randstring.jpg

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 22 (10 by maintainers)

Most upvoted comments

Ran into this issue too on Magento Commerce 2.3.3 Dug a little deeper and think I found the root cause of this issue.

Not sure if I fixed it in the correct way, but this POC patch might help anyone trying to fix this.

external-image-upload.patch.txt

I found another solution. In method: \Magento\CatalogImportExport\Model\Import\Uploader::_setUploadFile

        try {
            $fullPath = $this->_directory->getAbsolutePath($filePath);
            if ($this->getTmpDir()) {
                $tmpDir = $this->fileSystem->getDirectoryReadByPath(
                    $this->_directory->getAbsolutePath($this->getTmpDir())
                );
            } else {
                $tmpDir = $this->_directory;
            }
            $readable = $tmpDir->isReadable($fullPath);
        } catch (ValidatorException $exception) {
            $readable = false;
        }
        if (!$readable) {
            throw new \Magento\Framework\Exception\LocalizedException(
                __('File \'%1\' was not found or has read restriction.', $filePath)
            );
        }
        $this->_file = $this->_readFileInfo($filePath);

        $this->_validateFile();

Change to:

        try {
            $fullPath = $this->_directory->getAbsolutePath($filePath);
            if ($this->getTmpDir() && strpos($filePath, $this->downloadDir) !== 0) {
                $tmpDir = $this->fileSystem->getDirectoryReadByPath(
                    $this->_directory->getAbsolutePath($this->getTmpDir())
                );
            } else {
                $tmpDir = $this->_directory;
            }
            $readable = $tmpDir->isReadable($fullPath);
        } catch (ValidatorException $exception) {
            $readable = false;
        }
        if (!$readable) {
            throw new \Magento\Framework\Exception\LocalizedException(
                __('File \'%1\' was not found or has read restriction.', $filePath)
            );
        }
        $this->_file = $this->_readFileInfo($filePath);

        $this->_validateFile();

@Nagamaiah007 , this would work with any csv file you try. But anyway, here’s the file (renamed it to txt as github doesn’t allow csv for some reason): export_catalog_product_test_external_image1.txt

local image: 41jZY1tUV9L SX395_QL70

Note that I took these images from google, they’re not my own.