duanbi1983 2016-01-27 11:32
浏览 58
已采纳

从Go lang中的响应流创建Image magick对象

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") 
}
  • 写回答

1条回答 默认 最新

  • douwenan9849 2016-01-27 20:11
    关注

    You only need a DownloadManager if you want to download large files more efficiently. A DownloadManager requires a WriterAt (generally an os.File), which you would have to implement yourself over a []byte or use a file in memory.

    If you fetch the object directly, you can read it into a []byte which can be passed to 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)
    }
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题