obs.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package file_store
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
  6. )
  7. type HuaWeiOBS struct {
  8. Client interface{}
  9. BucketName string
  10. }
  11. func (e *HuaWeiOBS) Setup(endpoint, accessKeyID, accessKeySecret, BucketName string, options ...ClientOption) error {
  12. // 创建ObsClient结构体
  13. client, err := obs.New(accessKeyID, accessKeySecret, endpoint)
  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. // yourObjectName 文件路径名称,与objectKey是同一概念,表示断点续传上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg
  24. func (e *HuaWeiOBS) UpLoad(yourObjectName string, localFile interface{}) error {
  25. // 获取存储空间。
  26. input := &obs.PutFileInput{}
  27. input.Bucket = e.BucketName
  28. input.Key = yourObjectName
  29. input.SourceFile = localFile.(string)
  30. output, err := e.Client.(*obs.ObsClient).PutFile(input)
  31. if err == nil {
  32. fmt.Printf("RequestId:%s\n", output.RequestId)
  33. fmt.Printf("ETag:%s, StorageClass:%s\n", output.ETag, output.StorageClass)
  34. } else {
  35. if obsError, ok := err.(obs.ObsError); ok {
  36. fmt.Println(obsError.Code)
  37. fmt.Println(obsError.Message)
  38. } else {
  39. fmt.Println(err)
  40. }
  41. }
  42. return nil
  43. }
  44. func (e *HuaWeiOBS) GetTempToken() (string, error) {
  45. return "", nil
  46. }