我正在使用以下代码从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")
}
呼啦一阵风
相关分类