minio/presigned_url.go

38 lines
913 B
Go
Raw Normal View History

2023-01-06 04:56:26 +00:00
package minio
import (
"context"
"fmt"
2023-01-08 13:39:17 +00:00
"net/url"
2023-01-12 09:08:17 +00:00
"strings"
2023-01-06 04:56:26 +00:00
"time"
)
2023-01-12 09:08:17 +00:00
func (c Client) GetPresignedURL(ctx context.Context, bucket, object, fileType string, expiry time.Duration) string {
2023-01-06 04:56:26 +00:00
// default 15 min
if expiry == 0 {
expiry = 15 * time.Minute
}
// params
2023-01-08 13:39:17 +00:00
reqParams := make(url.Values)
reqParams.Set("response-content-disposition", fmt.Sprintf("attachment; filename=\"%s\"", object))
2023-01-06 04:56:26 +00:00
2023-01-12 09:08:17 +00:00
// add content type for video
if fileType == ListTypes.Video {
2023-01-12 09:26:22 +00:00
parts := strings.Split(object, ".")
2023-01-12 09:08:17 +00:00
reqParams.Set("response-content-type", fmt.Sprintf("video/%s", parts[len(parts)-1]))
}
2023-01-06 04:56:26 +00:00
// get
2023-01-08 13:39:17 +00:00
presignedURL, err := c.client.PresignedGetObject(ctx, bucket, object, expiry, reqParams)
2023-01-06 04:56:26 +00:00
if err != nil {
2023-01-08 13:39:17 +00:00
fmt.Printf("[minio.GetPresignedURL][%s] get object '%s' error: %s \n", bucket, object, err.Error())
fmt.Printf("params %v \n", reqParams)
2023-01-06 04:56:26 +00:00
return ""
}
// return
return presignedURL.String()
}