This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
chore: update README and setup scripts #193
Merged
+101
−50
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6d0a16f
update README
tetiana-karasova 793a3c3
fix: sleep is added to the setup script
tetiana-karasova 78ecaac
Merge branch 'main' into main
parthea 9066332
Merge branch 'main' into main
parthea 872ac0d
Merge branch 'main' into main
parthea ae1a3a6
fix: sleep is added to the setup script
tetiana-karasova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,44 +43,69 @@ If, for some reason, you have decided to proceed with these code samples without | |
### Select your project and enable the Retail API | ||
|
||
Google Cloud organizes resources into projects. This lets you | ||
collect all the related resources for a single application in one place. | ||
collect all related resources for a single application in one place. | ||
|
||
If you don't have a Google Cloud project yet or you're not the owner of an existing one, you can | ||
[create a new project](https://console.cloud.google.com/projectcreate). | ||
|
||
After the project is created, set your PROJECT_ID to a ```project``` variable. | ||
1. Run the following command in Terminal: | ||
After the project is created, set your PROJECT_ID to a ```project``` variable: | ||
|
||
1. Run the following command in the Terminal: | ||
|
||
```bash | ||
gcloud config set project <YOUR_PROJECT_ID> | ||
``` | ||
|
||
1. To check that the Retail API is enabled for your Project, go to the [Admin Console](https://console.cloud.google.com/ai/retail/). | ||
1. Ensure that the Retail API is enabled for your project in the [API & Services page](https://console.cloud.google.com/apis/api/retail.googleapis.com/). | ||
|
||
1. Log in with your user credentials to run a code sample from the Cloud Shell: | ||
|
||
```bash | ||
gcloud auth login | ||
``` | ||
|
||
1. Type `Y` and press **Enter**. Click the link in the Terminal. A browser window | ||
should appear asking you to log in using your Gmail account. | ||
|
||
1. Provide the Google Auth Library with access to your credentials and paste | ||
the code from the browser to the Terminal. | ||
|
||
## Prepare your work environment | ||
|
||
To prepare the work environment you should perform the following steps: | ||
- Create a service account. | ||
- Create service account key and set it to authorize your calls to the Retail API. | ||
- Install Google Cloud Retail library. | ||
|
||
### Create service account | ||
|
||
To access the Retail API, you must create a service account. | ||
To access the Retail API, you must create a service account. Check that you are an owner of your Google Cloud project on the [IAM page](https://console.cloud.google.com/iam-admin/iam). | ||
|
||
1. To create a service account, follow this [instruction](https://cloud.google.com/retail/docs/setting-up#service-account) | ||
1. To create a service account, perform the following command: | ||
|
||
1. Find your service account on the [IAM page](https://console.cloud.google.com/iam-admin/iam), | ||
click `Edit` icon, add the 'Storage Admin' and 'BigQuery Admin' roles. It may take some time for changes to apply. | ||
```bash | ||
gcloud iam service-accounts create <YOUR_SERVICE_ACCOUNT_ID> | ||
``` | ||
|
||
1. Copy the service account email in the Principal field. | ||
1. Assign the needed roles to your service account: | ||
|
||
### Set up authentication | ||
```bash | ||
for role in {retail.admin,storage.admin,bigquery.admin} | ||
do gcloud projects add-iam-policy-binding <YOUR_PROJECT_ID> --member="serviceAccount:<YOUR_SERVICE_ACCOUNT_ID>@<YOUR_PROJECT_ID>.iam.gserviceaccount.com" --role="roles/${role}" | ||
done | ||
``` | ||
|
||
To run a code sample from the Cloud Shell, you need to be authenticated using the service account credentials. | ||
1. Use the following command to print out the service account email: | ||
|
||
1. Login with your user credentials. | ||
```bash | ||
gcloud auth login | ||
gcloud iam service-accounts list|grep <YOUR_SERVICE_ACCOUNT_ID> | ||
``` | ||
|
||
1. Type `Y` and press **Enter**. Click the link in a Terminal. A browser window should appear asking you to log in using your Gmail account. | ||
Copy the service account email. | ||
|
||
1. Provide the Google Auth Library with access to your credentials and paste the code from the browser to the Terminal. | ||
|
||
1. Upload your service account key JSON file and use it to activate the service account: | ||
1. Upload your service account key JSON file and use it to activate the service | ||
account: | ||
|
||
```bash | ||
gcloud iam service-accounts keys create ~/key.json --iam-account <YOUR_SERVICE_ACCOUNT_EMAIL> | ||
|
@@ -90,7 +115,9 @@ To run a code sample from the Cloud Shell, you need to be authenticated using th | |
gcloud auth activate-service-account --key-file ~/key.json | ||
``` | ||
|
||
1. To request the Retail API, set your service account key JSON file as the GOOGLE_APPLICATION_CREDENTIALS environment variable : | ||
1. Set the key as the GOOGLE_APPLICATION_CREDENTIALS environment variable to | ||
use it for sending requests to the Retail API. | ||
|
||
```bash | ||
export GOOGLE_APPLICATION_CREDENTIALS=~/key.json | ||
``` | ||
|
@@ -100,12 +127,14 @@ To run a code sample from the Cloud Shell, you need to be authenticated using th | |
To run Python code samples for the Retail API tutorial, you need to set up your virtual environment. | ||
|
||
1. Run the following commands in a Terminal to create an isolated Python environment: | ||
|
||
```bash | ||
pip install virtualenv | ||
virtualenv myenv | ||
virtualenv -p python3 myenv | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Virtualenv is installed by default on python3: https://docs.python.org/3/library/venv.html#creating-virtual-environments
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to is because the CloudShell by default still has the python 2.7 |
||
source myenv/bin/activate | ||
``` | ||
|
||
1. Next, install Google packages: | ||
|
||
```bash | ||
pip install google | ||
pip install google-cloud-retail | ||
|
@@ -114,41 +143,53 @@ To run Python code samples for the Retail API tutorial, you need to set up your | |
|
||
``` | ||
|
||
## Import Catalog Data | ||
## Import catalog data | ||
|
||
This step is required if this is the first Retail API Tutorial you run. | ||
Otherwise, you can skip it. | ||
There is a python-retail/samples/interactive-tutorials/resources/products.json file with valid products prepared in the `resources` directory. | ||
|
||
The other file, python-retail/samples/interactive-tutorials/resources/products_some_invalid.json, contains both valid and invalid products. You will use it to check the error handling. | ||
|
||
### Upload catalog data to Cloud Storage | ||
|
||
There is a JSON file with valid products prepared in the `product` directory: | ||
`product/resources/products.json`. | ||
In your own project you need to create a Cloud Storage bucket and put the JSON file there. | ||
The bucket name must be unique. For convenience, you can name it `<YOUR_PROJECT_ID>_<TIMESTAMP>`. | ||
|
||
Another file, `product/resources/products_some_invalid.json`, contains both valid and invalid products, and you will use it to check the error handling. | ||
1. The code samples for each of the Retail services are stored in different directories. | ||
|
||
In your own project, create a Cloud Storage bucket and put the JSON file there. | ||
The bucket name must be unique. For convenience, you can name it `<YOUR_PROJECT_ID>_<TIMESTAMP>`. | ||
Go to the code samples directory, your starting point to run more commands. | ||
|
||
1. To create the bucket and upload the JSON file, run the following command in the Terminal: | ||
```bash | ||
cd python-retail/samples/interactive-tutorials | ||
``` | ||
|
||
1. To create the bucket and upload the JSON file, open python-retail/samples/interactive-tutorials/product/setup_product/products_create_gcs_bucket.py file | ||
|
||
1. Go to the **product** directory and run the following command in the Terminal: | ||
|
||
```bash | ||
python product/setup_product/create_gcs_bucket.py | ||
python setup_product/products_create_gcs_bucket.py | ||
``` | ||
|
||
Now you can see the bucket is created in the [Cloud Storage](https://console.cloud.google.com/storage/browser), and the files are uploaded. | ||
|
||
1. The name of the created Retail Search bucket is printed in the Terminal. Copy the name and set it as the environment variable `BUCKET_NAME`: | ||
1. The name of the created Cloud Storage bucket is printed in the Terminal. | ||
|
||
``` | ||
The gcs bucket <YOUR_PROJECT_ID>_<TIMESTAMP> was created | ||
``` | ||
|
||
Copy the name and set it as the environment variable `BUCKET_NAME`: | ||
|
||
```bash | ||
export BUCKET_NAME=<YOUR_BUCKET_NAME> | ||
``` | ||
|
||
### Import products to the Retail Catalog | ||
|
||
To import the prepared products to a catalog, run the following command in the Terminal: | ||
To import the prepared products to a catalog, open python-retail/samples/interactive-tutorials/product/import_products_gcs.py file and run the following command in the Terminal: | ||
|
||
```bash | ||
python product/import_products_gcs.py | ||
python import_products_gcs.py | ||
``` | ||
|
||
## Run your code sample | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be
google auth application-default login
?https://cloud.google.com/sdk/gcloud/reference/auth/application-default/login
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no - if we use default authentication we have permission denied error creating the big query tables