奇淼(piexlmax
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 125 additions and 1 deletions
-
9server/config.yaml
-
1server/config/config.go
-
10server/config/oss_aws.go
-
1server/go.mod
-
9server/go.sum
-
94server/utils/upload/aws_s3.go
-
2server/utils/upload/upload.go
@ -0,0 +1,10 @@ |
|||
package config |
|||
|
|||
type AwsS3 struct { |
|||
Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"` |
|||
Region string `mapstructure:"region" json:"region" yaml:"region"` |
|||
SecretID string `mapstructure:"secret-id" json:"secretID" yaml:"secret-id"` |
|||
SecretKey string `mapstructure:"secret-key" json:"secretKey" yaml:"secret-key"` |
|||
BaseURL string `mapstructure:"base-url" json:"baseURL" yaml:"base-url"` |
|||
PathPrefix string `mapstructure:"path-prefix" json:"pathPrefix" yaml:"path-prefix"` |
|||
} |
@ -0,0 +1,94 @@ |
|||
package upload |
|||
|
|||
import ( |
|||
"errors" |
|||
"fmt" |
|||
"mime/multipart" |
|||
"time" |
|||
|
|||
"github.com/flipped-aurora/gin-vue-admin/server/global" |
|||
|
|||
"github.com/aws/aws-sdk-go/aws" |
|||
"github.com/aws/aws-sdk-go/aws/credentials" |
|||
"github.com/aws/aws-sdk-go/aws/session" |
|||
"github.com/aws/aws-sdk-go/service/s3" |
|||
"github.com/aws/aws-sdk-go/service/s3/s3manager" |
|||
"go.uber.org/zap" |
|||
) |
|||
|
|||
type AwsS3 struct{} |
|||
|
|||
//@author: [WqyJh](https://github.com/WqyJh)
|
|||
//@object: *AwsS3
|
|||
//@function: UploadFile
|
|||
//@description: Upload file to Aws S3 using aws-sdk-go. See https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/s3-example-basic-bucket-operations.html#s3-examples-bucket-ops-upload-file-to-bucket
|
|||
//@param: file *multipart.FileHeader
|
|||
//@return: string, string, error
|
|||
|
|||
func (*AwsS3) UploadFile(file *multipart.FileHeader) (string, string, error) { |
|||
session := newSession() |
|||
uploader := s3manager.NewUploader(session) |
|||
|
|||
fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename) |
|||
filename := global.GVA_CONFIG.AwsS3.PathPrefix + "/" + fileKey |
|||
f, openError := file.Open() |
|||
if openError != nil { |
|||
global.GVA_LOG.Error("function file.Open() Filed", zap.Any("err", openError.Error())) |
|||
return "", "", errors.New("function file.Open() Filed, err:" + openError.Error()) |
|||
} |
|||
defer f.Close() // 创建文件 defer 关闭
|
|||
|
|||
_, err := uploader.Upload(&s3manager.UploadInput{ |
|||
Bucket: aws.String(global.GVA_CONFIG.AwsS3.Bucket), |
|||
Key: aws.String(filename), |
|||
Body: f, |
|||
}) |
|||
if err != nil { |
|||
global.GVA_LOG.Error("function uploader.Upload() Filed", zap.Any("err", err.Error())) |
|||
return "", "", err |
|||
} |
|||
|
|||
return global.GVA_CONFIG.AwsS3.BaseURL + "/" + filename, fileKey, nil |
|||
} |
|||
|
|||
//@author: [WqyJh](https://github.com/WqyJh)
|
|||
//@object: *AwsS3
|
|||
//@function: DeleteFile
|
|||
//@description: Delete file from Aws S3 using aws-sdk-go. See https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/s3-example-basic-bucket-operations.html#s3-examples-bucket-ops-delete-bucket-item
|
|||
//@param: file *multipart.FileHeader
|
|||
//@return: string, string, error
|
|||
|
|||
func (*AwsS3) DeleteFile(key string) error { |
|||
session := newSession() |
|||
svc := s3.New(session) |
|||
filename := global.GVA_CONFIG.AwsS3.PathPrefix + "/" + key |
|||
bucket := global.GVA_CONFIG.AwsS3.Bucket |
|||
|
|||
_, err := svc.DeleteObject(&s3.DeleteObjectInput{ |
|||
Bucket: aws.String(bucket), |
|||
Key: aws.String(filename), |
|||
}) |
|||
if err != nil { |
|||
global.GVA_LOG.Error("function svc.DeleteObject() Filed", zap.Any("err", err.Error())) |
|||
return errors.New("function svc.DeleteObject() Filed, err:" + err.Error()) |
|||
} |
|||
|
|||
_ = svc.WaitUntilObjectNotExists(&s3.HeadObjectInput{ |
|||
Bucket: aws.String(bucket), |
|||
Key: aws.String(filename), |
|||
}) |
|||
return nil |
|||
} |
|||
|
|||
// newSession Create S3 session
|
|||
func newSession() *session.Session { |
|||
sess, _ := session.NewSession(&aws.Config{ |
|||
Region: aws.String(global.GVA_CONFIG.AwsS3.Region), |
|||
Credentials: credentials.NewStaticCredentials( |
|||
global.GVA_CONFIG.AwsS3.SecretID, |
|||
global.GVA_CONFIG.AwsS3.SecretKey, |
|||
"", |
|||
), |
|||
}) |
|||
return sess |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue