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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程