Before starting with the example I'd recommend to create a Singleton with a delegate class member so you could achieve a use case of uploading a file in the background and let the user keep using your app while the files are being uploaded even when the app is the background.
Let's start, first, we should create an enum that represent the S3 configuration:
Now, we should set the credentials when your app launch for the first time, thus, we should set them inside the AppDelegate at the didFinishLaunchingWithOptions method (pay attention that you should set your region at the regionType param):
Since we are already inside the AppDelegate, we should implement the background callback that is handled by the AWS SDK:
Now, when the user will move the app to the background your upload will continue the actual upload.
In order to upload the file using the AWS SDK we will have to write the file to the device and give the SDK the actual path. For the sake of the example, imagine we have a UIImage (could be a video also..) and we will write it to a temp folder:
FileURL and fileName will be used for the actual uploading later.
There are 2 closures we will have to define that are provided by the AWS SDK,
AWSS3TransferUtilityUploadCompletionHandlerBlock - A closure that notifies when the upload is done (or not)
AWSS3TransferUtilityUploadProgressBlock - A closure that notifies each byte sent
If you plan to have a Singleton you should define those types as class members. The implementation should look like this:
NOTE: If you are using a Singleton you might want to define a delegate that will report back with progress or when the file is done. If you are not using a Singleton you can create a static method that would have the relevant types:
progressFunctionUpdater - will report back to a function with progress.
resultBlock - If you return nil then upload was successfully else, you send the error object
Ladies and gentlemen, the actual upload:
Happy S3 uploading :)
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0