我是 CommerceTools 的一名新开发人员,我已经使用这个工具工作了几个星期。
目前,我需要开发一个流程,该流程应该能够使用 JVM API 将与产品相关的所有图像从文件夹上传到 commercetools。
我认为最好的方法是从 CTP 数据库中恢复每个产品的 SKU(例如 PROD001ABC),然后如果文件名(PROD001ABC_front.jpg、PROD001ABC_side1.jpg、PROD001ABC_side1.jpg、PROD001ABC_side1.jpg)中有包含此类 SKU 的图像,则使用此字符串在给定文件夹中进行定位。 jpg、PROD001ABC_side2.jpg 等)。找到所有产品图片后,我想使用 API 将它们上传到 CommerceTools。
根据我的研究,我认为我必须使用io.sphere.sdk.products.commands.ProductImageUploadCommand方法,但我不确定如何达到这一点。
我真的很失落。
非常感谢您的帮助
此致。米格尔
基本上你需要做的是创建一个 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();
希望这可以帮助 :)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句