amazon s3 - Create Image magick object from response stream in go lang -
i using following code download , upload images amazon s3
. now, after downloading image want resize using imagick library, without writing on disk. so, how create image magick object directly response stream s3 , upload same on amazon s3. please suggest changes same in below code? also, how change http handler takes value of key query string?
i have commented out code of image magick object reason being sure how write it.
func main() { file, err := os.create("download_file") if err != nil { log.fatal("failed 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 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 upload", err) } log.println("successfully uploaded to", result.location) fmt.println("code ran successfully") }
you need downloadmanager if want download large files more efficiently. downloadmanager requires writerat
(generally os.file
), have implement on []byte
or use file in memory.
if fetch object directly, can read []byte
can passed readimageblob
:
out, err := s3client.getobject(&s3.getobjectinput{ bucket: aws.string(bucket), key: aws.string(key), }) if err != nil { log.fatal(err) } img, err := ioutil.readall(out.body) if err != nil { log.fatal(err) } mw := imagick.newmagickwand() defer mw.destroy() err = mw.readimageblob(img) if err != nil { log.fatal(err) } ...
Comments
Post a Comment