How do I upload an image from iOS app (built with Kony) to AWS S3 using aws-sdk for javascript?

431 Views Asked by At

I am using Kony Visualizer (a low code platorm) to build a mobile app (builds an iOS and Android native app) that can post images taken by the user to an s3 bucket. I am currently using the aws sdk for js (easy import into this low code platform) and am trying to use an s3.putObject call to push the image up. I have found that I can push the base64 string of the image's rawbytes to s3 storage from an Android device, but am unsuccessful in doing so from an iOS device. I have posted this on the Kony forum too at this link and will paste my code below. Also, I know the connection to s3 is working, because I am posting csv files to the same bucket in another part of my code.

AWS.config.update({
    accessKeyId: <accessKeyId>,
    secretAccessKey: <secretAccessKey>,
    region: <region>
});

var rawImg = this.view.Picture.rawBytes;
var b64img = kony.convertToBase64(rawImg);

var bucketImage = new AWS.S3();
var paramsImage = {
    Bucket: <bucket-name>,
    Key: "images/imageB64.jpg",
    ContentType: 'image/jpg',
    Body: b64img};

bucketImage.putObject(paramsImage, function(err,res){
    if (err) {
        alert(err);}
    else {
        alert('Success');
    }
});
2

There are 2 best solutions below

0
On BEST ANSWER

I'm sure the FFI option would have worked, but I found that once I added the App Transport Security Settings -- Allow Arbitrary Loads - YES to my Info.plist in Xcode, and , these uploads started working. Apparently this uses http headers, which iOS doesn't automatically allow. I also had to add the following JSON to my infoplist_configuration.json file under my project in Kony Workspace: "NSAppTransportSecurity" : { "NSAllowsArbitraryLoads" : true }

3
On

If you continue having trouble doing the photo upload using the AWS SDK for JS, try the alternative route of creating a Foreign Function Interface (FFI) and using the AWS SDK for iOS. That's how the developer on my team got S3 uploads working in our Kony app.

He used the AWSS3TransferManager and AWSS3TransferManagerUploadRequest classes to do this.

Here's a code example in Swift