Tommy
Tommy

Reputation: 989

How to upload image to S3 using AWSUserFileManager Swift iOS

I've managed to setup AWS mobile hub and I can now login using my facebook credentials. Basically I just want to upload an image to the S3 bucket using AWSMobileHubHelper. I know in AWS Mobile hub things work differently to regular S3 upload. We're supposed to use AWSUserFileManager instead of the usual setup up AWScognito credentials + S3bucket etc.

I found this function in their documentation but there is no explanation whatsoever. I assume I can pass my image as NSData and the key would be my path "public/aaa". I ran the code everything seems fine. I doesn't even throw an error. But when I go to my S3 bucket,the image isn't there.

My question is: What am i suppose to pass as a key?? There is absolutely no documentation whatsoever on this.

If this is not how to do it? How does AWSUserFileManager work?

func uploadWithData(data: NSData, forKey key: String) {
    let userFilemanager = AWSUserFileManager.defaultUserFileManager()
    let localContent = userFilemanager.localContentWithData(data, key: key)
    localContent.uploadWithPinOnCompletion(false, progressBlock: {(content: AWSLocalContent?, progress: NSProgress?) -> Void in
        // handle progress here
        }, completionHandler: {(content: AWSContent?, error: NSError?) -> Void in
            if let error = error {
                // handle error here
                print("Error occured in uploading: \(error)")
                return
            }
            // handle successful upload here
    })
}

Thanks.

Upvotes: 1

Views: 728

Answers (1)

Tommy
Tommy

Reputation: 989

I found a solution to this. Basically, this is correct.

To upload data to a public folder you can simply use the exact function above. while the key = "public/yourFileName"

But if you want to upload to a private folder, you need to add the identityId when you declare the AWSUserFileManager, and the key would be = "private/(identityId!)/yourFileName":

func uploadWithData(data: NSData, forKey key: String) {
    let userFilemanager = AWSUserFileManager.defaultUserFileManager().identityId!
    let localContent = userFilemanager.localContentWithData(data, key: key)
    localContent.uploadWithPinOnCompletion(false, progressBlock: {(content: AWSLocalContent?, progress: NSProgress?) -> Void in
        // handle progress here
        }, completionHandler: {(content: AWSContent?, error: NSError?) -> Void in
            if let error = error {
                // handle error here
                print("Error occured in uploading: \(error)")
                return
            }
            // handle successful upload here
    })
}

Upvotes: 1

Related Questions