MCPcopy
hub / github.com/kingwrcy/moments / S3PreSigned

Method S3PreSigned

backend/handler/file.go:293–367  ·  view source on GitHub ↗

S3PreSigned godoc @Tags File @Summary S3预签名 @Accept json @Produce json @Param object body PreSignedReq true "S3预签名" @Param x-api-token header string true "登录TOKEN" @Success 200 {object} s3PresignedResp @Router /api/file/s3PreSigned [post]

(c echo.Context)

Source from the content-addressed store, hash-verified

291// @Success 200 {object} s3PresignedResp
292// @Router /api/file/s3PreSigned [post]
293func (f FileHandler) S3PreSigned(c echo.Context) error {
294 var (
295 req PreSignedReq
296 sysConfig db.SysConfig
297 sysConfigVo vo.FullSysConfigVO
298 )
299 if err := c.Bind(&req); err != nil {
300 return FailResp(c, ParamError)
301 }
302
303 if err := f.base.db.First(&sysConfig).Error; errors.Is(err, gorm.ErrRecordNotFound) {
304 return FailResp(c, Fail)
305 }
306
307 if err := json.Unmarshal([]byte(sysConfig.Content), &sysConfigVo); err != nil {
308 f.base.log.Error().Msgf("无法反序列化系统配置, %s", err)
309 return FailRespWithMsg(c, Fail, err.Error())
310 }
311
312 cfg, err := config.LoadDefaultConfig(
313 context.TODO(),
314 config.WithRegion(sysConfigVo.S3.Region),
315 config.WithEndpointResolver(
316 aws.EndpointResolverFunc(
317 func(service, region string) (aws.Endpoint, error) {
318 return aws.Endpoint{URL: sysConfigVo.S3.Endpoint}, nil
319 },
320 ),
321 ),
322 config.WithCredentialsProvider(
323 credentials.NewStaticCredentialsProvider(
324 sysConfigVo.S3.AccessKey,
325 sysConfigVo.S3.SecretKey,
326 "",
327 ),
328 ),
329 )
330 if err != nil {
331 f.base.log.Error().Msgf("无法加载SDK配置, %s", err)
332 return FailRespWithMsg(c, Fail, err.Error())
333 }
334
335 client := s3.NewFromConfig(cfg)
336 presignedClient := s3.NewPresignClient(client)
337
338 key := fmt.Sprintf(
339 "moments/%s/%s",
340 time.Now().Format("2006/01/02"),
341 strings.ReplaceAll(uuid.NewString(), "-", ""),
342 )
343 presignedResult, err := presignedClient.PresignPutObject(
344 context.TODO(),
345 &s3.PutObjectInput{
346 Bucket: aws.String(sysConfigVo.S3.Bucket),
347 Key: aws.String(key),
348 ContentType: aws.String(req.ContentType),
349 },
350 func(opts *s3.PresignOptions) {

Callers

nothing calls this directly

Calls 4

FailRespFunction · 0.85
FailRespWithMsgFunction · 0.85
SuccessRespFunction · 0.85
ErrorMethod · 0.80

Tested by

no test coverage detected