Friends,
I have a Dictionary<int, string>
which contains a list of DepartmentIds and its s3 folder names. For e.g., { {key=1, value="dept-1"}, {key=2, value="dept-2"} }
etc.
For each of those s3 folders, I would like to create a user user-dept-group-{DepartmentId}
mapped logically to its SFTP server folder. For e.g., user-dept-group-1
will have a logical folder dept-1
mapped to my-s3-bucket/dept-1
in the aws sftp server. Below is the code I was using and I am always getting Cannot set HomeDirectory for LOGICAL HomeDirectoryType
error.
If I comment the HomeDirectoryMappings
and set the HomeDirectoryType
to HomeDirectoryType.PATH
, then I see the user getting created.
public async Task<List<CreateUserResponse>> SetupSftpUser(Dictionary<int, string> masterDeptFolders)
{
var result = new List<CreateUserResponse>();
var awsTransferClient = new AmazonTransferClient(AwsAccessKey, AwsSecretKey, RegionEndpoint.USEast1);
foreach (var (key, folder) in masterDeptFolders)
{
var createUserRequest = new CreateUserRequest
{
HomeDirectory = $"/{ParentBucketName}/{folder}/",
HomeDirectoryMappings = new List<HomeDirectoryMapEntry>{new HomeDirectoryMapEntry{Entry = $"/{folder}", Target = $"/{ParentBucketName}/{folder}" } },
HomeDirectoryType = HomeDirectoryType.LOGICAL,
// Policy = GetSftpPolicy(),
Tags = GetTags(key, folder),
ServerId = SftpServerId,
UserName = $"user-dept-group-{key}",
Role = SftpRole,
//SshPublicKeyBody = "MY_DEPT_SSH.PUB_FILE"
};
var response = await awsTransferClient.CreateUserAsync(createUserRequest);
if (response.HttpStatusCode == HttpStatusCode.OK)
{
result.Add(response);
}
}
return result;
}
EDIT: One thing I noticed in all the examples is the s3 bucket folder names are mentioned as <s3bucket>/home/username
. Should I need to have this folder structure to make it work?
I found out that the HomeDirectory
was not needed and the Entry value must be set to \
. Below is how the createUserRequest
look like.
var createUserRequest = new CreateUserRequest
{
HomeDirectoryMappings = new List<HomeDirectoryMapEntry>{new HomeDirectoryMapEntry{Entry = "/", Target = $"/{ParentBucketName}/{folder}" } },
HomeDirectoryType = HomeDirectoryType.LOGICAL,
// Policy = GetSftpPolicy(),
Tags = GetTags(key, folder),
ServerId = SftpServerId,
UserName = $"user-dept-group-{key}",
Role = SftpRole,
//SshPublicKeyBody = "MY_DEPT_SSH.PUB_FILE"
};