|
| 1 | +# Copyright 2021 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import re |
| 17 | +import time |
| 18 | + |
| 19 | +from google.cloud.storage.bucket import Bucket |
| 20 | + |
| 21 | +from google.cloud import storage |
| 22 | +from google.cloud.retail import GcsSource, ImportErrorsConfig, \ |
| 23 | + ImportProductsRequest, ProductInputConfig |
| 24 | +from google.cloud.retail_v2 import ProductServiceClient |
| 25 | + |
| 26 | +project_number = os.getenv('PROJECT_NUMBER') |
| 27 | +bucket_name = os.getenv('BUCKET_NAME') |
| 28 | +storage_client = storage.Client() |
| 29 | +resource_file = "resources/products.json" |
| 30 | +object_name = re.search('resources/(.*?)$', resource_file).group(1) |
| 31 | +default_catalog = "projects/{0}/locations/global/catalogs/default_catalog/branches/default_branch".format( |
| 32 | + project_number) |
| 33 | + |
| 34 | + |
| 35 | +def create_bucket(bucket_name: str) -> Bucket: |
| 36 | + """Create a new bucket in Cloud Storage""" |
| 37 | + print("Creating new bucket:" + bucket_name) |
| 38 | + bucket_exists = check_if_bucket_exists(bucket_name) |
| 39 | + if bucket_exists: |
| 40 | + print("Bucket {} already exists".format(bucket_name)) |
| 41 | + return storage_client.bucket(bucket_name) |
| 42 | + else: |
| 43 | + bucket = storage_client.bucket(bucket_name) |
| 44 | + bucket.storage_class = "STANDARD" |
| 45 | + new_bucket = storage_client.create_bucket(bucket, location="us") |
| 46 | + print( |
| 47 | + "Created bucket {} in {} with storage class {}".format( |
| 48 | + new_bucket.name, new_bucket.location, new_bucket.storage_class |
| 49 | + ) |
| 50 | + ) |
| 51 | + return new_bucket |
| 52 | + |
| 53 | + |
| 54 | +def check_if_bucket_exists(new_bucket_name): |
| 55 | + """Check if bucket is already exists""" |
| 56 | + bucket_exists = False |
| 57 | + buckets = storage_client.list_buckets() |
| 58 | + for bucket in buckets: |
| 59 | + if bucket.name == new_bucket_name: |
| 60 | + bucket_exists = True |
| 61 | + break |
| 62 | + return bucket_exists |
| 63 | + |
| 64 | + |
| 65 | +def upload_data_to_bucket(bucket: Bucket): |
| 66 | + """Upload data to a GCS bucket""" |
| 67 | + blob = bucket.blob(object_name) |
| 68 | + blob.upload_from_filename(resource_file) |
| 69 | + print("Data from {} has being uploaded to {}".format(resource_file, |
| 70 | + bucket.name)) |
| 71 | + |
| 72 | + |
| 73 | +def get_import_products_gcs_request(): |
| 74 | + """Get import products from gcs request""" |
| 75 | + gcs_bucket = "gs://{}".format(bucket_name) |
| 76 | + gcs_errors_bucket = "{}/error".format(gcs_bucket) |
| 77 | + |
| 78 | + gcs_source = GcsSource() |
| 79 | + gcs_source.input_uris = ["{0}/{1}".format(gcs_bucket, object_name)] |
| 80 | + |
| 81 | + input_config = ProductInputConfig() |
| 82 | + input_config.gcs_source = gcs_source |
| 83 | + |
| 84 | + errors_config = ImportErrorsConfig() |
| 85 | + errors_config.gcs_prefix = gcs_errors_bucket |
| 86 | + |
| 87 | + import_request = ImportProductsRequest() |
| 88 | + import_request.parent = default_catalog |
| 89 | + import_request.reconciliation_mode = ImportProductsRequest.ReconciliationMode.INCREMENTAL |
| 90 | + import_request.input_config = input_config |
| 91 | + import_request.errors_config = errors_config |
| 92 | + |
| 93 | + print("---import products from google cloud source request---") |
| 94 | + print(import_request) |
| 95 | + |
| 96 | + return import_request |
| 97 | + |
| 98 | + |
| 99 | +def import_products_from_gcs(): |
| 100 | + """Call the Retail API to import products""" |
| 101 | + import_gcs_request = get_import_products_gcs_request() |
| 102 | + gcs_operation = ProductServiceClient().import_products( |
| 103 | + import_gcs_request) |
| 104 | + print( |
| 105 | + "Import operation is started: {}".format(gcs_operation.operation.name)) |
| 106 | + |
| 107 | + while not gcs_operation.done(): |
| 108 | + print("Please wait till operation is completed") |
| 109 | + time.sleep(5) |
| 110 | + print("Import products operation is completed") |
| 111 | + |
| 112 | + if gcs_operation.metadata is not None: |
| 113 | + print("Number of successfully imported products") |
| 114 | + print(gcs_operation.metadata.success_count) |
| 115 | + print("Number of failures during the importing") |
| 116 | + print(gcs_operation.metadata.failure_count) |
| 117 | + else: |
| 118 | + print("Operation.metadata is empty") |
| 119 | + |
| 120 | + print( |
| 121 | + "Wait 2 -5 minutes till products become indexed in the catalog,\ |
| 122 | +after that they will be available for search") |
| 123 | + |
| 124 | + |
| 125 | +created_bucket = create_bucket(bucket_name) |
| 126 | +upload_data_to_bucket(created_bucket) |
| 127 | +import_products_from_gcs() |
0 commit comments