Reputation: 55
I am in the process of building an iOS App, and I've been stuck on S3 uploads for a few days. I have been following the documentation provided here:
https://aws-amplify.github.io/docs/ios/storage.
When I hit this function it appears as if everything worked in Xcode, and it returns no errors. However, when I go look in my S3 bucket I see that the image was never uploaded. Here is my upload function (I put the credential info in here as well):
I think my source of confusion is coming from getting credentials from Cognito. I feel like I need Cognito credentials to access the AWS Transfer Utility, and the code below seems like it should be doing that?
@IBAction func uploadData() {
let pool = AWSCognitoIdentityUserPool(forKey: "UserPool")
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast2, identityPoolId: "us-east-2:XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX", identityProviderManager:pool)
let serviceConfiguration = AWSServiceConfiguration(region: .USEast2, credentialsProvider: credentialsProvider)
let userPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: "XXXX", clientSecret: "XXXX", poolId: "us-east-XXXX")
let tu = AWSS3TransferUtilityConfiguration()
AWSS3TransferUtility.register(with: serviceConfiguration!, transferUtilityConfiguration: tu, forKey: "UserPool")
let transferUtility:(AWSS3TransferUtility?) = AWSS3TransferUtility.s3TransferUtility(forKey: "UserPool")
AWSServiceManager.default()?.defaultServiceConfiguration = serviceConfiguration
let data = self.imageView?.image?.pngData() // Data to be uploaded
let expression = AWSS3TransferUtilityUploadExpression()
expression.progressBlock = {(task, progress) in
DispatchQueue.main.async(execute: {
// Do something e.g. Update a progress bar.
})
}
var completionHandler: AWSS3TransferUtilityUploadCompletionHandlerBlock?
completionHandler = { (task, error) -> Void in
DispatchQueue.main.async(execute: {
// Do something e.g. Alert a user for transfer completion.
// On failed uploads, `error` contains the error object.
})
}
transferUtility!.uploadData(data as! Data,
bucket: "mybucket-env",
key: "YourFileName",
contentType: "image/png",
expression: expression,
completionHandler: completionHandler).continueWith {
(task) -> AnyObject? in
if let error = task.error {
print("Error: \(error.localizedDescription)")
}
if let _ = task.result {
// Do something with uploadTask.
}
return nil;
}
}
Upvotes: 0
Views: 407
Reputation: 157
The documentation has been updated: https://aws-amplify.github.io/docs/ios/storage
You can achieve the same functionality using AWS Amplify where you can use
Amplify CLI to provision S3 and cognito resources
Add configuration files to your app, awsconfiguration.json
and amplifyconfiguration.json
.
Install the dependencies
pod 'AmplifyPlugins/AWSS3StoragePlugin'
pod 'AWSMobileClient', '~> 2.12.0'
Initialize AWSMobileClient and Amplify
Amplify.Storage.uploadData()
Upvotes: 1