s3manager-web/internal/app/s3manager/get_object.go

34 lines
890 B
Go
Raw Normal View History

2017-05-08 23:07:07 +02:00
package s3manager
2017-03-30 22:48:27 +02:00
import (
"fmt"
"io"
"net/http"
2018-05-31 16:10:41 +02:00
"github.com/matryer/way"
2017-12-05 23:30:28 +01:00
minio "github.com/minio/minio-go"
2017-05-25 18:33:44 +02:00
"github.com/pkg/errors"
2017-03-30 22:48:27 +02:00
)
2018-05-31 16:10:41 +02:00
// HandleGetObject downloads an object to the client.
func HandleGetObject(s3 S3) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bucketName := way.Param(r.Context(), "bucketName")
objectName := way.Param(r.Context(), "objectName")
2017-03-30 22:48:27 +02:00
2017-12-05 23:30:28 +01:00
object, err := s3.GetObject(bucketName, objectName, minio.GetObjectOptions{})
2017-03-30 22:48:27 +02:00
if err != nil {
2017-05-25 18:33:44 +02:00
handleHTTPError(w, errors.Wrap(err, "error getting object"))
2017-03-30 22:48:27 +02:00
return
}
2018-05-22 22:56:01 +02:00
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", objectName))
w.Header().Set("Content-Type", "application/octet-stream")
2017-03-30 22:48:27 +02:00
_, err = io.Copy(w, object)
if err != nil {
2017-05-25 18:33:44 +02:00
handleHTTPError(w, errors.Wrap(err, "error copying object to response writer"))
2017-03-30 22:48:27 +02:00
return
}
2018-05-31 16:10:41 +02:00
}
2017-03-30 22:48:27 +02:00
}