s3manager-web/internal/app/s3manager/get_object.go
Sergey Shevchenko c077332721 Refactoring:
- Replaced router by gorrila/mux
- Used viper for environment variables instead of os
- Added option to forbid object deletion
- Added option to list bucket recursively
- Added option to not add donwload headers in getObject
- Added ability to download nested objects
- Object donwloading will open in a new tab

Signed-off-by: Sergey Shevchenko <sergeyshevchdevelop@gmail.com>
2022-03-28 11:37:49 +02:00

34 lines
906 B
Go

package s3manager
import (
"fmt"
"io"
"net/http"
"github.com/gorilla/mux"
"github.com/minio/minio-go/v7"
)
// HandleGetObject downloads an object to the client.
func HandleGetObject(s3 S3, forceDownload bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bucketName := mux.Vars(r)["bucketName"]
objectName := mux.Vars(r)["objectName"]
object, err := s3.GetObject(r.Context(), bucketName, objectName, minio.GetObjectOptions{})
if err != nil {
handleHTTPError(w, fmt.Errorf("error getting object: %w", err))
return
}
if forceDownload {
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", objectName))
w.Header().Set("Content-Type", "application/octet-stream")
}
_, err = io.Copy(w, object)
if err != nil {
handleHTTPError(w, fmt.Errorf("error copying object to response writer: %w", err))
return
}
}
}