外部网络访问示例
本主题提供从用户定义函数和过程访问外部网络位置的示例。
访问 PyPi 以在 Snowpark Container 中安装包¶
You can access the PyPi package repository by creating an external access integration.
You might do this when you want to allow Notebook users on Container Runtime to install pip
packages using the pip install command. With this kind of integration, you can also
allow Snowpark Container Services to install pip packages.
This example uses the Snowflake-managed network rule snowflake.external_access.pypi_rule
described in Privileges and commands.
-
Create an external access integration using the
snowflake.external_access.pypi_rulenetwork rule. -
Create a
developerrole for users who need to usepip installin a Snowpark Container or Notebook on Container Runtime. -
Grant to the
developerrole the privileges needed to use the external access integration you created.
使用 OAuth 访问 Google Translate API¶
以下步骤包括创建外部访问集成以访问 Google Translation API 的代码。这些步骤添加了安全集成和执行语句所需的权限。
-
创建代表外部位置的网络规则。
For more information about the role of a network rule in external access, including privileges required, see Creating a network rule to represent the external network location.
-
Create a security integration to hold the OAuth credentials required to authenticate with the external network location specified by the
google_apis_network_rulenetwork rule.For reference information on the command, including privileges required, see CREATE SECURITY INTEGRATION (External API Authentication).
-
Create a secret to represent the credentials contained by the
google_translate_oauthsecurity integration.For more information about the role of the secret in external access, including privileges required, see Creating a secret to represent credentials.
密钥必须使用 OAUTH_REFRESH_TOKEN 参数指定刷新令牌。要从服务提供商(本例中为 Google Cloud Translation API 服务)处获取刷新令牌,可以使用提供商提供的方式或使用 Snowflake 系统函数。
要使用刷新令牌创建密钥,要么 使用 Google OAuth Playground,要么使用 Snowflake 系统函数,具体方法如下:
- Snowflake 系统函数
-
执行 CREATE SECRET 创建一个密钥。将在后面的步骤中用刷新令牌更新该密钥。
-
Execute the SYSTEM$START_OAUTH_FLOW function to retrieve a URL with which you can obtain a refresh token, specifying as its argument the name of the secret you created previously.
-
该函数生成一个 URL,可以用它来完成 OAuth 意见征求流程。
- In a browser, visit the generated URL and complete the OAuth2 consent process. When you’ve finished, leave the browser open to the last page of the process.
- 从浏览器地址栏中,复制意见征求流程最后一页 URL 中问号后面的所有文本。
- Execute the SYSTEM$FINISH_OAUTH_FLOW function, specifying as an argument the parameters you just copied from the browser address bar to update the secret with a refresh token.
请务必在 SYSTEM$START_OAUTH_FLOW 的同一会话中执行 SYSTEM$FINISH_OAUTH_FLOW。SYSTEM$FINISH_OAUTH_FLOW 使用它从 OAuth 服务器获取的访问令牌和刷新令牌,更新您在 SYSTEM$START_OAUTH_FLOW 中指定的密钥。
- Google OAuth Playground
-
In Google OAuth Playground (https://developers.google.com/oauthplayground/), select and authorize the Cloud Translation API as specified in step 1.
-
In Step 2, click exchange authorization code for tokens, then copy the refresh token token value.
-
执行 CREATE SECRET 创建一个密钥,指定复制的刷新令牌值。
For more information about the role of a secret in external access, including privileges required, see Creating a secret to represent credentials.
-
-
使用网络规则和密钥创建外部访问集成。
For more information about the role of an external access integration, including privileges required, see Creating an external access integration.
-
Create a
developerrole that will be assigned to users who need to create a UDF or procedure that uses the integration. -
Grant to the
developerrole privileges needed to create a UDF that uses the objects for external access. This includes the following:- 对密钥的 READ 权限。
- 对包含密钥的架构的 USAGE 权限。
- 对集成的 USAGE 权限。
-
Create a UDF
google_translate_pythonthat translates the specified text into a phrase in the specified language. For more information, see Using the external access integration in a function or procedure. -
Grant the USAGE privilege on the
google_translate_pythonfunction so that those with theuserrole can call it. -
Execute the
google_translate_pythonfunction to translate a phrase.
这会生成以下输出。
使用基本身份验证访问外部 Lambda 函数¶
以下步骤包括创建外部访问集成的示例代码,用于访问 Snowflake 外部的 lambda 函数。示例中使用的外部端点本身就是一个占位符,但它也可以是可在 REST 服务端点使用的一个函数。
The external access is used in a vectorized Python UDF that receives a Pandas DataFrame containing the data.
-
Create a network rule
lambda_network_rulerepresenting the external locationmy_external_service(here, a placeholder value for the location of an external endpoint).For more information about the role of a network rule in external access, see Creating a network rule to represent the external network location.
-
创建一个密钥,代表外部服务所需的凭据。
本示例稍后的处理程序代码将使用用于 Python 的 Snowflake API 从密钥中检索凭据。
For more information about the role of the secret in external access, see Creating a secret to represent credentials.
- Create a
developerrole and grant to it READ privileges on the secret. This role will be assigned to users who need to create a UDF or procedure that uses the secret.
此外,创建用户将用来调用该函数的角色。
-
Grant to the
developerrole privileges needed to create a UDF that uses the objects for external access. This includes the following:- 对密钥的 READ 权限。
- 对包含密钥的架构的 USAGE 权限。
-
创建外部访问集成,通过创建的网络规则和密钥指定外部端点和凭据。
For more information about the role of an external access integration, including privileges required, see Creating an external access integration.
-
Create a vectorized Python UDF
return_double_columnthat accesses an external network location to process data received as a Pandas DataFrame (https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html).For more information on using external access in a UDF, see Using the external access integration in a function or procedure.
-
Grant the USAGE privilege on the
return_double_columnfunction so that those with theuserrole can call it. -
Execute the
return_double_columnfunction, making a request to the external endpoint.Code in the following example creates a two-column table and inserts 100,000,000 rows containing 4-byte integers. The code then executes the
return_double_columnfunction, passing values from columnafor processing by the external endpoint.
使用 AWS IAM 访问 Amazon S3¶
以下步骤包括示例代码,以使用 IAM 连接到 AWS S3 桶。
For more information about AWS IAM, see AWS IAM documentation (https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_identifiers.html).
-
Create a network rule,
aws_s3_network_rule, that represents the AWS S3 bucket at the location specified by the VALUE_LIST parameter.For more information about the role of a network rule in external access, see Creating a network rule to represent the external network location.
-
Create a security integration to hold the AWS IAM Amazon Resource Name (ARN) credentials required to authenticate with the external network location specified by the
aws_s3_network_rulenetwork rule.For reference information on the command, including privileges required, see CREATE SECURITY INTEGRATION (AWS IAM Authentication).
-
获取 IAM USER 的 ARN 和 ID。
-
在您创建的安全集成上执行 DESC 命令。
-
从显示的输出中,复制以下属性的值以在下一步中使用:
- API_AWS_IAM_USER_ARN
- API_AWS_EXTERNAL_ID
-
-
授予必要的 IAM 用户权限以访问桶。
Use the ARN and ID values when configuring a trust policy as described in Step 5 of Option 1: Configure a Snowflake storage integration to access Amazon S3.
-
Create a secret of type CLOUD_PROVIDER_TOKEN to represent credentials required by the external service.
Handler code later in this example retrieves the credentials from the secret using a Snowflake API.
For more information about the role of the secret in external access, see Creating a secret to represent credentials.
-
Create a
developerrole and grant to it READ privileges on the secret. This role will be assigned to users who need to create a UDF or procedure that uses the secret.
此外,创建用户将用来调用该函数的角色。
-
Grant to the
developerrole the privileges needed to create a UDF that uses the objects for external access. This includes the following:- 对密钥的 READ 权限。
- 对包含密钥的架构的 USAGE 权限。
-
Create an external access integration to specify the external endpoint and credentials through the network rule and secret you created.
For more information about the role of an external access integration, including privileges required, see Creating an external access integration.
-
创建 UDF,使用外部访问集成来连接您创建的网络规则中指定的 Amazon S3 桶。
The handler code uses Snowflake APIs to retrieve a token from the secret you created. From this token, you can use Snowflake APIs to retrieve values needed to create a session for connecting with Amazon S3, including an access key ID, secret access key, and session token.
For more information on using external access in a UDF, see Using the external access integration in a function or procedure.
-
Grant the USAGE privilege on the UDF so that those with the
userrole can call it.
- 执行函数以连接到外部端点。