1 Answer
- Newest
- Most votes
- Most comments
0
Hello.
This error may occur if the access key cannot be obtained for some reason.
Does this occur even if I embed the access key directly in the code like below?
public class ServiceHandler: ObservableObject {
private var client: S3Client?
init() {
Task(priority: .high) {
do {
client = try S3Client(
accessKeyId: "your-access-key",
secretAccessKey: "your-secret-key",
region: "us-east-2"
)
} catch {
print(error)
}
}
}
public func listBucketFiles(bucket: String) async throws -> [String] {
if let clientInstance = client {
let input = ListObjectsV2Input(bucket: bucket)
let output = try await clientInstance.listObjectsV2(input: input)
var names: [String] = []
guard let objList = output.contents else {
return []
}
for obj in objList {
if let objName = obj.key {
names.append(objName)
}
}
return names
} else {
print("Client has not been initialized!")
return []
}
}
}
struct ContentView: View {
@StateObject private var s3 = ServiceHandler()
var body: some View {
let tmpStrAry = try await s3.listBucketFiles(bucket: "my_bucket")
}
}
Relevant content
- asked 9 months ago
- AWS OFFICIALUpdated 8 months ago
- AWS OFFICIALUpdated 2 years ago
Please provide the code in a code block (i.e. within ```) for readability.