I am using the following code to download and upload the images from Amazon S3
. Now, after downloading the image i want to resize it using the imagick library, but without writing it on to the disk. So, how do i create the image magick object directly from response stream which i will get from S3 and upload the same on Amazon S3. Could you please suggest the changes for the same in below code?
Also, How do i change it to a http handler which takes the value of key from query string?
I have commented out my code of image magick object reason being i am sure how to write it.
func main() {
file, err := os.Create("download_file")
if err != nil {
log.Fatal("Failed to create file", err)
}
defer file.Close()
downloader := s3manager.NewDownloader(session.New(&aws.Config{Region: aws.String(REGION_NAME)}))
numBytes, err := downloader.Download(file,
&s3.GetObjectInput{
Bucket: aws.String(BUCKET_NAME),
Key: aws.String(KEY),
})
if err != nil {
fmt.Println("Failed to download file", err)
return
}
fmt.Println("Downloaded file", file.Name(), numBytes, "bytes")
//mw := imagick.NewMagickWand()
// defer mw.Destroy()
// err = mw.ReadImage(file)
// if err != nil {
// panic(err)
// }
// using io.Pipe read/writer file contents.
reader, writer := io.Pipe()
go func() {
io.Copy(writer, file)
file.Close()
writer.Close()
}()
uploader := s3manager.NewUploader(session.New(&aws.Config{Region: aws.String(REGION_NAME)}))
result, err := uploader.Upload(&s3manager.UploadInput{
Body: reader,
Bucket: aws.String(BUCKET),
Key: aws.String(KEY),
})
if err != nil {
log.Fatalln("Failed to upload", err)
}
log.Println("Successfully uploaded to", result.Location)
fmt.Println("code ran successfully")
}