You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.9 KiB

  1. package upload
  2. import (
  3. "errors"
  4. "fmt"
  5. "mime/multipart"
  6. "time"
  7. "github.com/flipped-aurora/gin-vue-admin/server/global"
  8. "github.com/aws/aws-sdk-go/aws"
  9. "github.com/aws/aws-sdk-go/aws/credentials"
  10. "github.com/aws/aws-sdk-go/aws/session"
  11. "github.com/aws/aws-sdk-go/service/s3"
  12. "github.com/aws/aws-sdk-go/service/s3/s3manager"
  13. "go.uber.org/zap"
  14. )
  15. type AwsS3 struct{}
  16. //@author: [WqyJh](https://github.com/WqyJh)
  17. //@object: *AwsS3
  18. //@function: UploadFile
  19. //@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
  20. //@param: file *multipart.FileHeader
  21. //@return: string, string, error
  22. func (*AwsS3) UploadFile(file *multipart.FileHeader) (string, string, error) {
  23. session := newSession()
  24. uploader := s3manager.NewUploader(session)
  25. fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename)
  26. filename := global.GVA_CONFIG.AwsS3.PathPrefix + "/" + fileKey
  27. f, openError := file.Open()
  28. if openError != nil {
  29. global.GVA_LOG.Error("function file.Open() Filed", zap.Any("err", openError.Error()))
  30. return "", "", errors.New("function file.Open() Filed, err:" + openError.Error())
  31. }
  32. defer f.Close() // 创建文件 defer 关闭
  33. _, err := uploader.Upload(&s3manager.UploadInput{
  34. Bucket: aws.String(global.GVA_CONFIG.AwsS3.Bucket),
  35. Key: aws.String(filename),
  36. Body: f,
  37. })
  38. if err != nil {
  39. global.GVA_LOG.Error("function uploader.Upload() Filed", zap.Any("err", err.Error()))
  40. return "", "", err
  41. }
  42. return global.GVA_CONFIG.AwsS3.BaseURL + "/" + filename, fileKey, nil
  43. }
  44. //@author: [WqyJh](https://github.com/WqyJh)
  45. //@object: *AwsS3
  46. //@function: DeleteFile
  47. //@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
  48. //@param: file *multipart.FileHeader
  49. //@return: string, string, error
  50. func (*AwsS3) DeleteFile(key string) error {
  51. session := newSession()
  52. svc := s3.New(session)
  53. filename := global.GVA_CONFIG.AwsS3.PathPrefix + "/" + key
  54. bucket := global.GVA_CONFIG.AwsS3.Bucket
  55. _, err := svc.DeleteObject(&s3.DeleteObjectInput{
  56. Bucket: aws.String(bucket),
  57. Key: aws.String(filename),
  58. })
  59. if err != nil {
  60. global.GVA_LOG.Error("function svc.DeleteObject() Filed", zap.Any("err", err.Error()))
  61. return errors.New("function svc.DeleteObject() Filed, err:" + err.Error())
  62. }
  63. _ = svc.WaitUntilObjectNotExists(&s3.HeadObjectInput{
  64. Bucket: aws.String(bucket),
  65. Key: aws.String(filename),
  66. })
  67. return nil
  68. }
  69. // newSession Create S3 session
  70. func newSession() *session.Session {
  71. sess, _ := session.NewSession(&aws.Config{
  72. Region: aws.String(global.GVA_CONFIG.AwsS3.Region),
  73. Credentials: credentials.NewStaticCredentials(
  74. global.GVA_CONFIG.AwsS3.SecretID,
  75. global.GVA_CONFIG.AwsS3.SecretKey,
  76. "",
  77. ),
  78. })
  79. return sess
  80. }