我是CommerceTools的一个非常新的开发人员,我已经使用这个工具仅仅几个星期了。
此时,我需要开发一个过程,它应该能够使用JVM将与产品相关的所有图像从文件夹上传到commercetools。
我认为最好的方法是从CTP数据库中恢复每个产品的SKU (例如PROD001ABC),然后如果文件名中有包含这种SKU的图像(PROD001ABC_front.jpg、PROD001ABC_side1.jpg、PROD001ABC_side2.jpg等),则使用这个字符串来定位给定的文件夹。一旦找到所有的产品图像,我想使用API将它们上传到CommerceTools。
正如我所研究的,我认为我必须使用io.sphere.sdk.products.commands.ProductImageUploadCommand方法,但我不知道如何达到这个目的。
我真的迷路了。
非常感谢你的帮助
诚挚的问候。米格尔
发布于 2017-11-08 13:24:27
基本上,您需要做的是创建一个HttpClient,然后使用这个客户机执行图像上传命令,为了使事情更加具体,请看一下这个测试老师,这里是commercetools的典型用途:
//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-12-01 09:05:42
我终于做到了。我使用我的产品(EAN)的一个属性来定位与"product / 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++;
}
}
}
}上传图片后,我将它们移动到另一个“上传”子文件夹。
非常感谢你的帮助。
https://stackoverflow.com/questions/47177260
复制相似问题