0

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

MiguelHoz
  • 49
  • 6

2 Answers2

1

Basically what you need to do is to create an HttpClient and then use this client to execute the image upload command, to make things more concrete take a look at this test senario here is the typical use of the commercetools JVM SDK for your purpose:

        //create client
        SphereClientConfig sphereClientConfig = SphereClientConfig.of( projectKey,  clientId,  clientSecret);
        SphereClient client = SphereClient.of(sphereClientConfig, SphereClientFactory.of().createHttpClient(), SphereAccessTokenSupplier.ofConstantToken("accessToken"))
        final ByIdVariantIdentifier identifier = product.getMasterData().getStaged().getMasterVariant().getIdentifier();
        File imageFile = new File("Path to your image");

        //create update commands
        final ProductImageUploadCommand cmd1 = ProductImageUploadCommand
                .ofVariantId(imageFile, identifier)
                .withFilename("myProductImage1.gif")
                .withStaged(true);
        final ProductImageUploadCommand cmd2 = ProductImageUploadCommand
                .ofVariantId(imageFile, identifier)
                .withFilename("myProductImage2.gif")
                .withStaged(true);

        //update the product
        final Product updatedProduct1 = client().executeBlocking(cmd1);
        final Product updatedProduct = client().executeBlocking(cmd2);

        //get the images
        List<Image> images = updatedProduct.getMasterData().getStaged().getMasterVariant().getImages();

Hope this helps :)

  • Thank you very much friend. I have not yet managed to make it work, but it has brought me closer to the solution since until now I was trying to create a simple client and not an HTTPCLIENT. I will keep you informed. Thanks again. – MiguelHoz Nov 08 '17 at 15:03
0

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.

MiguelHoz
  • 49
  • 6