55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/objectsync"
|
|
"fmt"
|
|
"github.com/baidubce/bce-sdk-go/services/bos"
|
|
bosapi "github.com/baidubce/bce-sdk-go/services/bos/api"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
func (s *server) localAnnotationSource(ctx context.Context, org, source, target string) (string, error) {
|
|
c, bucket, key, e := s.annotationWorkspaceObjectAccess(ctx, org, source, false)
|
|
if e != nil {
|
|
return "", e
|
|
}
|
|
if c == nil {
|
|
return source, nil
|
|
}
|
|
p := objectsync.BOSPublisher{GlobalEndpoint: s.globalBOSEndpoint}
|
|
meta, e := p.Head(ctx, *c, bucket, key)
|
|
if e != nil {
|
|
return "", e
|
|
}
|
|
if !meta.Exists {
|
|
return "", fmt.Errorf("source object missing")
|
|
}
|
|
client, e := bos.NewClient(c.AccessKeyID, c.SecretAccessKey, s.chooseBOSEndpoint(c.Endpoint))
|
|
if e != nil {
|
|
return "", e
|
|
}
|
|
response, e := client.GetObjectWithArgs(ctx, bucket, key, &bosapi.GetObjectArgs{IfMatch: meta.ETag})
|
|
if e != nil {
|
|
return "", e
|
|
}
|
|
defer response.Body.Close()
|
|
file, e := os.Create(target)
|
|
if e != nil {
|
|
return "", e
|
|
}
|
|
size, e := io.CopyBuffer(file, contextFileReader{ctx, response.Body}, make([]byte, 1<<20))
|
|
closeErr := file.Close()
|
|
if e != nil {
|
|
return "", e
|
|
}
|
|
if closeErr != nil {
|
|
return "", closeErr
|
|
}
|
|
if size != meta.SizeBytes {
|
|
return "", fmt.Errorf("source download size mismatch")
|
|
}
|
|
return target, nil
|
|
}
|