javajvmcommercetoolssphere.io

How can I automate image upload to commercetools using JVM API


I'm a pretty new developer in CommerceTools and I've been working with this tool for just a few weeks.

At this moment I need to develop a process that should be able to upload all the images related to a product from a folder to commercetools using the JVM API.

I think the best way would be to recover the SKU (eg PROD001ABC) of each product from the CTP database and then use this string to locate in the given folder if there are images containing such SKU in the filename (PROD001ABC_front.jpg, PROD001ABC_side1.jpg, PROD001ABC_side2.jpg, etc.). Once all the product images are located, I want to upload them to CommerceTools using the API.

As I've researched, I think I'd have to use the io.sphere.sdk.products.commands.ProductImageUploadCommand method, but I'm not sure how to get to that point.

I'm really lost.

Thanks so much for any help

Best regards. Miguel


Solution

  • Well, finally I have achieved it. I used an attribute of my products (EAN) to locate the images in the path corresponding to "product type / EAN".

    // Buscamos una carpeta con el nombre del EAN
    final String pathCarpetaEan = rutaBaseLocal + "\\" + typeName + "\\" + vEan;
    final File carpetaEAN = new File(pathCarpetaEan);
    final File carpetaEanSubidas = new File(pathCarpetaEan + "\\subidas\\");
    if (carpetaEAN.exists() && carpetaEAN.isDirectory()) {
        // La carpeta existe. Buscamos imagenes dentro.
        System.out.println("Encontrada la carpeta " + pathCarpetaEan);
        File[] fileImages = carpetaEAN.listFiles();
    
        for (File fileImagen : fileImages) {
            if (fileImagen.isFile()) {
                final String nomImagen = fileImagen.getName();
                System.out.println("---\nNombre fichero: " + nomImagen);
                if (nomImagen.startsWith(vEan)
                        && (nomImagen.toLowerCase().endsWith(JPEG)
                        || nomImagen.toLowerCase().endsWith(PNG)
                        || nomImagen.toLowerCase().endsWith(GIF))) {
                    System.out.println("El nombre de archivo contiene el EAN: " + vEan);
                    System.out.println("Y se trata de una imagen");
                    // A partir de aqui realizamos la subida de las imagenes para la variante concreta.
                    final ProductImageUploadCommand cmd = ProductImageUploadCommand
                            .ofVariantId(fileImagen, identificador)
                            .withFilename(nomImagen)
                            .withStaged(true);
                    final Product updatedProduct = client.executeBlocking(cmd);
                    System.out.println("Uploaded your image (" + nomImagen + ") and added to the masterVariant");
                    System.out.println("Producto actualizado: " + updatedProduct.toString());
                    nUploadedImages++;
                }
            }
        }
    }
    

    After uploading the images, I move them to another subfolder "uploaded".

    Thank you very much for your help.