I have set up AWS S3 bucket and I'm able to upload files there using Vapor 3 and Postman. I can also get files using Vapor 3 and Postman but I have a hard time to get and actually display files(png -images) on my browser (I'm using leaf).
So how can I display image files on the view? I'm new with HTML, AWS S3 and Vapor.
I'm using:
- Vapor 3
- AWS S3
- Leaf package
- S3 package
- VaporExt package
I followed this tutorial to set up everything (except the get request and the bucket policies): https://fivedottwelve.com/blog/using-amazon-s3-with-vapor/
Here are my Vapor code:
/// GET reguest
func preparePresignedImageUrl(request: Request) throws -> String {
let baseUrl = awsConfig.url
let imagePath = awsConfig.imagePath
let fileName = "x.png"
guard var url = URL(string: baseUrl) else {
throw Abort(.internalServerError)
}
url.appendPathComponent(imagePath)
url.appendPathComponent(fileName)
print("url is \(url)")
let headers = ["x-amz-acl" : "public-read"]
let s3 = try request.makeS3Signer()
let result = try s3.presignedURL(for: .GET, url: url, expiration: Expiration.hour, headers: headers)
/// Retrieve file data from S3
guard let presignedUrl = result?.absoluteString else {
throw Abort(.internalServerError)
}
return presignedUrl
}
The route:
// GET request
group.get("aws", "image", use: preparePresignedImageUrl)
And in the Postman when I make a GET request to that presignedURL
it gives me a status code 200OK.
My showImage.leaf
file:
#set("content") {
<h1>#(title)</h1>
// Here some html to get the image path and display the image?
<img>
}
#embed("base")