Is it possible to get the canonical user id from AWS IAM users, from the .NET API?

Question:

I have successfully created a user, credentials, and a bucket.
Now I need to grant bucket access to this user.

Is there any way to get this CanonicalUser value from code?
The IAM user object only provides ARN, Path, UserId and UserName values, but none of these are valid for the grant.

Answer:

You can easily get CanonicalUser ID using ListAllMyBuckets API call [1] (s3:ListAllMyBuckets permission is required):

In AWS SDK for .NET, use code like this [2]:

In AWSJavaSDK you can use AmazonS3.getS3AccountOwner wrapper method [3].

  1. https://docs.aws.amazon.com/AmazonS3/latest/API/RESTServiceGET.html
  2. https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/MS3ListBuckets.html
  3. https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3Client.html#getS3AccountOwner–

Leave a Reply