feat(infra): support uploading files via io.Reader (#1793)

This commit is contained in:
Ryo
2025-08-25 18:25:12 +08:00
committed by GitHub
parent 6fa2acf05a
commit 14ce6bc112
6 changed files with 115 additions and 36 deletions

View File

@@ -138,6 +138,11 @@ func (m *minioClient) test() {
}
func (m *minioClient) PutObject(ctx context.Context, objectKey string, content []byte, opts ...storage.PutOptFn) error {
opts = append(opts, storage.WithObjectSize(int64(len(content))))
return m.PutObjectWithReader(ctx, objectKey, bytes.NewReader(content), opts...)
}
func (m *minioClient) PutObjectWithReader(ctx context.Context, objectKey string, content io.Reader, opts ...storage.PutOptFn) error {
option := storage.PutOption{}
for _, opt := range opts {
opt(&option)
@@ -165,7 +170,7 @@ func (m *minioClient) PutObject(ctx context.Context, objectKey string, content [
}
_, err := m.client.PutObject(ctx, m.bucketName, objectKey,
bytes.NewReader(content), int64(len(content)), minioOpts)
content, option.ObjectSize, minioOpts)
if err != nil {
return fmt.Errorf("PutObject failed: %v", err)
}

View File

@@ -152,16 +152,47 @@ func (t *s3Client) CheckAndCreateBucket(ctx context.Context) error {
}
func (t *s3Client) PutObject(ctx context.Context, objectKey string, content []byte, opts ...storage.PutOptFn) error {
opts = append(opts, storage.WithObjectSize(int64(len(content))))
return t.PutObjectWithReader(ctx, objectKey, bytes.NewReader(content), opts...)
}
func (t *s3Client) PutObjectWithReader(ctx context.Context, objectKey string, content io.Reader, opts ...storage.PutOptFn) error {
client := t.client
body := bytes.NewReader(content)
bucket := t.bucketName
// upload object
_, err := client.PutObject(ctx, &s3.PutObjectInput{
option := storage.PutOption{}
for _, opt := range opts {
opt(&option)
}
input := &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(objectKey),
Body: body,
})
Body: content,
}
if option.ContentType != nil {
input.ContentType = option.ContentType
}
if option.ContentEncoding != nil {
input.ContentEncoding = option.ContentEncoding
}
if option.ContentDisposition != nil {
input.ContentDisposition = option.ContentDisposition
}
if option.ContentLanguage != nil {
input.ContentLanguage = option.ContentLanguage
}
if option.Expires != nil {
input.Expires = option.Expires
}
if option.ObjectSize > 0 {
input.ContentLength = aws.Int64(option.ObjectSize)
}
// upload object
_, err := client.PutObject(ctx, input)
return err
}

View File

@@ -140,22 +140,49 @@ func (t *tosClient) CheckAndCreateBucket(ctx context.Context) error {
return err
}
func (t *tosClient) PutObject(ctx context.Context, objectKey string, content []byte, opts ...storage.PutOptFn) error {
opts = append(opts, storage.WithObjectSize(int64(len(content))))
return t.PutObjectWithReader(ctx, objectKey, bytes.NewReader(content), opts...)
}
func (t *tosClient) PutObjectWithReader(ctx context.Context, objectKey string, content io.Reader, opts ...storage.PutOptFn) error {
client := t.client
body := bytes.NewReader(content)
bucketName := t.bucketName
_, err := client.PutObjectV2(ctx, &tos.PutObjectV2Input{
option := storage.PutOption{}
for _, opt := range opts {
opt(&option)
}
input := &tos.PutObjectV2Input{
PutObjectBasicInput: tos.PutObjectBasicInput{
Bucket: bucketName,
Key: objectKey,
},
Content: body,
})
Content: content,
}
// logs.CtxDebugf(ctx, "PutObject resp: %v, err: %v", conv.DebugJsonToStr(output), err)
if option.ContentType != nil {
input.ContentType = *option.ContentType
}
if option.ContentEncoding != nil {
input.ContentEncoding = *option.ContentEncoding
}
if option.ContentDisposition != nil {
input.ContentDisposition = *option.ContentDisposition
}
if option.ContentLanguage != nil {
input.ContentLanguage = *option.ContentLanguage
}
if option.Expires != nil {
input.Expires = *option.Expires
}
if option.ObjectSize > 0 {
input.ContentLength = option.ObjectSize
}
_, err := client.PutObjectV2(ctx, input)
return err
}