Dimostra come eliminare un gruppo di prodotti in un set di prodotti.
Per saperne di più
Per la documentazione dettagliata che include questo esempio di codice, consulta quanto segue:
Esempio di codice
Go
import (
"context"
"fmt"
"io"
vision "cloud.google.com/go/vision/apiv1"
visionpb "google.golang.org/genproto/googleapis/cloud/vision/v1"
)
// purgeProductsInProductSet deletes all products in a product set.
func purgeProductsInProductSet(w io.Writer, projectID string, location string, productSetID string) error {
// projectID := "your-gcp-project-id"
// location := "us-west1"
// productSetID := "sampleProductSetID"
ctx := context.Background()
c, err := vision.NewProductSearchClient(ctx)
if err != nil {
return fmt.Errorf("NewProductSearchClient: %v", err)
}
defer c.Close()
req := &visionpb.PurgeProductsRequest{
Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
Target: &visionpb.PurgeProductsRequest_ProductSetPurgeConfig{
ProductSetPurgeConfig: &visionpb.ProductSetPurgeConfig{
ProductSetId: productSetID,
},
},
Force: true,
}
// The purge operation is async.
op, err := c.PurgeProducts(ctx, req)
if err != nil {
return fmt.Errorf("PurgeProducts: %v", err)
}
fmt.Fprintf(w, "Processing operation name: %q\n", op.Name())
if err := op.Wait(ctx); err != nil {
return fmt.Errorf("Wait: %v", err)
}
fmt.Fprintf(w, "Deleted products in product set.\n")
return nil
}
Java
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.vision.v1.BatchOperationMetadata;
import com.google.cloud.vision.v1.LocationName;
import com.google.cloud.vision.v1.ProductSearchClient;
import com.google.cloud.vision.v1.ProductSetPurgeConfig;
import com.google.cloud.vision.v1.PurgeProductsRequest;
import com.google.protobuf.Empty;
import java.util.concurrent.TimeUnit;
public class PurgeProductsInProductSet {
// Delete all products in a product set.
public static void purgeProductsInProductSet(
String projectId, String location, String productSetId) throws Exception {
// String projectId = "YOUR_PROJECT_ID";
// String location = "us-central1";
// String productSetId = "YOUR_PRODUCT_SET_ID";
// boolean force = true;
try (ProductSearchClient client = ProductSearchClient.create()) {
String parent = LocationName.format(projectId, location);
ProductSetPurgeConfig productSetPurgeConfig =
ProductSetPurgeConfig.newBuilder().setProductSetId(productSetId).build();
PurgeProductsRequest request =
PurgeProductsRequest.newBuilder()
.setParent(parent)
.setProductSetPurgeConfig(productSetPurgeConfig)
// The operation is irreversible and removes multiple products.
// The user is required to pass in force=True to actually perform the
// purge.
// If force is not set to True, the service raises an exception.
.setForce(true)
.build();
OperationFuture<Empty, BatchOperationMetadata> response = client.purgeProductsAsync(request);
response.getPollingFuture().get(180, TimeUnit.SECONDS);
System.out.println("Products removed from product set.");
}
}
}
Node.js
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');
// Creates a client
const client = new vision.ProductSearchClient();
async function purgeProductsInProductSet() {
// Deletes all products in a product set.
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const projectId = 'Your Google Cloud project Id';
// const location = 'A compute region name';
// const productSetId = 'Id of the product set';
const formattedParent = client.locationPath(projectId, location);
const purgeConfig = {productSetId: productSetId};
// The operation is irreversible and removes multiple products.
// The user is required to pass in force=true to actually perform the purge.
// If force is not set to True, the service raises an error.
const force = true;
try {
const [operation] = await client.purgeProducts({
parent: formattedParent,
productSetPurgeConfig: purgeConfig,
force: force,
});
await operation.promise();
console.log('Products removed from product set.');
} catch (err) {
console.log(err);
}
}
purgeProductsInProductSet();
Python
from google.cloud import vision
def purge_products_in_product_set(
project_id, location, product_set_id, force):
"""Delete all products in a product set.
Args:
project_id: Id of the project.
location: A compute region name.
product_set_id: Id of the product set.
force: Perform the purge only when force is set to True.
"""
client = vision.ProductSearchClient()
parent = f"projects/{project_id}/locations/{location}"
product_set_purge_config = vision.ProductSetPurgeConfig(
product_set_id=product_set_id)
# The purge operation is async.
operation = client.purge_products(request={
"parent": parent,
"product_set_purge_config": product_set_purge_config,
# The operation is irreversible and removes multiple products.
# The user is required to pass in force=True to actually perform the
# purge.
# If force is not set to True, the service raises an exception.
"force": force
})
operation.result(timeout=500)
print('Deleted products in product set.')
Passaggi successivi
Per cercare e filtrare esempi di codice per altri prodotti Google Cloud, vedi il browser di esempio Google Cloud.