Browse Source

增加通过ID下载文件的预签名接口

gitlink
Sydonian 8 months ago
parent
commit
2970041208
3 changed files with 46 additions and 2 deletions
  1. +3
    -2
      client/internal/http/aws_auth.go
  2. +42
    -0
      client/internal/http/presigned.go
  3. +1
    -0
      client/internal/http/server.go

+ 3
- 2
client/internal/http/aws_auth.go View File

@@ -102,7 +102,7 @@ func (a *AWSAuth) Auth(c *gin.Context) {


verifySig := getSignatureFromAWSHeader(verifyReq) verifySig := getSignatureFromAWSHeader(verifyReq)
if !strings.EqualFold(verifySig, reqSig) { if !strings.EqualFold(verifySig, reqSig) {
logger.Warnf("signature mismatch, expect(%d): %s, actual(%d): %s", len(reqSig), reqSig, len(verifySig), verifySig)
logger.Warnf("signature mismatch, input header: %s, verify: %s", authorizationHeader, verifySig)
c.AbortWithStatusJSON(http.StatusOK, Failed(errorcode.Unauthorized, "signature mismatch")) c.AbortWithStatusJSON(http.StatusOK, Failed(errorcode.Unauthorized, "signature mismatch"))
return return
} }
@@ -158,7 +158,7 @@ func (a *AWSAuth) AuthWithoutBody(c *gin.Context) {


verifySig := getSignatureFromAWSHeader(verifyReq) verifySig := getSignatureFromAWSHeader(verifyReq)
if !strings.EqualFold(verifySig, reqSig) { if !strings.EqualFold(verifySig, reqSig) {
logger.Warnf("signature mismatch, expect(%d): %s, actual(%d): %s", len(reqSig), reqSig, len(verifySig), verifySig)
logger.Warnf("signature mismatch, input header: %s, verify: %s", authorizationHeader, verifySig)
c.AbortWithStatusJSON(http.StatusOK, Failed(errorcode.Unauthorized, "signature mismatch")) c.AbortWithStatusJSON(http.StatusOK, Failed(errorcode.Unauthorized, "signature mismatch"))
return return
} }
@@ -221,6 +221,7 @@ func (a *AWSAuth) PresignedAuth(c *gin.Context) {


verifySig := getSignatureFromAWSQuery(uri) verifySig := getSignatureFromAWSQuery(uri)
if !strings.EqualFold(verifySig, signature) { if !strings.EqualFold(verifySig, signature) {
logger.Warnf("signature mismatch, input: %s, verify: %s", signature, verifySig)
c.AbortWithStatusJSON(http.StatusOK, Failed(errorcode.Unauthorized, "signature mismatch")) c.AbortWithStatusJSON(http.StatusOK, Failed(errorcode.Unauthorized, "signature mismatch"))
return return
} }


+ 42
- 0
client/internal/http/presigned.go View File

@@ -82,6 +82,48 @@ func (s *PresignedService) ObjectDownloadByPath(ctx *gin.Context) {
} }
} }


func (s *PresignedService) ObjectDownload(ctx *gin.Context) {
log := logger.WithField("HTTP", "Presigned.ObjectDownloadByPath")

var req cdsapi.PresignedObjectDownload
if err := ctx.ShouldBindQuery(&req); err != nil {
log.Warnf("binding query: %s", err.Error())
ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
return
}

off := req.Offset
len := int64(-1)
if req.Length != nil {
len = *req.Length
}

file, err := s.svc.ObjectSvc().Download(req.UserID, downloader.DownloadReqeust{
ObjectID: req.ObjectID,
Offset: off,
Length: len,
})
if err != nil {
log.Warnf("downloading object: %s", err.Error())
ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "download object failed"))
return
}
defer file.File.Close()

ctx.Header("Content-Disposition", "attachment; filename="+url.PathEscape(path.Base(file.Object.Path)))
ctx.Header("Content-Type", "application/octet-stream")
ctx.Header("Content-Transfer-Encoding", "binary")

n, err := io.Copy(ctx.Writer, file.File)
if err != nil {
log.Warnf("copying file: %s", err.Error())
}

if config.Cfg().StorageID > 0 {
s.svc.AccessStat.AddAccessCounter(file.Object.ObjectID, file.Object.PackageID, config.Cfg().StorageID, math2.DivOrDefault(float64(n), float64(file.Object.Size), 1))
}
}

func (s *PresignedService) ObjectUpload(ctx *gin.Context) { func (s *PresignedService) ObjectUpload(ctx *gin.Context) {
log := logger.WithField("HTTP", "Presigned.ObjectUpload") log := logger.WithField("HTTP", "Presigned.ObjectUpload")




+ 1
- 0
client/internal/http/server.go View File

@@ -125,6 +125,7 @@ func (s *Server) routeV1(eg *gin.Engine, rt gin.IRoutes) {
rt.POST(cdsapi.ObjectCompleteMultipartUploadPath, s.Object().CompleteMultipartUpload) rt.POST(cdsapi.ObjectCompleteMultipartUploadPath, s.Object().CompleteMultipartUpload)


rt.GET(cdsapi.PresignedObjectDownloadByPathPath, s.awsAuth.PresignedAuth, s.Presigned().ObjectDownloadByPath) rt.GET(cdsapi.PresignedObjectDownloadByPathPath, s.awsAuth.PresignedAuth, s.Presigned().ObjectDownloadByPath)
rt.GET(cdsapi.PresignedObjectDownloadPath, s.awsAuth.PresignedAuth, s.Presigned().ObjectDownload)
rt.POST(cdsapi.PresignedObjectUploadPath, s.awsAuth.PresignedAuth, s.Presigned().ObjectUpload) rt.POST(cdsapi.PresignedObjectUploadPath, s.awsAuth.PresignedAuth, s.Presigned().ObjectUpload)


rt.POST(cdsapi.PresignedObjectNewMultipartUploadPath, s.awsAuth.PresignedAuth, s.Presigned().ObjectNewMultipartUpload) rt.POST(cdsapi.PresignedObjectNewMultipartUploadPath, s.awsAuth.PresignedAuth, s.Presigned().ObjectNewMultipartUpload)


Loading…
Cancel
Save