Last active
July 28, 2025 10:40
-
-
Save maksadbek/85d10e171b822c81466b618fc0b3bce8 to your computer and use it in GitHub Desktop.
example code to aws-sigv4 sign custom http request with aws-sdk swift. Code doesn't compile, you should amend it a little bit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import AWSClientRuntime | |
import AWSSigner | |
import AWSSDKHTTPAuth | |
import Foundation | |
import AWSSDKIdentity | |
import SmithyHTTPAPI | |
import SmithyHTTPAuth | |
import Smithy | |
import AwsCommonRuntimeKit | |
import SmithyHTTPAuthAPI | |
import ClientRuntime | |
let credentialResolver = ProfileAWSCredentialIdentityResolver() | |
let identity = try await credentialResolver.getIdentity() | |
let url = URL(string: host) | |
guard | |
let urlComponents = URLComponents(url: url!, resolvingAgainstBaseURL: false) else { | |
return | |
} | |
let config = AWSSigningConfig( | |
credentials: identity, | |
expiration: 3600, | |
signedBodyHeader: .contentSha256, | |
signedBodyValue: .empty, | |
flags: SigningFlags( | |
useDoubleURIEncode: false, | |
shouldNormalizeURIPath: false, | |
omitSessionToken: false | |
), | |
date: Date(), | |
service: "service", | |
region: "earth-region-1", | |
signatureType: .requestHeaders, | |
signingAlgorithm: .sigv4 | |
) | |
let requestBuilder = HTTPRequestBuilder() | |
.withMethod(.post) | |
.withHost(urlComponents.host!) | |
.withPath("/") | |
.withPort(UInt16(exactly: urlComponents.port!)!) | |
.withProtocol(URIScheme(rawValue: urlComponents.scheme!)!) | |
.withHeaders(Headers(httpHeaders: [ | |
HTTPHeader(name: "Host", value: host), | |
HTTPHeader(name: "content-type", value: "application/json"), | |
])) | |
.withBody(.data(Data(payload.utf8))) | |
CommonRuntimeKit.initialize() | |
let signer = AWSSigV4Signer() | |
guard let signedRequest = await signer.sigV4SignedRequest(requestBuilder: requestBuilder, signingConfig: config) else { | |
return | |
} | |
print(signedRequest.headers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment