Java 如何使用JVMAPI自动将图像上传到commercetools

Java 如何使用JVMAPI自动将图像上传到commercetools,java,jvm,commercetools,sphere.io,Java,Jvm,Commercetools,Sphere.io,我是CommerceTools的一名新开发人员,我使用这个工具才几个星期 目前,我需要开发一个流程,该流程应该能够使用JVMAPI将与产品相关的所有图像从文件夹上传到commercetools 我认为最好的方法是从CTP数据库中恢复每个产品的SKU(例如PROD001ABC),然后如果文件名(PROD001ABC_-front.jpg、PROD001ABC_-side1.jpg、PROD001ABC_-side2.jpg等)中存在包含此类SKU的图像,则使用此字符串在给定文件夹中进行定位。 一旦

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

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

我认为最好的方法是从CTP数据库中恢复每个产品的SKU(例如PROD001ABC),然后如果文件名(PROD001ABC_-front.jpg、PROD001ABC_-side1.jpg、PROD001ABC_-side2.jpg等)中存在包含此类SKU的图像,则使用此字符串在给定文件夹中进行定位。 一旦找到所有的产品图片,我想使用API将它们上传到CommerceTools

正如我所研究的,我认为我必须使用这种方法,但我不确定如何达到这一点

我真的迷路了

非常感谢你的帮助

致以最良好的祝愿。
Miguel

基本上,您需要做的是创建一个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();
//创建客户端
SphereClientConfig SphereClientConfig=SphereClientConfig.of(projectKey、clientId、clientSecret);
SphereClient=SphereClient.of(sphereClientConfig,SphereClientFactory.of().createHttpClient(),SphereAccessTokenSupplier.of ConstantToken(“accessToken”))
最终ByIdVariantIdentifier标识符=product.getMasterData().getStaged().getMasterVariant().getIdentifier();
File imageFile=新文件(“图像路径”);
//创建更新命令
最终ProductImageUploadCommand cmd1=ProductImageUploadCommand
.ofVariantId(图像文件,标识符)
.withFilename(“myProductImage1.gif”)
.带有标记(正确);
最终ProductImageUploadCommand cmd2=ProductImageUploadCommand
.ofVariantId(图像文件,标识符)
.withFilename(“myProductImage2.gif”)
.带有标记(正确);
//更新产品
最终产品updatedProduct1=client().ExecuteBlock(cmd1);
最终产品updatedProduct=client().ExecuteBlock(cmd2);
//获取图像
列表图像=updatedProduct.getMasterData().getStaged().getMasterVariant().getImages();

希望这有帮助:)

好吧,我终于做到了。 我使用我的产品(EAN)的一个属性来定位对应于“产品类型/EAN”的路径中的图像

上传完图片后,我将它们移动到另一个子文件夹“Upload”


非常感谢您的帮助。

非常感谢您的朋友。我还没有设法使它工作,但它使我更接近解决方案,因为直到现在我一直在尝试创建一个简单的客户端,而不是一个HTTPCLIENT。我会通知你的。再次感谢。
// 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++;
            }
        }
    }
}