Search code examples
iosswiftaws-amplify

iOS: Amplify always storing files to public directory


I am using Amplify library to store files from iOS to AWS storage. My code looks something like this:

class UploadServiceController {
    
    static let `default` = UploadServiceController()
    
    init() {
        Amplify.Logging.logLevel = .verbose
        do {
            try Amplify.add(plugin: AWSCognitoAuthPlugin())
            try Amplify.add(plugin: AWSS3StoragePlugin())
            try Amplify.configure()
        } catch {
            assert(false, "An error occurred setting up Amplify: \(error)")
        }
    }
    
    func upload(data: Data, for filePath: String) -> UploadServiceOperation {
        let storageOperation = Amplify.Storage.uploadData(key: "media/images", data: data)
        return UploadServiceOperation(storageOperation: storageOperation)
    }
}

storage json:

"storage": {
            "plugins": {
                "awsS3StoragePlugin": {
                      "bucket": "native-media-storage",
                      "region": "eu-central-1"
                }
            }
        }

However when I perform upload my images are stored to: native-media-storage/public/media/images, instead of native-media-storage/media/images. I have browsed SO, I found solution for javascript: AWS amplify adding files in public directory, but nothing for iOS.

How can this be done on iOS?


Solution

  • While Amplify Docs leave a lot to be desired, browsing through their github, I found PR that adds this functionality. The PR is from September 2021, and here is the solution:

    // MARK: - Custom Prefix Resolver
    
    private struct CustomPrefixResolver: AWSS3PluginPrefixResolver {
        func resolvePrefix(for accessLevel: StorageAccessLevel,
                           targetIdentityId: String?) -> Result<String, StorageError> {
            return .success("")
        }
    }
    

    and use it like this:

    try Amplify.add(plugin: AWSS3StoragePlugin(configuration: .prefixResolver(CustomPrefixResolver())))