1

I'm trying to upload an image to an S3 bucket using the putObject method in the AWS Swift SDK. I have followed the instructions here: https://docs.aws.amazon.com/sdk-for-swift/latest/developer-guide/examples-s3-objects.html

I am able to add strings to the s3 bucket, but can't figure out how to add images. I have tried converting the images to jpegdata and pngdata to no avail.

Here is my code:

//      AWS S3 image upload
        func uploadFile(withImage image: UIImage) {
            let s3Client = try? S3Client(region: "us-west-2")

            let bucketName = "xxxxx"
            let imageData = image.pngData()
            guard let dataToUpload = "Text to upload working".data(using: .utf8) else {
                return
            }
            let body = ByteStream.from(data: imageData!)
            s3Client!.putObject(input: PutObjectInput(body: body, bucket: bucketName, contentType: "image/png", key: "Test")) { result in
                switch(result) {
                case .success(let response):
                    if let eTag = response.eTag {
                        print("Successfully uploaded the file with the etag: \(eTag)")
                    }
                case .failure(let err):
                    print(err)
                }
            }
        }

If I place "dataToUpload" into the ByteStream.from function, the data gets stored in the s3 bucket. If I use the imagedata, however, it does not.

Open to any and all solutions, Thanks!!!

1 Answer 1

6

you can also Use AWSS3TransferUtility to upload files to S3

Steps

1)Setup bucket credential before uploading or simply in your app delegate by this method setupCreditial()

  1. upload file to s3 by using the uploadFileToS3 method in your source view

      /** regionType is your bucket region name identityPoolId is your bucket unique id*/
    
     func setupCreditial(){
    
    
         let credentialProvider = AWSCognitoCredentialsProvider(regionType: .AFSouth1, identityPoolId: "YourPoolID")
         let config = AWSServiceConfiguration.init(region: .AFSouth1, credentialsProvider: credentialProvider)
    
         AWSServiceManager.default().defaultServiceConfiguration = config
         AWSS3TransferUtility.register(with: config!, forKey: "NAMEOFUTILITY")
     }
     /*
      key is your aws3 key where you want to put image . in most cases it would public/key where key = any udid
      content type is is file type like image,pdf,word etc
      **/
     public func uploadFileToS3(withKey key : String , contentType type : String, andData data : Data) {
         let awsUploadExp = AWSS3TransferUtilityUploadExpression()
         awsUploadExp.progressBlock = {task ,progress in
             //progress goes here
         }
         guard   let transferUtililty  = AWSS3TransferUtility.s3TransferUtility(forKey: "NAMEOFUTILITY") else{
             return
         }
         let completionHandler : AWSS3TransferUtilityUploadCompletionHandlerBlock = {(task,error)-> Void in
             //handle your completion
         }
    
         transferUtililty.uploadData(data, bucket: "YourBucketname", key: key, contentType: type, expression: awsUploadExp, completionHandler: completionHandler)
     }
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.