如何使用 JVM 自动将图像上传到 commercetools API

How can I automate image upload to commercetools using JVM API

我是 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 方法,但我不确定如何达到这一点。

我真的迷路了

非常感谢您的帮助

此致。 米格尔

基本上你需要做的就是创建一个HttpClient,然后使用这个客户端来执行图片上传命令,更具体的看一下this test senario 以下是用于您目的的 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();

希望对您有所帮助:)

嗯,我终于实现了。 我使用我的产品 (EAN) 的一个属性在 "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++;
            }
        }
    }
}

上传图片后,我将它们移动到另一个子文件夹"uploaded"。

非常感谢您的帮助。