0

我是 CommerceTools 的一名相当新的开发人员,我使用这个工具仅几个星期。

此时我需要开发一个流程,该流程应该能够使用 JVM API 将与产品相关的所有图像从文件夹上传到 commercetools。

我认为最好的方法是从 CTP 数据库中恢复每个产品的 SKU(例如 PROD001ABC),然后如果文件名中包含包含此类 SKU 的图像(PROD001ABC_front.jpg、PROD001ABC_side1. jpg、PROD001ABC_side2.jpg 等)。找到所有产品图片后,我想使用 API 将它们上传到 CommerceTools。

正如我所研究的那样,我认为我必须使用io.sphere.sdk.products.commands.ProductImageUploadCommand方法,但我不确定如何达到这一点。

我真的迷路了。

非常感谢您的帮助

此致。米格尔

4

2 回答 2

1

基本上,您需要做的是创建一个 HttpClient,然后使用此客户端执行图像上传命令,让事情变得更具体,看看这个测试场景 这里是 commercetools JVM SDK 的典型用途:

        //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();

希望这可以帮助 :)

于 2017-11-08T13:24:27.300 回答
0

好吧,我终于实现了。我使用我的产品 (EAN) 的属性来定位与“产品类型/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++;
            }
        }
    }
}

上传图像后,我将它们移动到另一个“已上传”子文件夹。

非常感谢您的帮助。

于 2017-12-01T09:05:42.970 回答