Check if File exists in AWS S3 Storage with Amplify

Viewed 1123

I'm using AWS S3 Storage with Amplify and to avoid multiple uploads of the same file i want to check if the file already exists.

Currently by getting the download url via Amplify but it also generates an url if the file doesn't exists. I was hoping it returns an error:

_ = Amplify.Storage.getURL(key: "myKey") { event in
switch event {
case let .success(url):
    print("Completed: \(url)")
case let .failure(storageError):
    print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
}

}

Are there any other ways to check if files exists in amplify?

Without downloading it of course. The whole point is to save traffic.

1 Answers

It looks like you might be able to do something similar with Amplify.Storage.list as shown here https://docs.amplify.aws/lib/storage/list/q/platform/ios

_ = Amplify.Storage.list { event in
    switch event {
    case .success(let listResult):
        let keys = listResult.items.map { $0.key }
        if !keys.contains("myKey") {
            // upload unique file
        }
        
    case .failure(let error):
        print("Failed: \(error.errorDescription).")
    }
}
Related