问题描述
我认为我了解获取AWS Quicksight仪表板的嵌入URL所需的所有步骤,并且已经能够通过。
但是,我似乎无法通过AWS Java SDK(特别是v2)使它正常工作。似乎没有一个很好的例子。
有人知道使用Java(或Groovy)最好使用Spring Boot(或Grails 3或4)的良好工作示例吗?
我终于明白了。这是一个有效的使用AWS Java SDK v2的Quicksight仪表板URL。
摘要
AWS文档中。文档说让您的服务帐户承担用户的IAM角色,然后使用 IdentityType.IAM
要求仪表板作为最终用户。但是,我提出了获取该URL作为服务帐户的请求,但没有承担用户的角色,并在API调用中指定了最终用户的ARN和 IdentityType.QUICKSIGHT
。 / p>
代码()
进口软件.amazon.awssdk.services.quicksight.QuickSightClient
进口软件.amazon.awssdk.services.quicksight.model。*
类ApplicationController {
字符串roleName = embed-dashboard
字符串命名空间= default
字符串awsAccountId
字符串dashboardId
QuickSightClient quickSightClient
def索引(字符串awsAccountId ,字符串电子邮件,字符串dashboardId){
//已删除不重要的代码
字符串url = fetchEmbedUrl(email)
呈现< a href ='$ url'target ='_ blank'>仪表板< / a>
}
private String fetchEmbedUrl(String email){
//通过电子邮件地址查找用户
//如果我们找不到用户注册他们
用户user = fetchUser(email)?:registerUser(email)
//获取仪表板URL
字符串embedUrl = quickSightClient.getDashboardEmbedUrl(GetDashboardEmbedUrlRequest.builder()
.awsAccountId(awsAccountId)
.dashboardId(dashboardId)
.userArn(user.arn)
.identityType(IdentityType.QUICKSIGHT)
.sessionLifetimeInMinutes(600L)
.build()
).embedUrl
log.info( URL:\n $ embedUrl)
return embedUrl
}
私人用户fetchUser(字符串电子邮件){
return quickSightClient.listUsers(ListUsersRequest.builder()
.awsAccountId(awsAccountId)
.namespace(namespace)
.buil d()
).userList()。find {it.email()== email}
}
private User registerUser(String email){
String roleArn = arn:aws:iam :: $ awsAccountId:role / $ roleName
返回quickSightClient.registerUser(RegisterUserRequest.builder()
.awsAccountId(awsAccountId)
.namespace(namespace)
.identityType(IdentityType.IAM)
.iamArn(roleArn)
.userRole( READER)
.email(电子邮件)
.sessionName(email)
.build()
).user()
}
}
I think I understand all the steps necessary to get the embed URL for an AWS Quicksight dashboard, and I have been able to get a valid URL via the AWS CLI as described in the AWS docs.
However, I can't seem to get it to work via the AWS Java SDK (specifically v2). There doesn't seem to be a good example anywhere.
Does anyone know of a good working example using Java (or Groovy) preferably using Spring Boot (or Grails 3 or 4)?
I finally figured it out. Here's a working Groovy/Grails sample project that gets Quicksight dashboard URLs using the AWS Java SDK v2.
Summary
I followed a modified version of the process outline in the AWS docs, here. The docs say to have your service account assume the user's IAM role and then request the dashboard as the end user using IdentityType.IAM
. However, I made the request to get the url as the service account, without assuming the user's role, and specified the end user's ARN and IdentityType.QUICKSIGHT
in the API call.
Code (full code)
import software.amazon.awssdk.services.quicksight.QuickSightClient
import software.amazon.awssdk.services.quicksight.model.*
class ApplicationController {
String roleName = "embed-dashboard"
String namespace = "default"
String awsAccountId
String dashboardId
QuickSightClient quickSightClient
def index(String awsAccountId, String email, String dashboardId) {
//unimportant code removed
String url = fetchEmbedUrl(email)
render "<a href='$url' target='_blank'>Dashboard</a>"
}
private String fetchEmbedUrl(String email) {
// look up the user by email address
// if we don't find the user register them
User user = fetchUser(email) ?: registerUser(email)
// get the dashboard URL
String embedUrl = quickSightClient.getDashboardEmbedUrl(GetDashboardEmbedUrlRequest.builder()
.awsAccountId(awsAccountId)
.dashboardId(dashboardId)
.userArn(user.arn)
.identityType(IdentityType.QUICKSIGHT)
.sessionLifetimeInMinutes(600L)
.build()
).embedUrl
log.info("URL:\n$embedUrl")
return embedUrl
}
private User fetchUser(String email) {
return quickSightClient.listUsers(ListUsersRequest.builder()
.awsAccountId(awsAccountId)
.namespace(namespace)
.build()
).userList().find { it.email() == email }
}
private User registerUser(String email) {
String roleArn = "arn:aws:iam::$awsAccountId:role/$roleName"
return quickSightClient.registerUser(RegisterUserRequest.builder()
.awsAccountId(awsAccountId)
.namespace(namespace)
.identityType(IdentityType.IAM)
.iamArn(roleArn)
.userRole("READER")
.email(email)
.sessionName(email)
.build()
).user()
}
}
这篇关于如何使用Java SDK获取AWS Quicksight仪表板的嵌入URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!