How to Get Embed URLs for AWS Quicksight Dashboards using Java SDK

佐手、 提交于 2020-01-22 02:50:27

问题


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)?


回答1:


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()
    }

}


来源:https://stackoverflow.com/questions/59619725/how-to-get-embed-urls-for-aws-quicksight-dashboards-using-java-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!