I’m building an app in SwiftUI and right now I have it so users can select a photo from their gallery to upload. When they do, I save it locally and store a URL variable for fetching it from it’s file path location. I use this URL to load the image in the view to show the user what image they selected.
Now after the user finishes, I want to call an API to upload the image to AWS. However, with how things are set up right now in the project I’m working in, I need to rename the file to a specific name before uploading it to the database.
Now I know before uploading, I’m going to have to convert the image to its data by doing something like:
let uiImage = UIImage(contentsOfFile: filepath)
let data = uiImage.pngData()
And then use the data to send to AWS. While I haven’t tested this yet, I am required to use a presigned URL to upload the image and so will likely do so by setting up a PUT request such as this:
let session = URLSession(configuration: URLSessionConfiguration.default)
var request: URLRequest = URLRequest(url: URL(string: url)!)
request.httpMethod = "PUT"
request.httpBody = data
request.setValue("image/png", forHTTPHeaderField: "Content-Type")
But my question is what exactly is this Data object? How can I rename it to match the format I need the image file name to be so then when I upload to the database, it will be saved there with the correct name?