您位於此處:
為外部儲存的檔案產生批次對應檔案
透過產生將 Salesforce 檔案與其 Amazon S3 位置相關聯的對應檔案,為 Amazon Bedrock 或 Amazon Kendra 準備現有檔案。只有在您想要搜尋的 Amazon S3 中有現有檔案時,才必須完成此工作。
必要版本
| 所需的使用者權限 | |
|---|---|
| 若要啟用「外部儲存空間搜尋」: | 自訂應用程式 |
| 檢視權限集: | 「檢視設定和組態」權限 |
| 指派權限集: | 指派權限集權限 |
| 若要建立本機外部用戶端應用程式: | 建立、編輯和刪除外部用戶端應用程式 |
開始之前,請確認您有:
- 具有 ContentHubAWSS3 類型 Amazon S3 外部資料來源的 Salesforce 組織。請參閱 設定語意搜尋。
- 具有權限可建立 Lambda 函數、IAM 角色和 S3 分類的 AWS 帳戶。
- 儲存對應檔案的 Amazon S3 分類。
建立 Salesforce 整合使用者
- 進入「Salesforce 設定」,建立整合使用者。
-
使用這些值設定使用者。
欄位 值 使用者授權 Salesforce 整合 設定檔 最低存取權 - 僅 API 整合 - 將 Salesforce API 整合權限集授權指派給使用者。
-
建立授與這些權限的權限集:
- 已啟用 API
- 查詢所有檔案
- 將權限集指派給整合使用者。
建立外部用戶端應用程式
建立使用 OAuth 2.0 用戶端認證流程的本機外部用戶端應用程式。在 OAuth 設定中,新增「透過 API 管理使用者資料 (api)」範圍,並將 Salesforce 整合使用者設定為執行使用者。
如需詳細指示,請參閱 建立外部用戶端應用程式和 設定用戶端認證流程。
建立對應檔案
部署 AWS Lambda 函數,此函數會查詢 Salesforce 外部儲存的檔案,並在每個 Salesforce ContentDocument 識別碼與其 Amazon S3 位置之間產生對應。
透過環境變數設定函數。附加具有授與權限的 IAM 執行角色,其 s3:PutObject 會將對應檔案寫入指定的 S3 分類,並將記錄傳送至 Amazon CloudWatch。
如需指示,請參閱 AWS 文件中的 部署 Python Lambda 函數、使用 Lambda 環境變數、Lambda 執行角色。
以下是參照 Lambda 代碼:
# 1. Authenticate to Salesforce (OAuth 2.0 Client Credentials)
token = sf_oauth_token(SF_INSTANCE_URL, SF_CLIENT_ID, SF_CLIENT_SECRET)
# 2. Query ContentVersion -> ContentDocumentId, ExternalDocumentInfo2, ExternalDataSource
soql = ("SELECT ContentDocumentId, ExternalDocumentInfo2, "
"ExternalDataSource.NamedCredentialId, ExternalDataSource.MasterLabel "
"FROM ContentVersion WHERE IsLatest=true AND ExternalDocumentInfo2!=null "
"AND ExternalDataSource.Type='ContentHubAWSS3'")
records = sf_query_all(token, soql)
# 3. Resolve each Named Credential's S3 endpoint URL (Tooling API)
nc_url = {nc_id: sf_named_credential_url(token, nc_id)
for nc_id in {r["ExternalDataSource"]["NamedCredentialId"] for r in records}}
# 4. Build the mapping keyed by the external document id
mapping = defaultdict(list)
for r in records:
nc_id = r["ExternalDataSource"]["NamedCredentialId"]
mapping[r["ExternalDocumentInfo2"]].append({
"ContentDocumentId": r["ContentDocumentId"],
"DataSourceLabel": r["ExternalDataSource"]["MasterLabel"],
"NamedCredentialId": nc_id,
"S3_Endpoint_URL": nc_url.get(nc_id),
})
# 5. Upload the mapping JSON to S3
boto3.client("s3").put_object(
Bucket=S3_BUCKET_NAME, Key=S3_FILE_NAME,
Body=json.dumps(mapping, indent=4), ContentType="application/json")此文章是否解決您的問題?
請讓我們知道,以便我們改進!
