Create Image magick object from response stream in go lang

huangapple go评论88阅读模式
英文:

Create Image magick object from response stream in go lang

问题

我正在使用以下代码从Amazon S3下载和上传图像。现在,在下载图像后,我想使用imagick库调整图像大小,但不将其写入磁盘。那么,我该如何直接从S3获取的响应流创建图像魔术对象,并将其上传到Amazon S3上呢?请为下面的代码提供相应的更改建议。
另外,我该如何将其更改为一个HTTP处理程序,该处理程序从查询字符串中获取键的值?

我已经注释掉了我的图像魔术对象的代码,原因是我不确定如何编写它。

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") 
}
英文:

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

得分: 2

只有在您想要更高效地下载大文件时才需要使用DownloadManager。DownloadManager需要一个WriterAt(通常是os.File),您需要自己在[]byte上实现它,或者使用内存中的文件。

如果您直接获取对象,可以将其读入[]byte,然后将其传递给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)
}
...
英文:

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)
}
...

huangapple
  • 本文由 发表于 2016年1月27日 19:32:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/35036200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定