func testS3UploadUsingFileInput()

in AWSAppSyncIntegrationTests/AWSAppSyncCognitoAuthTests.swift [115:171]


    func testS3UploadUsingFileInput() throws {
        let testBundle = Bundle(for: AWSAppSyncCognitoAuthTests.self)
        let testConfiguration = AppSyncClientTestConfiguration(with: testBundle)!
        let appSyncClient = try makeS3EnabledAppSyncClient(testConfiguration: testConfiguration, testBundle: testBundle)

        // 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 postCreated = expectation(description: "Post created successfully.")

        let s3ObjectInput = S3ObjectInput(
            bucket: testConfiguration.bucketName,
            key: objectKey,
            region: region,
            localUri: localURL.path,
            mimeType: "image/jpeg")

        let addPostWithFileInput = CreatePostWithFileInput(
            author: "Test S3 Object Author",
            title: "Test S3 Object Upload",
            content: "Testing S3 object upload",
            ups: 0,
            downs: 0,
            file: s3ObjectInput)

        let createPostWithFile = CreatePostWithFileUsingInputTypeMutation(input: addPostWithFileInput)

        var mutationResult: GraphQLResult<CreatePostWithFileUsingInputTypeMutation.Data>? = nil
        appSyncClient.perform(mutation: createPostWithFile,
                              queue: AWSAppSyncCognitoAuthTests.mutationQueue) { result, error in
            XCTAssertNil(error)
            mutationResult = result
            postCreated.fulfill()
        }

        wait(for: [postCreated], timeout: AWSAppSyncCognitoAuthTests.networkOperationTimeout)

        guard let postId = mutationResult?.data?.createPostWithFileUsingInputType?.id else {
            XCTFail("Mutation result unexpectedly has nil ID")
            return
        }

        let destinationURL: URL
        do {
            destinationURL = try downloadFile(from: postId, with: appSyncClient)
        } catch {
            XCTFail("Error downloading file from \(postId): \(error.localizedDescription)")
            return
        }

        let file1Data = try Data(contentsOf: destinationURL)
        let file2Data = try Data(contentsOf: localURL)
        XCTAssertEqual(file1Data, file2Data)
    }