func performS3Mutation()

in AWSAppSyncTestApp/ViewController.swift [151:200]


    func performS3Mutation() throws {
        let testBundle = Bundle(for: ViewController.self)
        let testConfiguration = AppSyncClientTestConfiguration(with: testBundle)!
        
        guard appSyncClient != nil else  {
            self.resultLabel.text  = "Error: AppSync Client not initialized."
            return
        }
        
        // Note "public" prefix. See https://aws-amplify.github.io/docs/js/storage#using-amazon-s3
        let objectKey = "public/testS3Object-\(UUID().uuidString).jpg"
        let localURL = testBundle.url(forResource: "testS3Object", withExtension: ".jpg")!
        
        let region = AWSEndpoint.regionName(from: testConfiguration.bucketRegion)!
        
        let s3ObjectInput = S3ObjectInput(
            bucket: testConfiguration.bucketName,
            key: objectKey,
            region: region,
            localUri: localURL.path,
            mimeType: "image/jpeg")
        
        let createPostWithFile = CreatePostWithFileUsingParametersMutation(
            author: "Test S3 Object Author",
            title: "Test S3 Object Upload",
            content: "Testing S3 object upload",
            url: "http://www.example.testing.com",
            ups: 0,
            downs: 0,
            file: s3ObjectInput)
        
        appSyncClient?.perform(mutation: createPostWithFile,
                               queue: ViewController.mutationQueue) { result, error in
            if let error = error {
                DispatchQueue.main.async {
                    self.resultLabel.text = "Failed IAM normal mutation. Queue size: \(self.appSyncClient!.queuedMutationCount!). \n \(error.localizedDescription)"
                }
                return
            }
            guard let _ = result?.data?.createPostWithFileUsingParameters?.id else {
                DispatchQueue.main.async {
                    self.resultLabel.text = "Mutation result unexpectedly has nil ID. Queue size: \(self.appSyncClient!.queuedMutationCount!)."
                }
                return
            }
            DispatchQueue.main.async {
                self.resultLabel.text = "S3 mutation done successfully. Queue size: \(self.appSyncClient!.queuedMutationCount!)."
            }
        }
    }