移除预签名url相关实现

This commit is contained in:
2026-01-04 16:08:23 +08:00
parent 9e83ae16af
commit e761ff5be5
2 changed files with 0 additions and 78 deletions

View File

@@ -79,57 +79,6 @@ func (s *StorageClient) GetBucket(name string) (string, error) {
return bucket, nil
}
// GeneratePresignedURL 生成预签名上传URL (PUT方法)
func (s *StorageClient) GeneratePresignedURL(ctx context.Context, bucketName, objectName string, expires time.Duration) (string, error) {
url, err := s.client.PresignedPutObject(ctx, bucketName, objectName, expires)
if err != nil {
return "", fmt.Errorf("生成预签名URL失败: %w", err)
}
return url.String(), nil
}
// PresignedPostPolicyResult 预签名POST策略结果
type PresignedPostPolicyResult struct {
PostURL string // POST的URL
FormData map[string]string // 表单数据
FileURL string // 文件的最终访问URL
}
// GeneratePresignedPostURL 生成预签名POST URL (支持表单上传)
// 注意使用时必须确保file字段是表单的最后一个字段
func (s *StorageClient) GeneratePresignedPostURL(ctx context.Context, bucketName, objectName string, minSize, maxSize int64, expires time.Duration) (*PresignedPostPolicyResult, error) {
// 创建上传策略
policy := minio.NewPostPolicy()
// 设置策略的基本信息
policy.SetBucket(bucketName)
policy.SetKey(objectName)
policy.SetExpires(time.Now().UTC().Add(expires))
// 设置文件大小限制
if err := policy.SetContentLengthRange(minSize, maxSize); err != nil {
return nil, fmt.Errorf("设置文件大小限制失败: %w", err)
}
// 使用MinIO客户端和策略生成预签名的POST URL和表单数据
postURL, formData, err := s.client.PresignedPostPolicy(ctx, policy)
if err != nil {
return nil, fmt.Errorf("生成预签名POST URL失败: %w", err)
}
// 移除form_data中多余的bucket字段MinIO Go SDK可能会添加这个字段但会导致签名错误
// 注意在Go中直接delete不存在的key是安全的
delete(formData, "bucket")
// 使用配置的公开访问URL构造文件的永久访问URL
fileURL := s.BuildFileURL(bucketName, objectName)
return &PresignedPostPolicyResult{
PostURL: postURL.String(),
FormData: formData,
FileURL: fileURL,
}, nil
}
// BuildFileURL 构建文件的公开访问URL
func (s *StorageClient) BuildFileURL(bucketName, objectName string) string {