Home > database >  Set public-read ACL for AWS S3 data upload via iOS Amplify
Set public-read ACL for AWS S3 data upload via iOS Amplify

Time:02-10

I've been attempting to upload images to an existing resource in S3, the images need to be publicly viewable and our website expects that the app sets the ACL to public-read on the file.

I have been unable to find a solution using the Amplify SDK that gets this done.

Currently even using the "guest" access level my images are not viewable at their S3 URLS.

Does anyone know how to set the "public-read" ACL during upload using the iOS Amplify SDK?

https://docs.amplify.aws/lib/storage/configureaccess/q/platform/ios/

CodePudding user response:

Have you tried using "protected"?

CodePudding user response:

I was able to hack together something that works for now using the escape hatch of the Amplify SDK.

https://docs.amplify.aws/lib/storage/escapehatch/q/platform/ios/

   func uploadToS3(path: URL, data: Data, bucketName: String, uploadKeyName: String, contentType: String) {
   
        do {
            let plugin = try Amplify.Storage.getPlugin(for: "awsS3StoragePlugin") as? AWSS3StoragePlugin
            if let escapedPlugin =  plugin {
                let awsS3 = escapedPlugin.getEscapeHatch()


                let request = AWSS3PutObjectRequest()


                if let req = request {
                    req.body = data
                    req.contentType = contentType
                    req.contentLength = NSNumber(integerLiteral: NSData(data: data).length)
                    req.bucket = bucketName
                    req.key = uploadKeyName
                    req.acl = .publicRead

                    awsS3.putObject(req).continueWith { (task) -> AnyObject? in
                        if let error = task.error {
                            print("there was an error with uploading image \(error)")

                        }

                        if task.result != nil {
                            let s3URL = NSURL(string: "http://s3.amazonaws.com/\(bucketName)/\(uploadKeyName)")
                            print("Uploaded to:\n\(s3URL)")
                        }

                        return nil
                    }
                }
            }

        } catch {
            print("Get escape hatch failed with error - \(error)")
        }
}
  • Related