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

35 lines
906 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"
"github.com/gorilla/mux"
2021-08-05 11:44:40 +02:00
"github.com/minio/minio-go/v7"
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, forceDownload bool) http.HandlerFunc {
2018-05-31 16:10:41 +02:00
return func(w http.ResponseWriter, r *http.Request) {
bucketName := mux.Vars(r)["bucketName"]
objectName := mux.Vars(r)["objectName"]
2017-03-30 22:48:27 +02:00
2021-08-05 11:44:40 +02:00
object, err := s3.GetObject(r.Context(), bucketName, objectName, minio.GetObjectOptions{})
2017-03-30 22:48:27 +02:00
if err != nil {
2019-09-05 00:44:02 +02:00
handleHTTPError(w, fmt.Errorf("error getting object: %w", err))
2017-03-30 22:48:27 +02:00
return
}
if forceDownload {
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 {
2019-09-05 00:44:02 +02:00
handleHTTPError(w, fmt.Errorf("error copying object to response writer: %w", err))
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
}