AWSS3 Transfer Utility Complete Functioning (using Swift iOS)
This demo app requires you to register on AWS and get the Bucket-region, Bucket-name from there to use in your app for further steps.
Features :-
- Single Video/Image upload (With functionality of Pause, Resume, Cancel)
- Multiple Videos/Images upload (With each has functionality of Upload, Pause, Resume, Cancel)
- See the list of running uploads in-progress.
- Mutlipart Video/Image upload.
- Get the list of uploaded Videos/Images.
- Easily Delete the already uploaded Videos/Images from your bucket
Let's Start With actual codes
1. We require pod file changes such as
pod 'AWSS3'
pod 'AWSMobileClient'
2. Some mandatory changes in info.plist file
3. awsconfiguration.json file that is downloaded from AWS while you created your bucket. Put this file in the same tree directory where your info.plist is present. The below screenshot shows the demo content within it.
4. Some important changes in Appdelegate.swift file.
Here the first method "handleEventsForBackgroundURLSession" will manage background upload tasks.
Second method will help us to get default AWSServiceManager set up with our Region and PoolId configuration keys.
import AWSS3
import AWSMobileClient
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
AWSS3TransferUtility.interceptApplication(application, handleEventsForBackgroundURLSession: identifier, completionHandler: completionHandler)
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.APNorthEast2, identityPoolId: "Your-PoolId-In-String-Format")
let configuration = AWSServiceConfiguration(region: AWSRegionType.APNorthEast2, credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
return AWSMobileClient.sharedInstance().interceptApplication(application, didFinishLaunchingWithOptions: launchOptions)
}
5. Create singleton class with any name (I given UploadManager.swift), It will manage all your upload tasks, handling pause/resume/cancel of each task simultaneously, multiple in-Progress uploads, provides list of current running tasks, handling delete of the uploads from your AWS bucket. We will have enum for type of assest to be uploaded and many more.
enum MediaContentType: String {
case Video = "movie/mov"
case Image = "image/png"
}
enum UploadStatus: String {
case Pause = "Pause"
case Resume = "Resume"
case Cancelled = "Cancelled"
case InProgress = "InProgress"
case Failed = "Failed"
case Success = "Success"
}
6. The above class will handle all the status of each running task via single instance variable of Transfer Utility
lazyvar transferUtility: AWSS3TransferUtility = {
return AWSS3TransferUtility.default()
}()
The above transferUtility give update to the UIViewController/collectionViewController/TableViewController via NotificationCenter.
You just have to create anonymous callbacks for 3 important callbacks which we got from above transferUtility. These are as follows
- continuationTaskBlock - It will be fired only once when your upload starts and the uploading task gets TransferId from AWSS3 sdk. This TransferId will be used by your controller to distinguish the uploads which are Initiated by that controller. If you want to save instance of that task for further manipluation then you can put in variable like (var uploadTask: AWSS3TransferUtilityUploadTask). In this closure you will fire a ContinuationNotification.
- progressBlock - It will be fired multiple times while you video/image is getting uploaded. This block will give you the progess of upload between 0.0 - 1.0 range. 1.0 will denote that your upload completed successfully. In this closure you will fire a ProgressNotification.
- completionBlock - It will be fired when your upload completes successfully or with error. In this closure you will fire a CompletionNotification.
7. Custom Model will be useful for developer perspective when he want to show/manage multiple uploads in CollectionView/TableView at a time.
class UploadModel{
var tranferId: String = ""
var progress: Double = 0.0
var status: String = ""
var failed: Bool = false
var completed: Bool = false
var inProgess: Bool = false
var cancelled: Bool = false
var paused: Bool = false
init(){
}
}
You can keep shared dictionary with upload file URL as key and above model as Value for distinguishing each upload in your whole app.
e.g. var dict: [String: UploadModel] = ["video1.mov": UploadModel(), "video2.mov": UploadModel(), "image1.png": UploadModel()]
As per the dictionary you can update your views or collectionviewcells/ tableviewcells.
On each notification from progressBlock, continuationBlock, completionBlock you can easily update above dict and as per that dict values your view gets updated.
By using this approach you can easily manage many number of uploads at a time.
8. Key Name (Asset name is important)
For uploading any asset you have to pass key name for that upload and that key name will be assigned to that asset on your AWS cloud bucket. You can keep that key names in array for future purpose such as delete or download from cloud.
9. Delete Asset
For deleting the uploaded asset from AWS bucket you have to only give the key name which you had set when you started a upload for that asset.
No comments:
Post a Comment