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.

65 lines
1.7 KiB

  1. package upload
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
  5. "github.com/pkg/errors"
  6. "mime/multipart"
  7. )
  8. var HuaWeiObs = new(_obs)
  9. type _obs struct{}
  10. func NewHuaWeiObsClient() (client *obs.ObsClient, err error) {
  11. return obs.New(global.GVA_CONFIG.HuaWeiObs.AccessKey, global.GVA_CONFIG.HuaWeiObs.SecretKey, global.GVA_CONFIG.HuaWeiObs.Endpoint)
  12. }
  13. func (o *_obs) UploadFile(file *multipart.FileHeader) (filename string, filepath string, err error) {
  14. var open multipart.File
  15. open, err = file.Open()
  16. if err != nil {
  17. return filename, filepath, err
  18. }
  19. filename = file.Filename
  20. input := &obs.PutObjectInput{
  21. PutObjectBasicInput: obs.PutObjectBasicInput{
  22. ObjectOperationInput: obs.ObjectOperationInput{
  23. Bucket: global.GVA_CONFIG.HuaWeiObs.Bucket,
  24. Key: filename,
  25. },
  26. ContentType: file.Header.Get("content-type"),
  27. },
  28. Body: open,
  29. }
  30. var client *obs.ObsClient
  31. client, err = NewHuaWeiObsClient()
  32. if err != nil {
  33. return filepath, filename, errors.Wrap(err, "获取华为对象存储对象失败!")
  34. }
  35. _, err = client.PutObject(input)
  36. if err != nil {
  37. return filepath, filename, errors.Wrap(err, "文件上传失败!")
  38. }
  39. filepath = global.GVA_CONFIG.HuaWeiObs.Path + "/" + filename
  40. return filepath, filename, err
  41. }
  42. func (o *_obs) DeleteFile(key string) error {
  43. client, err := NewHuaWeiObsClient()
  44. if err != nil {
  45. return errors.Wrap(err, "获取华为对象存储对象失败!")
  46. }
  47. input := &obs.DeleteObjectInput{
  48. Bucket: global.GVA_CONFIG.HuaWeiObs.Bucket,
  49. Key: key,
  50. }
  51. var output *obs.DeleteObjectOutput
  52. output, err = client.DeleteObject(input)
  53. if err != nil {
  54. return errors.Wrapf(err, "删除对象(%s)失败!, output: %v", key, output)
  55. }
  56. return nil
  57. }