本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
從後端服務SDK撥打電話
建立代表後端服務的使用者後,您可以建立頻道、傳送訊息至該頻道,以及讀取來自該頻道的訊息。
執行下列CLI命令以建立公有頻道。
aws chime-sdk-messaging create-channel \ --chime-bearer "
app_instance_user_arn
" \ --app-instance-arn "app_instance_arn
" \ --name "firstChannel"
此命令會產生此格式ARN的 : arn:aws:chime:
region
:aws_account_id
:app-instance/app_instance_id
/channel/channel_id
.
IAM 授權對後端服務的運作方式
在上一節的 CLI 命令中,記下 chime-bearer
參數。它識別建立或與管道和訊息等資源互動的使用者。幾乎所有 Amazon Chime SDK 訊息都會以 參數chime-bearer
的形式APIs進行,但APIs僅開發人員需要呼叫 。 CreateAppInstance
Amazon Chime SDK 訊息的IAM許可APIs需要app-instance-user-arn
符合 chime-bearer
參數的 。其他 ARNs— 通常是頻道 ARNs— 可能需要根據 API。對於如上述範例的後端服務,這會導致如下列範例IAM的政策:
{ "Version": "2012-10-17", "Statement": { "Effect": "Allow", "Action": [ "chime:SendChannelMessage", "chime:ListChannelMessages", "chime:CreateChannelMembership", "chime:ListChannelMemberships", "chime:DeleteChannelMembership", "chime:CreateChannel", "chime:ListChannels", "chime:DeleteChannel", ... ], "Resource": [ "arn:aws:chime:
region
:aws_account_id
:app-instance/app_instance_id
/user/back-end-worker", "arn:aws:chime:region
:aws_account_id
:app-instance/app_instance_id
/channel/*" ] }
請注意 Resource
區段ARN中的 AppInstanceUser
ARN和 頻道。此IAM政策範例授予後端服務許可,以 ID 為 "" 的使用者身分API撥打電話back-end-worker。如果您希望後端服務能夠為使用您應用程式的人員撥打電話,請將 app_instance_user_arn
變更為 arn:aws:chime:
。region
:aws_account_id
:app-instance/app_instance_id
/user/*
了解隱含API授權
除了IAM政策之外,Amazon Chime SDK 訊息還APIs具有隱含許可。例如, AppInstanceUser
只能在使用者所屬的頻道中傳送訊息或列出頻道成員資格。其中一個例外是 已提升為 AppInstanceUser
的 AppInstanceAdmin
。根據預設,管理員有權存取您應用程式中的所有頻道。對於大多數使用案例,對於包含重要業務邏輯的後端服務,您只需要此項目。
下列CLI命令會將後端使用者提升為管理員。
aws chime-sdk-identity create-app-instance-admin \ --app-instance-admin-arn "
app_instance_user_arn
" \ --app-instance-arn "app_instance_arn
"
傳送和列出頻道訊息
下列CLI命令會傳送頻道訊息。
aws chime-sdk-messaging send-channel-message \ --chime-bearer "
app_instance_user_arn
" \ --channel-arn "channel_arn
" \ --content "hello world" \ --type STANDARD \ --persistence PERSISTENT
下列CLI命令會依反向時間順序列出頻道訊息。
aws chime list-channel-messages
aws chime-sdk-messaging list-channel-messages
aws chime-sdk-messaging list-channel-messages \ --chime-bearer "
app_instance_user_arn
" \ --channel-arn "channel_arn
"