This example uses a custom Google Apps Script that you create and maintain.
Any issues with file detection, timing, or script execution could be related to your custom script,
rather than with Unstructured. If you are getting unexpected or no results, be sure to check your custom
script’s execution logs first for any informational and error messages.
Requirements
To use this example, you will need the following:-
An Unstructured account, and an Unstructured API key for your account, as follows:
-
If you do not already have an Unstructured account, sign up for free.
After you sign up, you are automatically signed in to your new Unstructured Starter account, at https://platform.unstructured.io.
To sign up for a Team or Enterprise account instead, contact Unstructured Sales, or learn more.
-
If you have an Unstructured Starter or Team account and are not already signed in, sign in to your account at https://platform.unstructured.io.
For an Enterprise account, see your Unstructured account administrator for instructions, or email Unstructured Support at support@unstructured.io.
-
Get your Unstructured API key:
a. After you sign in to your Unstructured Starter account, click API Keys on the sidebar.
b. Click Generate API Key.For a Team or Enterprise account, before you click API Keys, make sure you have selected the organizational workspace you want to create an API key for. Each API key works with one and only one organizational workspace. Learn more.
c. Follow the on-screen instructions to finish generating the key.
d. Click the Copy icon next to your new key to add the key to your system’s clipboard. If you lose this key, simply return and click the Copy icon again.
-
If you do not already have an Unstructured account, sign up for free.
After you sign up, you are automatically signed in to your new Unstructured Starter account, at https://platform.unstructured.io.
-
The Unstructured Workflow Endpoint URL for your account, as follows:
- In the Unstructured UI, click API Keys on the sidebar.
- Note the value of the Unstructured Workflow Endpoint field.
- In the Unstructured UI, click API Keys on the sidebar.
- A Google Cloud Storage source connector in your Unstructured account. Learn how.
- Some available destination connector in your Unstructured account.
- A workflow that uses the preceding source and destination connectors. Learn how.
-
An OAuth 2.0 client ID and client secret to call the Google API, as follows:
- Sign in to your Google Cloud account.
- Go to the Google Cloud APIs dashboard.
- Click + Enable APIs and services.
- In the Search for APIs & Services box, enter
Cloud Storage API
. - In the list of search results, click Cloud Storage API.
- Make sure that API Enabled is shown. If not, click Enable.
- Go to your Google Cloud console welcome page.
- In the Search (/) for resources, docs, products, and more box, enter
Credentials
. - Click Credentials (APIs & Services).
- Click + Create credentials > OAuth client ID.
- For Application type, select Web application.
- (Optional) Enter some non-default Name for this OAuth 2.0 client to be shown in the list of created clients in your Google Cloud Console.
- Click Create.
- After the OAuth client is created, click Download JSON to save the client ID and client secret values to a JSON file on your local machine. Store this JSON file in a secure location.
Step 1: Create the Google Apps Script project
- Go to http://script.google.com/.
- Click + New project.
- Click the new project’s default name (such as Untitled project), and change it to something more descriptive, such as Unstructured Scripts for GCS.
Step 2: Add the script
- With the project still open, on the sidebar, click the < > (Editor) icon.
- In the Files tab, click Code.gs.
-
Replace the contents of the
Code.gs
file with the following code instead: - Click the Save project to Drive button.
Step 3: Customize the script for your workflow
- With the project still open, on the Files tab, click the Add a file button, and then click Script.
-
Name the new file
Constants
. The.gs
extension is added automatically. -
Replace the contents of the
Constants.gs
file with the following code instead:Replace the following placeholders:- Replace
<bucket-path>
with the path to your Google Cloud Storage bucket. This is the same path that you specified when you created your Google Cloud Storage source connector in your Unstructured account. Do not include thegs://
prefix here. - Replace
<unstructured-api-url>
with your Unstructured API URL value. - Replace
<workflow-id>
with the ID of your Unstructured workflow. - Replace
<unstructured-api-key>
with your Unstructured API key value. - Replace
<client-id>
with your OAuth 2.0 client ID value. - Replace
<client-secret>
with your OAuth 2.0 client secret value.
- Replace
- Click the disk (Save project to Drive) icon.
Step 4: Generate an initial OAuth 2.0 access token
- On the sidebar, click the gear (Project Settings) icon.
- In the IDs area, next to Script ID, click Copy to copy the script’s ID value to your system’s clipboard.
- In a separate tab in your web browser, open your Google Cloud Console welcome page.
- In the Search (/) for resources, docs, products, and more box, enter
Credentials
. - Click Credentials (APIs & Services).
- In the OAuth 2.0 client IDs list, click the link for the client ID that you created earlier in the requirements.
- Under Authorized redirect URIs, click Add URI.
- In the URIs 1 box, enter
https://script.google.com/macros/d/<script-id>/usercallback
, replacing<script-id>
with the script’s ID value that you copied earlier. - Click Save.
- On the original tab in your web browser, with the Google Apps Script project still open to the Constants.gs file, on the sidebar, next to Libraries, click the + (Add a library) icon.
- For Script ID, enter
1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF
, and then click Look up. - For Version, make sure the largest number is selected.
- Click Add.
- In the sidebar, click the Code.gs file to open it.
- In the file’s top navigation bar, select getAccessToken.
- Click the Run icon.
- In the Execution log area, next to the message
Open the following URL and re-run the script
, copy the entire URL into a separate tab in your web browser and then browse to that URL. - When prompted, click Review permissions, and follow the on-screen instructions to grant the necessary permissions.
Step 5: Create the script trigger
- On the original tab in your web browser, with the Google Apps Script project still open, on the sidebar, click the alarm clock (Triggers) icon.
- Click the + Add Trigger button.
-
Set the following values:
-
For Choose which function to run, select
checkForNewOrUpdatedGCSFiles
. - For Choose which deployment should run, select Head.
- For Select event source, select Time-driven.
- For Select type of time based trigger, select Minutes timer.
-
For Select minute interval, select Every 5 minutes.
If you change Minutes timer or Every 5 minutes to a different interval, you should also go back and change the number
5
in the following line of code in thecheckForNewOrUpdatedFiles
function. Change the number5
to the number of minutes that correspond to the alternate interval you selected: - For Failure notification settings, select an interval such as immediately, hourly, or daily.
-
For Choose which function to run, select
- Click Save.
Step 6: View trigger results
- With the Google Apps Script project still open, on the sidebar, click the three lines (Executions) icon.
- As soon as the first script execution completes, you should see a corresponding message appear in the Executions list. If the Status column shows Completed, then keep going with this procedure. If the Status column shows Failed, expand the message to get any details about the failure. Fix the failure, and then wait for the next script execution to complete.
- When the Status column shows Completed then, in your Unstructured account’s user interface, click Jobs on the sidebar to see if a new job is running for that worklow. If no new job is running for that workflow, then add at least one new file to—or update at least one existing file within—the Google Cloud Storage bucket, within 5 minutes of the next script execution. After the next script execution, check the Jobs list again.
Step 7 (Optional): Delete the trigger
- To stop the script from automatically executing on a regular basis, with the Google Apps Script project still open, on the sidebar, click the alarm clock (Triggers) icon.
- Rest your mouse pointer on the trigger you created in Step 5.
- Click the ellipsis (three dots) icon, and then click Delete trigger.