Amazon WorkDocs
Developer Guide

Connect to Amazon WorkDocs Using IAM User Credentials and Query for Users

The following simple code, using the AWS SDK, illustrates the steps in making API calls using IAM user’s API credentials. In this case API user and Amazon WorkDocs site belong to same AWS Account.

Ensure that the IAM user has been granted Amazon WorkDocs API access through an appropriate IAM policy.

The code sample uses describeUsers API to search for users and obtain metadata for users. User metadata provides details such as first name, last name, User ID and Root Folder ID. Root Folder ID is particularly helpful if you want to perform any content upload or download operations on behalf of the user.

The code requires that you obtain an Amazon WorkDocs Organization ID.

You can obtain a Amazon WorkDocs Organization ID from the AWS console using the following steps:

Obtaining an Organization Id

  1. In the AWS Directory Service console navigation pane, select Directories.

  2. The Directory ID corresponding to your Amazon WorkDocs site is the Organization ID for that site.

The following is the code example:

import java.util.ArrayList; import java.util.List; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.regions.Regions; import; import; import; import; import; public class GetUserDemo { public static void main(String[] args) throws Exception { AWSCredentials longTermCredentials = new BasicAWSCredentials("accessKey", "secretKey"); AWSStaticCredentialsProvider staticCredentialProvider = new AWSStaticCredentialsProvider(longTermCredentials); AmazonWorkDocs workDocs = AmazonWorkDocsClient.builder().withCredentials(staticCredentialProvider) .withRegion(Regions.US_WEST_2).build(); List<User> wdUsers = new ArrayList<>(); DescribeUsersRequest request = new DescribeUsersRequest(); // The OrganizationId used here is an example and it should be replaced // with the OrganizationId of your WorkDocs site. request.setOrganizationId("d-123456789c"); request.setQuery("joe"); String marker = null; do { request.setMarker(marker); DescribeUsersResult result = workDocs.describeUsers(request); wdUsers.addAll(result.getUsers()); marker = result.getMarker(); } while (marker != null); System.out.println("List of users matching the query string: joe "); for (User wdUser : wdUsers) { System.out.printf("Firstname:%s | Lastname:%s | Email:%s | root-folder-id:%s\n", wdUser.getGivenName(), wdUser.getSurname(), wdUser.getEmailAddress(), wdUser.getRootFolderId()); } } }