You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

minio.go 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Copyright 2021 The KubeEdge Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package storage
  14. import (
  15. "context"
  16. "fmt"
  17. "net/url"
  18. "strings"
  19. "time"
  20. "k8s.io/klog/v2"
  21. "github.com/kubeedge/sedna/pkg/localcontroller/util"
  22. "github.com/minio/minio-go/v7"
  23. "github.com/minio/minio-go/v7/pkg/credentials"
  24. )
  25. // MinioClient defines a minio client
  26. type MinioClient struct {
  27. Client *minio.Client
  28. }
  29. // MaxTimeOut is max deadline time of client working
  30. const MaxTimeOut = 100 * time.Second
  31. // createMinioClient creates client
  32. func createMinioClient(endpoint string, useHTTPS string, accessKeyID string, secretAccessKey string) (*MinioClient, error) {
  33. token := ""
  34. useSSL := true
  35. if useHTTPS == "0" {
  36. useSSL = false
  37. }
  38. client, err := minio.New(endpoint, &minio.Options{
  39. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, token),
  40. Secure: useSSL,
  41. })
  42. if err != nil {
  43. return nil, fmt.Errorf("initialize minio client failed, endpoint = %+v, error: %+v", endpoint, err)
  44. }
  45. c := MinioClient{
  46. Client: client,
  47. }
  48. return &c, nil
  49. }
  50. // uploadFile uploads file from local host to storage service
  51. func (mc *MinioClient) uploadFile(localPath string, objectURL string) error {
  52. bucket, absPath, err := mc.parseURL(objectURL)
  53. if err != nil {
  54. return err
  55. }
  56. if !util.IsExists(localPath) {
  57. return fmt.Errorf("file(%s) in the local host is not exists", localPath)
  58. }
  59. ctx, cancel := context.WithTimeout(context.Background(), MaxTimeOut)
  60. defer cancel()
  61. if _, err = mc.Client.FPutObject(ctx, bucket, absPath, localPath, minio.PutObjectOptions{}); err != nil {
  62. return fmt.Errorf("upload file from file path(%s) to file url(%s) failed, error: %+v", localPath, objectURL, err)
  63. }
  64. return nil
  65. }
  66. // copyFile copies file from the bucket to another bucket in storage service
  67. func (mc *MinioClient) copyFile(srcURL string, objectURL string) error {
  68. srcBucket, srcAbsPath, err := mc.parseURL(srcURL)
  69. if err != nil {
  70. return err
  71. }
  72. srcOptions := minio.CopySrcOptions{
  73. Bucket: srcBucket,
  74. Object: srcAbsPath,
  75. }
  76. objectBucket, objectAbsPath, err := mc.parseURL(objectURL)
  77. if err != nil {
  78. return err
  79. }
  80. objectOptions := minio.CopyDestOptions{
  81. Bucket: objectBucket,
  82. Object: objectAbsPath,
  83. }
  84. ctx, cancel := context.WithTimeout(context.Background(), MaxTimeOut)
  85. defer cancel()
  86. if _, err := mc.Client.CopyObject(ctx, objectOptions, srcOptions); err != nil {
  87. return fmt.Errorf("copy file from file url(%s) to file url(%s) failed, error: %+v", srcURL, objectURL, err)
  88. }
  89. return nil
  90. }
  91. // DownloadFile downloads file from storage service to the local host
  92. func (mc *MinioClient) downloadFile(objectURL string, localPath string) error {
  93. bucket, absPath, err := mc.parseURL(objectURL)
  94. if err != nil {
  95. return err
  96. }
  97. ctx, cancel := context.WithTimeout(context.Background(), MaxTimeOut)
  98. defer cancel()
  99. if err = mc.Client.FGetObject(ctx, bucket, absPath, localPath, minio.GetObjectOptions{}); err != nil {
  100. return fmt.Errorf("download file from file url(%s) to file path(%s) failed, error: %+v", objectURL, localPath, err)
  101. }
  102. return nil
  103. }
  104. // parseURL parses url
  105. func (mc *MinioClient) parseURL(URL string) (string, string, error) {
  106. u, err := url.Parse(URL)
  107. if err != nil {
  108. return "", "", fmt.Errorf("invalid url(%s)", URL)
  109. }
  110. scheme := u.Scheme
  111. switch scheme {
  112. case S3Prefix:
  113. return u.Host, strings.TrimPrefix(u.Path, "/"), nil
  114. default:
  115. klog.Errorf("invalid scheme(%s)", scheme)
  116. }
  117. return "", "", fmt.Errorf("invalid url(%s)", URL)
  118. }