aliyun.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package oss
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. const (
  8. ENDPOINT = "oss-cn-beijing.aliyuncs.com"
  9. ACCESS_KEY = "LTAI5tBUvFtfWU4H3AikcmwF"
  10. ACCESS_SECRET = "W7Yceh0A0RODc6bELpcML1xHZOQ32q"
  11. BUCKET = "infish-oss"
  12. )
  13. func CreateClient() {
  14. _, err := oss.New(ENDPOINT, ACCESS_KEY, ACCESS_SECRET)
  15. if err != nil {
  16. panic(err)
  17. }
  18. return
  19. }
  20. type UpladImagesRes struct {
  21. Model string
  22. Shoes []string
  23. }
  24. func UpladImages(model string, shoes []string) (UpladImagesRes, error) {
  25. cli, err := oss.New(ENDPOINT, ACCESS_KEY, ACCESS_SECRET)
  26. if err != nil {
  27. log.Fatalf("Error: %v", err)
  28. return UpladImagesRes{}, err
  29. }
  30. // 获取存储空间
  31. bucket, err := cli.Bucket(BUCKET)
  32. if err != nil {
  33. return UpladImagesRes{}, err
  34. }
  35. res := UpladImagesRes{}
  36. localFileName := model
  37. objectName := fmt.Sprintf("tryon/%s", model)
  38. err = bucket.PutObjectFromFile(objectName, localFileName)
  39. if err != nil {
  40. return UpladImagesRes{}, err
  41. }
  42. res.Model = fmt.Sprintf("https://infish-oss.oss-cn-beijing.aliyuncs.com/%s", objectName)
  43. for _, shoe := range shoes {
  44. localFileName = shoe
  45. objectName = fmt.Sprintf("tryon/%s", shoe)
  46. err = bucket.PutObjectFromFile(objectName, localFileName)
  47. if err != nil {
  48. fmt.Println(err)
  49. continue
  50. }
  51. res.Shoes = append(res.Shoes, fmt.Sprintf("https://infish-oss.oss-cn-beijing.aliyuncs.com/%s", objectName))
  52. }
  53. return res, nil
  54. }