feat: 完成了 新建桶; 上传文件(基本功能)
todo: 上传 rename, 上传 public 权限选择 bug: 首次加载 conns list; 上传的时候前缀过滤失败
This commit is contained in:
36
internal/s3/create.go
Normal file
36
internal/s3/create.go
Normal file
@ -0,0 +1,36 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||
)
|
||||
|
||||
func (c *Client) CreateBucket(ctx context.Context, bucket string, publicRead bool, publicReadWrite bool) error {
|
||||
var (
|
||||
err error
|
||||
input = &s3.CreateBucketInput{
|
||||
Bucket: aws.String(bucket),
|
||||
ACL: types.BucketCannedACLAuthenticatedRead,
|
||||
}
|
||||
|
||||
output = &s3.CreateBucketOutput{}
|
||||
)
|
||||
|
||||
if publicRead {
|
||||
input.ACL = types.BucketCannedACLPublicRead
|
||||
}
|
||||
|
||||
if publicReadWrite {
|
||||
input.ACL = types.BucketCannedACLPublicReadWrite
|
||||
}
|
||||
|
||||
if output, err = c.client.CreateBucket(ctx, input); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_ = output
|
||||
|
||||
return nil
|
||||
}
|
62
internal/s3/put.go
Normal file
62
internal/s3/put.go
Normal file
@ -0,0 +1,62 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PutFilesObj struct {
|
||||
Key string
|
||||
Reader io.ReadSeeker
|
||||
ContentLength int64
|
||||
ContentType string
|
||||
ExpireAt int64
|
||||
PublicRead bool
|
||||
PublicReadWrite bool
|
||||
}
|
||||
|
||||
func (c *Client) PutFile(ctx context.Context, bucket string, obj *PutFilesObj) error {
|
||||
var (
|
||||
err error
|
||||
input = &s3.PutObjectInput{
|
||||
Bucket: aws.String(bucket),
|
||||
Key: aws.String(obj.Key),
|
||||
Body: obj.Reader,
|
||||
ACL: types.ObjectCannedACLPrivate,
|
||||
}
|
||||
output *s3.PutObjectOutput
|
||||
)
|
||||
|
||||
if obj.ExpireAt > 0 {
|
||||
t := time.UnixMilli(obj.ExpireAt)
|
||||
input.Expires = &t
|
||||
}
|
||||
|
||||
if obj.ContentLength > 0 {
|
||||
input.ContentLength = aws.Int64(obj.ContentLength)
|
||||
}
|
||||
|
||||
if obj.ContentType == "" {
|
||||
input.ContentType = aws.String(obj.ContentType)
|
||||
}
|
||||
|
||||
if obj.PublicRead {
|
||||
input.ACL = types.ObjectCannedACLPublicRead
|
||||
}
|
||||
|
||||
if obj.PublicReadWrite {
|
||||
input.ACL = types.ObjectCannedACLPublicReadWrite
|
||||
}
|
||||
|
||||
if output, err = c.client.PutObject(ctx, input); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_ = output
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user