The docs for the AWS SDk for Swift describe how to instantiate a service client:
let s3Client = try S3Client(region: "us-east-1")
However since this statement can throw errors it needs to be wrapped in a do catch block as shown on a separate documentation page:
do {
let s3 = try S3Client(region: "af-south-1")
// .....
} catch {
dump(error, name: "Error accessing S3 service")
exit(1)
}
I've created a public S3Manager class that will contain the specific functions I want to use in my code for manipulating s3 buckets. Typically I would instantiate the client in the class initializer but since it must be wrapped in a do catch block I have taken a different approach:
public class S3manager : ObservableObject {
public var client: S3Client?
public func initializeClient() {
do {
client = try S3Client(region: "us-west-2")
} catch {
fatalError("Error creating S3 client: \(error)")
}
}
public func listBucketFiles(bucket: String) async throws -> [String] {
if let clientInstance = client {
// ...
}
}
}
In my views I will reference the class as a StateObject or ObservedObject as appropriate and be certain to call initializeClient before calling any other methods I implement in the class (in the initial view's onAppear() method for example) Is this the best approach though?