文档 AWS SDK 示例 GitHub 存储库中还有更多 S AWS DK 示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 SDK for Swift 的 IAM 示例
以下代码示例向您展示了如何使用带有 IAM 的 Swift AWS 开发工具包来执行操作和实现常见场景。
操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。
每个示例都包含一个指向完整源代码的链接,您可以从中找到有关如何在上下文中设置和运行代码的说明。
主题
操作
以下代码示例演示如何使用 AttachRolePolicy
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func attachRolePolicy(role: String, policyArn: String) async throws { let input = AttachRolePolicyInput( policyArn: policyArn, roleName: role ) do { _ = try await client.attachRolePolicy(input: input) } catch { print("ERROR: Attaching a role policy:", dump(error)) throw error } }
-
如需了解 API 的详细信息,请参阅适用AttachRolePolicy
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 CreateAccessKey
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func createAccessKey(userName: String) async throws -> IAMClientTypes.AccessKey { let input = CreateAccessKeyInput( userName: userName ) do { let output = try await iamClient.createAccessKey(input: input) guard let accessKey = output.accessKey else { throw ServiceHandlerError.keyError } return accessKey } catch { print("ERROR: createAccessKey:", dump(error)) throw error } }
-
如需了解 API 的详细信息,请参阅适用CreateAccessKey
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 CreatePolicy
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func createPolicy(name: String, policyDocument: String) async throws -> IAMClientTypes.Policy { let input = CreatePolicyInput( policyDocument: policyDocument, policyName: name ) do { let output = try await iamClient.createPolicy(input: input) guard let policy = output.policy else { throw ServiceHandlerError.noSuchPolicy } return policy } catch { print("ERROR: createPolicy:", dump(error)) throw error } }
-
如需了解 API 的详细信息,请参阅适用CreatePolicy
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 CreateRole
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func createRole(name: String, policyDocument: String) async throws -> String { let input = CreateRoleInput( assumeRolePolicyDocument: policyDocument, roleName: name ) do { let output = try await client.createRole(input: input) guard let role = output.role else { throw ServiceHandlerError.noSuchRole } guard let id = role.roleId else { throw ServiceHandlerError.noSuchRole } return id } catch { print("ERROR: createRole:", dump(error)) throw error } }
-
如需了解 API 的详细信息,请参阅适用CreateRole
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 CreateServiceLinkedRole
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func createServiceLinkedRole(service: String, suffix: String? = nil, description: String?) async throws -> IAMClientTypes.Role { let input = CreateServiceLinkedRoleInput( awsServiceName: service, customSuffix: suffix, description: description ) do { let output = try await client.createServiceLinkedRole(input: input) guard let role = output.role else { throw ServiceHandlerError.noSuchRole } return role } catch { print("ERROR: createServiceLinkedRole:", dump(error)) throw error } }
-
如需了解 API 的详细信息,请参阅适用CreateServiceLinkedRole
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 CreateUser
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func createUser(name: String) async throws -> String { let input = CreateUserInput( userName: name ) do { let output = try await client.createUser(input: input) guard let user = output.user else { throw ServiceHandlerError.noSuchUser } guard let id = user.userId else { throw ServiceHandlerError.noSuchUser } return id } catch { print("ERROR: createUser:", dump(error)) throw error } }
-
如需了解 API 的详细信息,请参阅适用CreateUser
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 DeleteAccessKey
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func deleteAccessKey(user: IAMClientTypes.User? = nil, key: IAMClientTypes.AccessKey) async throws { let userName: String? if user != nil { userName = user!.userName } else { userName = nil } let input = DeleteAccessKeyInput( accessKeyId: key.accessKeyId, userName: userName ) do { _ = try await iamClient.deleteAccessKey(input: input) } catch { print("ERROR: deleteAccessKey:", dump(error)) throw error } }
-
如需了解 API 的详细信息,请参阅适用DeleteAccessKey
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 DeletePolicy
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func deletePolicy(policy: IAMClientTypes.Policy) async throws { let input = DeletePolicyInput( policyArn: policy.arn ) do { _ = try await iamClient.deletePolicy(input: input) } catch { print("ERROR: deletePolicy:", dump(error)) throw error } }
-
如需了解 API 的详细信息,请参阅适用DeletePolicy
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 DeleteRole
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func deleteRole(role: IAMClientTypes.Role) async throws { let input = DeleteRoleInput( roleName: role.roleName ) do { _ = try await iamClient.deleteRole(input: input) } catch { print("ERROR: deleteRole:", dump(error)) throw error } }
-
如需了解 API 的详细信息,请参阅适用DeleteRole
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 DeleteUser
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func deleteUser(user: IAMClientTypes.User) async throws { let input = DeleteUserInput( userName: user.userName ) do { _ = try await iamClient.deleteUser(input: input) } catch { print("ERROR: deleteUser:", dump(error)) throw error } }
-
如需了解 API 的详细信息,请参阅适用DeleteUser
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 DeleteUserPolicy
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 func deleteUserPolicy(user: IAMClientTypes.User, policyName: String) async throws { let input = DeleteUserPolicyInput( policyName: policyName, userName: user.userName ) do { _ = try await iamClient.deleteUserPolicy(input: input) } catch { print("ERROR: deleteUserPolicy:", dump(error)) throw error } }
-
如需了解 API 的详细信息,请参阅适用DeleteUserPolicy
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 DetachRolePolicy
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func detachRolePolicy(policy: IAMClientTypes.Policy, role: IAMClientTypes.Role) async throws { let input = DetachRolePolicyInput( policyArn: policy.arn, roleName: role.roleName ) do { _ = try await iamClient.detachRolePolicy(input: input) } catch { print("ERROR: detachRolePolicy:", dump(error)) throw error } }
-
如需了解 API 的详细信息,请参阅适用DetachRolePolicy
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 GetPolicy
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func getPolicy(arn: String) async throws -> IAMClientTypes.Policy { let input = GetPolicyInput( policyArn: arn ) do { let output = try await client.getPolicy(input: input) guard let policy = output.policy else { throw ServiceHandlerError.noSuchPolicy } return policy } catch { print("ERROR: getPolicy:", dump(error)) throw error } }
-
如需了解 API 的详细信息,请参阅适用GetPolicy
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 GetRole
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func getRole(name: String) async throws -> IAMClientTypes.Role { let input = GetRoleInput( roleName: name ) do { let output = try await client.getRole(input: input) guard let role = output.role else { throw ServiceHandlerError.noSuchRole } return role } catch { print("ERROR: getRole:", dump(error)) throw error } }
-
如需了解 API 的详细信息,请参阅适用GetRole
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 GetUser
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func getUser(name: String? = nil) async throws -> IAMClientTypes.User { let input = GetUserInput( userName: name ) do { let output = try await iamClient.getUser(input: input) guard let user = output.user else { throw ServiceHandlerError.noSuchUser } return user } catch { print("ERROR: getUser:", dump(error)) throw error } }
-
如需了解 API 的详细信息,请参阅适用GetUser
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 ListAttachedRolePolicies
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 /// Returns a list of AWS Identity and Access Management (IAM) policies /// that are attached to the role. /// /// - Parameter role: The IAM role to return the policy list for. /// /// - Returns: An array of `IAMClientTypes.AttachedPolicy` objects /// describing each managed policy that's attached to the role. public func listAttachedRolePolicies(role: String) async throws -> [IAMClientTypes.AttachedPolicy] { var policyList: [IAMClientTypes.AttachedPolicy] = [] // Use "Paginated" to get all the attached role polices. // This lets the SDK handle the 'isTruncated' in "ListAttachedRolePoliciesOutput". let input = ListAttachedRolePoliciesInput( roleName: role ) let output = client.listAttachedRolePoliciesPaginated(input: input) do { for try await page in output { guard let attachedPolicies = page.attachedPolicies else { print("Error: no attached policies returned.") continue } for attachedPolicy in attachedPolicies { policyList.append(attachedPolicy) } } } catch { print("ERROR: listAttachedRolePolicies:", dump(error)) throw error } return policyList }
-
如需了解 API 的详细信息,请参阅适用ListAttachedRolePolicies
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 ListGroups
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func listGroups() async throws -> [String] { var groupList: [String] = [] // Use "Paginated" to get all the groups. // This lets the SDK handle the 'isTruncated' property in "ListGroupsOutput". let input = ListGroupsInput() let pages = client.listGroupsPaginated(input: input) do { for try await page in pages { guard let groups = page.groups else { print("Error: no groups returned.") continue } for group in groups { if let name = group.groupName { groupList.append(name) } } } } catch { print("ERROR: listGroups:", dump(error)) throw error } return groupList }
-
如需了解 API 的详细信息,请参阅适用ListGroups
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 ListPolicies
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func listPolicies() async throws -> [MyPolicyRecord] { var policyList: [MyPolicyRecord] = [] // Use "Paginated" to get all the policies. // This lets the SDK handle the 'isTruncated' in "ListPoliciesOutput". let input = ListPoliciesInput() let output = client.listPoliciesPaginated(input: input) do { for try await page in output { guard let policies = page.policies else { print("Error: no policies returned.") continue } for policy in policies { guard let name = policy.policyName, let id = policy.policyId, let arn = policy.arn else { throw ServiceHandlerError.noSuchPolicy } policyList.append(MyPolicyRecord(name: name, id: id, arn: arn)) } } } catch { print("ERROR: listPolicies:", dump(error)) throw error } return policyList }
-
如需了解 API 的详细信息,请参阅适用ListPolicies
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 ListRolePolicies
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func listRolePolicies(role: String) async throws -> [String] { var policyList: [String] = [] // Use "Paginated" to get all the role policies. // This lets the SDK handle the 'isTruncated' in "ListRolePoliciesOutput". let input = ListRolePoliciesInput( roleName: role ) let pages = client.listRolePoliciesPaginated(input: input) do { for try await page in pages { guard let policies = page.policyNames else { print("Error: no role policies returned.") continue } for policy in policies { policyList.append(policy) } } } catch { print("ERROR: listRolePolicies:", dump(error)) throw error } return policyList }
-
如需了解 API 的详细信息,请参阅适用ListRolePolicies
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 ListRoles
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func listRoles() async throws -> [String] { var roleList: [String] = [] // Use "Paginated" to get all the roles. // This lets the SDK handle the 'isTruncated' in "ListRolesOutput". let input = ListRolesInput() let pages = client.listRolesPaginated(input: input) do { for try await page in pages { guard let roles = page.roles else { print("Error: no roles returned.") continue } for role in roles { if let name = role.roleName { roleList.append(name) } } } } catch { print("ERROR: listRoles:", dump(error)) throw error } return roleList }
-
如需了解 API 的详细信息,请参阅适用ListRoles
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 ListUsers
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 public func listUsers() async throws -> [MyUserRecord] { var userList: [MyUserRecord] = [] // Use "Paginated" to get all the users. // This lets the SDK handle the 'isTruncated' in "ListUsersOutput". let input = ListUsersInput() let output = client.listUsersPaginated(input: input) do { for try await page in output { guard let users = page.users else { continue } for user in users { if let id = user.userId, let name = user.userName { userList.append(MyUserRecord(id: id, name: name)) } } } } catch { print("ERROR: listUsers:", dump(error)) throw error } return userList }
-
如需了解 API 的详细信息,请参阅适用ListUsers
于 S wift 的AWS SDK API 参考。
-
以下代码示例演示如何使用 PutUserPolicy
。
- 适用于 Swift 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import AWSIAM import AWSS3 func putUserPolicy(policyDocument: String, policyName: String, user: IAMClientTypes.User) async throws { let input = PutUserPolicyInput( policyDocument: policyDocument, policyName: policyName, userName: user.userName ) do { _ = try await iamClient.putUserPolicy(input: input) } catch { print("ERROR: putUserPolicy:", dump(error)) throw error } }
-
如需了解 API 的详细信息,请参阅适用PutUserPolicy
于 S wift 的AWS SDK API 参考。
-