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!!!