/**
* Import images of different products in the product set.
*
* @param projectId - Id of the project.
* @param computeRegion - Region name.
* @param gcsUri - Google Cloud Storage URI.Target files must be in Product Search CSV format.
* @throws Exception - on client errors.
*/
public static void importProductSets(String projectId, String computeRegion, String gcsUri)
throws Exception {
try (ProductSearchClient client = ProductSearchClient.create()) {
// A resource that represents Google Cloud Platform location.
String formattedParent = ProductSearchClient.formatLocationName(projectId, computeRegion);
Builder gcsSource = ImportProductSetsGcsSource.newBuilder().setCsvFileUri(gcsUri);
// Set the input configuration along with Google Cloud Storage URI
ImportProductSetsInputConfig inputConfig =
ImportProductSetsInputConfig.newBuilder().setGcsSource(gcsSource).build();
// Import the product sets from the input URI.
OperationFuture<ImportProductSetsResponse, BatchOperationMetadata> response =
client.importProductSetsAsync(formattedParent, inputConfig);
System.out.println(String.format("Processing operation name: %s", response.getName()));
ImportProductSetsResponse results = response.get();
System.out.println("Processing done.");
System.out.println("Results of the processing:");
for (int i = 0; i < results.getStatusesCount(); i++) {
System.out.println(
String.format(
"Status of processing line %s of the csv: %s", i, results.getStatuses(i)));
// Check the status of reference image.
if (results.getStatuses(i).getCode() == 0) {
ReferenceImage referenceImage = results.getReferenceImages(i);
System.out.println(referenceImage);
} else {
System.out.println("No reference image.");
}
}
}
}
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');
// Creates a client
const client = new vision.ProductSearchClient();
async function importProductSets() {
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const projectId = 'Your Google Cloud project Id';
// const location = 'A compute region name';
// const gcsUri = 'Google Cloud Storage URI. Target files must be in Product Search CSV format';
// A resource that represents Google Cloud Platform location.
const projectLocation = client.locationPath(projectId, location);
// Set the input configuration along with Google Cloud Storage URI
const inputConfig = {
gcsSource: {
csvFileUri: gcsUri,
},
};
// Import the product sets from the input URI.
const [response, operation] = await client.importProductSets({
parent: projectLocation,
inputConfig: inputConfig,
});
console.log('Processing operation name: ', operation.name);
// synchronous check of operation status
const [result] = await response.promise();
console.log('Processing done.');
console.log('Results of the processing:');
for (const i in result.statuses) {
console.log(
'Status of processing ',
i,
'of the csv:',
result.statuses[i]
);
// Check the status of reference image
if (result.statuses[i].code === 0) {
console.log(result.referenceImages[i]);
} else {
console.log('No reference image.');
}
}
}
importProductSets();
def import_product_sets(project_id, location, gcs_uri):
"""Import images of different products in the product set.
Args:
project_id: Id of the project.
location: A compute region name.
gcs_uri: Google Cloud Storage URI.
Target files must be in Product Search CSV format.
"""
client = vision.ProductSearchClient()
# A resource that represents Google Cloud Platform location.
location_path = f"projects/{project_id}/locations/{location}"
# Set the input configuration along with Google Cloud Storage URI
gcs_source = vision.ImportProductSetsGcsSource(
csv_file_uri=gcs_uri)
input_config = vision.ImportProductSetsInputConfig(
gcs_source=gcs_source)
# Import the product sets from the input URI.
response = client.import_product_sets(
parent=location_path, input_config=input_config)
print('Processing operation name: {}'.format(response.operation.name))
# synchronous check of operation status
result = response.result()
print('Processing done.')
for i, status in enumerate(result.statuses):
print('Status of processing line {} of the csv: {}'.format(
i, status))
# Check the status of reference image
# `0` is the code for OK in google.rpc.Code.
if status.code == 0:
reference_image = result.reference_images[i]
print(reference_image)
else:
print('Status code not OK: {}'.format(status.message))