-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #235 from dayuy/minio
feat: breakpoint upload via MinIO
- Loading branch information
Showing
8 changed files
with
835 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
Copyright 2023 KubeAGI. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package minio | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"sync" | ||
|
||
"github.com/minio/minio-go/v7" | ||
"github.com/minio/minio-go/v7/pkg/credentials" | ||
"k8s.io/klog/v2" | ||
|
||
client3 "github.com/kubeagi/arcadia/graphql-server/go-server/pkg/client" | ||
"github.com/kubeagi/arcadia/pkg/config" | ||
"github.com/kubeagi/arcadia/pkg/utils" | ||
) | ||
|
||
var minioClient *minio.Client = nil | ||
var coreClient *minio.Core = nil | ||
|
||
var mutex *sync.Mutex | ||
|
||
var ( | ||
minioAddress string | ||
minioAccessKeyID string | ||
minioSecretAccessKey string | ||
minioSecure bool | ||
minioBucket string | ||
minioBasePath string | ||
) | ||
|
||
func init() { | ||
mutex = new(sync.Mutex) | ||
|
||
if err := utils.SetSelfNamespace(); err != nil { | ||
klog.Errorf("unable to get self namespace: %s", err) | ||
os.Exit(1) | ||
} | ||
|
||
c, err := client3.GetClient(nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
minioConfig, err := config.GetMinIO(context.TODO(), c) | ||
if err != nil { | ||
panic(err) | ||
} | ||
minioAddress = minioConfig.MinioAddress | ||
minioAccessKeyID = minioConfig.MinioAccessKeyID | ||
minioSecretAccessKey = minioConfig.MinioSecretAccessKey | ||
minioBucket = minioConfig.MinioBucket | ||
minioBasePath = minioConfig.MinioBasePath | ||
minioSecure = minioConfig.MinioSecure | ||
} | ||
|
||
func getClients() (*minio.Client, *minio.Core, error) { | ||
var client1 *minio.Client | ||
var client2 *minio.Core | ||
var err error | ||
|
||
mutex.Lock() | ||
|
||
if minioClient != nil && coreClient != nil { | ||
client1 = minioClient | ||
client2 = coreClient | ||
mutex.Unlock() | ||
return client1, client2, nil | ||
} | ||
|
||
aliasedURL := minioAddress | ||
accessKeyID := minioAccessKeyID | ||
secretAccessKey := minioSecretAccessKey | ||
secure := minioSecure | ||
|
||
if minioClient == nil { | ||
minioClient, err = minio.New(aliasedURL, &minio.Options{ | ||
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), | ||
Secure: secure, | ||
}) | ||
} | ||
if err != nil { | ||
mutex.Unlock() | ||
return nil, nil, err | ||
} | ||
|
||
client1 = minioClient | ||
|
||
if coreClient == nil { | ||
coreClient, err = minio.NewCore(aliasedURL, &minio.Options{ | ||
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), | ||
Secure: secure, | ||
}) | ||
} | ||
|
||
if err != nil { | ||
mutex.Unlock() | ||
return nil, nil, err | ||
} | ||
|
||
client2 = coreClient | ||
|
||
mutex.Unlock() | ||
|
||
return client1, client2, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
Copyright 2023 KubeAGI. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package models | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
const ( | ||
FileNotUploaded int = iota | ||
FileUploaded | ||
) | ||
|
||
// maxPartsCount - maximum number of parts for a single multipart session. | ||
const MaxPartsCount = 10000 | ||
|
||
// maxMultipartPutObjectSize - maximum size 5TiB of object for | ||
// Multipart operation. | ||
const MaxMultipartPutObjectSize = 1024 * 1024 * 1024 * 1024 * 5 | ||
|
||
// minPartSize - minimum part size 128MiB per object after which | ||
// putObject behaves internally as multipart. | ||
const MinPartSize = 1024 * 1024 * 64 | ||
|
||
type FileChunk struct { | ||
UUID string | ||
Md5 string | ||
IsUploaded int | ||
UploadID string | ||
TotalChunks int | ||
Size int64 | ||
FileName string | ||
CompletedParts string | ||
} | ||
|
||
var fileChunks = make([]*FileChunk, 0, 250) | ||
|
||
func GetFileChunkByMD5(md5 string) (*FileChunk, error) { | ||
fileChunk := new(FileChunk) | ||
matched := false | ||
for _, chunk := range fileChunks { | ||
if chunk.Md5 == md5 { | ||
fileChunk = chunk | ||
matched = true | ||
} | ||
} | ||
if !matched { | ||
return nil, errors.New("GetFileChunksByUUID failed") | ||
} | ||
return fileChunk, nil | ||
} | ||
|
||
func GetFileChunkByUUID(uuid string) (*FileChunk, error) { | ||
fileChunk := new(FileChunk) | ||
matched := false | ||
for _, chunk := range fileChunks { | ||
if chunk.UUID == uuid { | ||
fileChunk = chunk | ||
matched = true | ||
break | ||
} | ||
} | ||
if !matched { | ||
return nil, errors.New("GetFileChunksByUUID failed") | ||
} | ||
return fileChunk, nil | ||
} | ||
|
||
func UpdateFileChunk(fileChunk *FileChunk) error { | ||
updated := false | ||
for _, chunk := range fileChunks { | ||
if chunk.UUID == fileChunk.UUID && chunk.Md5 == fileChunk.Md5 { | ||
chunk.IsUploaded = fileChunk.IsUploaded | ||
chunk.CompletedParts = fileChunk.CompletedParts | ||
updated = true | ||
} | ||
} | ||
if !updated { | ||
return errors.New("UpdateFileChunk failed") | ||
} | ||
return nil | ||
} | ||
|
||
func InsetFileChunk(fileChunk *FileChunk) (_ *FileChunk, err error) { | ||
fileChunks = append(fileChunks, fileChunk) | ||
return fileChunk, nil | ||
} |
Oops, something went wrong.