Import media files

In most cases, the import of media files will be some kind of complicated, as the amount of data that has to be moved around can quickly be huge. Pacemaker offers a good solution to handle that in a comprehensive way.

In general, this means that Pacemaker doesn’t take care, that the images will be moved from the directory, where they have been uploaded, to the magento pub/media/catalog/product directory. Instead the upload directory will be symlinked to Magento’s media directory.

Directory structure

The proven directory structure looks like the following one, wheras this one also contains the necessary directories for the 360° images, which may also be of interest in many cases.

/
└--var/
   └--www/
      └--sftp/
         └--import/
            |--media/
            |  └--images/
            |     |--00490826/
            |     |  |--general/
            |     |  |  |--base_image.jpg
            |     |  |  └--small_image.jpg
            |     |  |--additional/
            |     |  |  |--additional_image_01.jpg
            |     |  |  └--additional_image_02.jpg
            |     |  └--360/
            |     |     |--10584391-SPK-001--_01.JPG
            |     |     |--10584391-SPK-001--_02.JPG
            |     |     |--10584391-SPK-001--_03.JPG
            |     |     |--10584391-SPK-001--_04.JPG
            |     |     |--10584391-SPK-001--_05.JPG
            |     |     └--10584391-SPK-001--_06.JPG
            |     └--00490794/
            |        |--general/
            |        |  |--base_image.jpg
            |        |  └--small_image.jpg
            |        |--additional/
            |        |  |--additional_image_01.jpg
            |        |  └--additional_image_02.jpg
            |        └--360/
            |           |--10584352-SAK-001-G_01.JPG
            |           |--10584352-SAK-001-G_02.JPG
            |           |--10584352-SAK-001-G_03.JPG
            |           |--10584352-SAK-001-G_04.JPG
            |           |--10584352-SAK-001-G_05.JPG
            |           └--10584352-SAK-001-G_06.JPG
            └--data/
               |--category-import_20191204-140200_01.csv
               |--category-import_20191204-140200_01.ok
               |--product-import_20191204-140200_01.csv
               └--product-import_20191204-140200_01.ok

Magento REQUIRES exactly a two level folder structure to make the image rendering work. This means, that after the SKU, exactly one additional folder, that finally contains the image files, e. g. general, MUST be available. This is, because Magento creates the image cache on-the-fly, based on the method \Magento\MediaStorage\App\Media::getOriginalImage() (if this has not already been done with the bin/magento catalog:image:resize command) which uses a regular expression that cuts off everything BEFORE those three levels. This leads to the issue that the path to the original file under the directory pub/media/catalog/product will not be found and instead the image the placeholder will be rendered.

Path in CSV files

The path to the images in the CSV files have to be relative to the Magento pub/media directory. Additionally, they have to start with a slash. For example, the files in the structure above MUST have the following format in the CSV file product-import_20191204-140200_01.csv

sku

base_image

small_image

is_360

images_360

00490826

/00490826/general/base_image.jpg

/00490826/general/small_image.jpg

Yes

/00490826/360

00490794

/00490794/general/base_image.jpg

/00490794/general/small_image.jpg

Yes

/00490794/360

When the directory structure has been created, also the symlinks can be created. This can be done by

ln -s <magento-install-dir>/var/pacemaker/import /var/www/sftp/import/data \
  && ln -s <magento-install-dir>/pub/media/catalog/product /var/www/sftp/import/media/images \
  && ln -s <magento-install-dir>/pub/media/magic360 /var/www/sftp/import/media/images

Import

Beside the automatic import by the apropriate pipeline, it is also possible to import the data manually. For debugging purposes this can be useful, as in some cases error messages will be rendered on the CLI and finding errors can then be a easier.

cd <magento-install-dir> \
  && rm var/pacemaker/import/*.csv \
  && cp <sample-data-dir>/* var/pacemaker/import \
  && vendor/bin/pacemaker import:products add-update \
    --serial=import \
    --source-dir=var/pacemaker \
    --target-dir=var/pacemaker \
    --magento-version=2.3.4 \
    --magento-edition=EE