This commit is contained in:
@@ -59,11 +59,17 @@ func NewS3Provider(cfg *config.StorageConfig) (*S3Provider, error) {
|
||||
}
|
||||
|
||||
func (p *S3Provider) Upload(ctx context.Context, key string, reader io.Reader, contentType string) error {
|
||||
return p.UploadWithMetadata(ctx, key, reader, contentType, nil)
|
||||
}
|
||||
|
||||
// UploadWithMetadata 上传对象并保存用于完整性复核的 metadata。
|
||||
func (p *S3Provider) UploadWithMetadata(ctx context.Context, key string, reader io.Reader, contentType string, metadata map[string]string) error {
|
||||
input := &s3manager.UploadInput{
|
||||
Bucket: aws.String(p.bucket),
|
||||
Key: aws.String(key),
|
||||
Body: reader,
|
||||
ContentType: aws.String(contentType),
|
||||
Metadata: aws.StringMap(metadata),
|
||||
}
|
||||
|
||||
_, err := p.uploader.UploadWithContext(ctx, input)
|
||||
@@ -73,6 +79,26 @@ func (p *S3Provider) Upload(ctx context.Context, key string, reader io.Reader, c
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stat 读取对象大小、内容类型和 metadata。
|
||||
func (p *S3Provider) Stat(ctx context.Context, key string) (*ObjectMetadata, error) {
|
||||
result, err := p.client.HeadObjectWithContext(ctx, &s3.HeadObjectInput{
|
||||
Bucket: aws.String(p.bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取对象 metadata 失败: %w", err)
|
||||
}
|
||||
metadata := make(map[string]string, len(result.Metadata))
|
||||
for name, value := range result.Metadata {
|
||||
metadata[strings.ToLower(name)] = aws.StringValue(value)
|
||||
}
|
||||
return &ObjectMetadata{
|
||||
Size: aws.Int64Value(result.ContentLength),
|
||||
ContentType: aws.StringValue(result.ContentType),
|
||||
Metadata: metadata,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *S3Provider) Download(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
input := &s3.GetObjectInput{
|
||||
Bucket: aws.String(p.bucket),
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
|
||||
type Provider interface {
|
||||
Upload(ctx context.Context, key string, reader io.Reader, contentType string) error
|
||||
UploadWithMetadata(ctx context.Context, key string, reader io.Reader, contentType string, metadata map[string]string) error
|
||||
Stat(ctx context.Context, key string) (*ObjectMetadata, error)
|
||||
Download(ctx context.Context, key string) (io.ReadCloser, error)
|
||||
DownloadToTemp(ctx context.Context, key string) (localPath string, cleanup func(), err error)
|
||||
Delete(ctx context.Context, key string) error
|
||||
@@ -15,3 +17,10 @@ type Provider interface {
|
||||
GetUploadURL(ctx context.Context, key string, contentType string, expires time.Duration) (string, error)
|
||||
GetDownloadURL(ctx context.Context, key string, expires time.Duration) (string, error)
|
||||
}
|
||||
|
||||
// ObjectMetadata 是对象存储返回的受控对象属性。
|
||||
type ObjectMetadata struct {
|
||||
Size int64
|
||||
ContentType string
|
||||
Metadata map[string]string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user