oss.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package file_store
  2. import (
  3. "log"
  4. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  5. )
  6. type ALiYunOSS struct {
  7. Client interface{}
  8. BucketName string
  9. }
  10. //Setup 装载
  11. //endpoint sss
  12. func (e *ALiYunOSS) Setup(endpoint, accessKeyID, accessKeySecret, BucketName string, options ...ClientOption) error {
  13. client, err := oss.New(endpoint, accessKeyID, accessKeySecret)
  14. if err != nil {
  15. log.Println("Error:", err)
  16. return err
  17. }
  18. e.Client = client
  19. e.BucketName = BucketName
  20. return nil
  21. }
  22. // UpLoad 文件上传
  23. func (e *ALiYunOSS) UpLoad(yourObjectName string, localFile interface{}) error {
  24. // 获取存储空间。
  25. bucket, err := e.Client.(*oss.Client).Bucket(e.BucketName)
  26. if err != nil {
  27. log.Println("Error:", err)
  28. return err
  29. }
  30. // 设置分片大小为100 KB,指定分片上传并发数为3,并开启断点续传上传。
  31. // 其中<yourObjectName>与objectKey是同一概念,表示断点续传上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
  32. // "LocalFile"为filePath,100*1024为partSize。
  33. err = bucket.UploadFile(yourObjectName, localFile.(string), 100*1024, oss.Routines(3), oss.Checkpoint(true, ""))
  34. if err != nil {
  35. log.Println("Error:", err)
  36. return err
  37. }
  38. return nil
  39. }
  40. func (e *ALiYunOSS) GetTempToken() (string, error) {
  41. return "", nil
  42. }