1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| func JdCloudUpload(file *multipart.FileHeader, filename string) string { f, _ := file.Open() defer f.Close() client := newClient() client.PutObject(&s3.PutObjectInput{ Body: f, Bucket: aws.String(viper.GetString("jdsdk.bucket")), Key: aws.String(filename), }) url := viper.GetString("jdsdk.domain") + filename return url }
func newClient() *s3.S3 { ak := viper.GetString("jdsdk.ak") sk := viper.GetString("jdsdk.sk") creds := credentials.NewStaticCredentials(ak, sk, "") _, err := creds.Get() if err != nil { fmt.Printf("err: %v\n", err) } config := &aws.Config{ Region: aws.String(viper.GetString("jdsdk.region")), Endpoint: aws.String(viper.GetString("jdsdk.endpoint")), DisableSSL: aws.Bool(false), Credentials: creds, } s, _ := session.NewSession(config) return s3.New(s) }
func UploadJdCloud(c *gin.Context) { f, err := c.FormFile("file") if err != nil { c.JSON(200, gin.H{ "code": -1, "msg": "file不存在", "err": err.Error(), }) } else { Path := "./upload/" date := time.Now().Format("20060102") pathTmp := path.Join(Path, date) filename := strconv.FormatInt(time.Now().Unix(), 10) + strconv.Itoa(rand.Intn(999999-100000)+100000) + path.Ext(f.Filename) filepath := path.Join(pathTmp, filename) s := sdk.JdCloudUpload(f, filepath) c.JSON(200, gin.H{ "code": 200, "msg": "ok", "url": s, }) } }
|