mirror of
https://github.com/nabbar/golib.git
synced 2025-09-26 20:01:15 +08:00

- rework MultipartUpload process & helper - update test to use lib size - update object multipart to use new helper Package IO Utils : - add truncate & sync to FileProgress - fix error on open file mode for FileProgress Package Console : - fix interface used for color buffer Package Cobra : - add function to print message on write config to use custom message instead of internal message. If the function is not set, the default message will be print. Other: - fix golangci-lint config to remove crazy linter (use only golang group compliance linter) - bump dependencies
118 lines
3.6 KiB
Go
118 lines
3.6 KiB
Go
/*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2020 Nicolas JUHEL
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
*/
|
|
|
|
package object
|
|
|
|
import (
|
|
"io"
|
|
|
|
sdkaws "github.com/aws/aws-sdk-go-v2/aws"
|
|
sdksss "github.com/aws/aws-sdk-go-v2/service/s3"
|
|
sdktyp "github.com/aws/aws-sdk-go-v2/service/s3/types"
|
|
libhlp "github.com/nabbar/golib/aws/helper"
|
|
libmpu "github.com/nabbar/golib/aws/multipart"
|
|
liberr "github.com/nabbar/golib/errors"
|
|
libsiz "github.com/nabbar/golib/size"
|
|
)
|
|
|
|
// MultipartList implement the ListMultipartUploads.
|
|
// See docs for more infos : https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListMultipartUploads.html
|
|
func (cli *client) MultipartList(keyMarker, markerId string) (uploads []sdktyp.MultipartUpload, nextKeyMarker string, nextIdMarker string, count int64, e liberr.Error) {
|
|
in := &sdksss.ListMultipartUploadsInput{
|
|
Bucket: sdkaws.String(cli.GetBucketName()),
|
|
MaxUploads: 1000,
|
|
}
|
|
|
|
if keyMarker != "" && markerId != "" {
|
|
in.KeyMarker = sdkaws.String(keyMarker)
|
|
in.UploadIdMarker = sdkaws.String(markerId)
|
|
}
|
|
|
|
out, err := cli.s3.ListMultipartUploads(cli.GetContext(), in)
|
|
|
|
if err != nil {
|
|
return nil, "", "", 0, cli.GetError(err)
|
|
} else if out.IsTruncated {
|
|
return out.Uploads, *out.NextKeyMarker, *out.NextUploadIdMarker, int64(out.MaxUploads), nil
|
|
} else {
|
|
return out.Uploads, "", "", int64(out.MaxUploads), nil
|
|
}
|
|
}
|
|
|
|
func (cli *client) MultipartNew(partSize libsiz.Size, object string) libmpu.MultiPart {
|
|
m := libmpu.New(partSize, object, cli.GetBucketName())
|
|
m.RegisterContext(cli.GetContext)
|
|
m.RegisterClientS3(func() *sdksss.Client {
|
|
return cli.s3
|
|
})
|
|
|
|
return m
|
|
}
|
|
|
|
func (cli *client) MultipartPut(object string, body io.Reader) liberr.Error {
|
|
return cli.MultipartPutCustom(libmpu.DefaultPartSize, object, body)
|
|
}
|
|
|
|
func (cli *client) MultipartPutCustom(partSize libsiz.Size, object string, body io.Reader) liberr.Error {
|
|
var (
|
|
e error
|
|
m = cli.MultipartNew(partSize, object)
|
|
)
|
|
|
|
defer func() {
|
|
if m != nil {
|
|
_ = m.Close()
|
|
}
|
|
}()
|
|
|
|
if e = m.StartMPU(); e != nil {
|
|
return cli.GetError(e)
|
|
} else if _, e = io.Copy(m, body); e != nil {
|
|
return cli.GetError(e)
|
|
} else if e = m.StopMPU(false); e != nil {
|
|
return cli.GetError(e)
|
|
} else {
|
|
m = nil
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (cli *client) MultipartCancel(uploadId, key string) liberr.Error {
|
|
res, err := cli.s3.AbortMultipartUpload(cli.GetContext(), &sdksss.AbortMultipartUploadInput{
|
|
Bucket: sdkaws.String(cli.GetBucketName()),
|
|
UploadId: sdkaws.String(uploadId),
|
|
Key: sdkaws.String(key),
|
|
})
|
|
|
|
if err != nil {
|
|
return cli.GetError(err)
|
|
} else if res == nil {
|
|
return libhlp.ErrorResponse.Error(nil)
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|