Modify: create folder if not exist while importing files
This commit is contained in:
parent
960c886496
commit
1afc750dae
2 changed files with 41 additions and 9 deletions
|
@ -18,6 +18,7 @@ import (
|
|||
"io"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -42,10 +43,6 @@ type Driver struct {
|
|||
HTTPClient request.Client
|
||||
}
|
||||
|
||||
func (handler Driver) List(ctx context.Context, path string, recursive bool) ([]response.Object, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
type key int
|
||||
|
||||
const (
|
||||
|
@ -103,6 +100,41 @@ func (handler *Driver) InitOSSClient() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (handler Driver) List(ctx context.Context, path string, recursive bool) ([]response.Object, error) {
|
||||
// 初始化客户端
|
||||
if err := handler.InitOSSClient(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 列取文件
|
||||
options := []oss.Option{
|
||||
oss.Prefix(strings.TrimPrefix(path, "/")),
|
||||
}
|
||||
if recursive {
|
||||
options = append(options, oss.Delimiter("/"))
|
||||
}
|
||||
listRes, err := handler.bucket.ListObjects(options...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 处理列取结果
|
||||
res := make([]response.Object, len(listRes.CommonPrefixes)+len(listRes.Objects))
|
||||
// 处理目录
|
||||
//for _,object := range listRes.CommonPrefixes{
|
||||
// res = append(res,response.Object{
|
||||
// Name: "",
|
||||
// RelativePath: "",
|
||||
// Source: "",
|
||||
// Size: 0,
|
||||
// IsDir: false,
|
||||
// LastModify: time.Time{},
|
||||
// })
|
||||
//}
|
||||
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Get 获取文件
|
||||
func (handler Driver) Get(ctx context.Context, path string) (response.RSCloser, error) {
|
||||
// 通过VersionID禁止缓存
|
||||
|
|
|
@ -146,13 +146,13 @@ func (job *ImportTask) Do() {
|
|||
if parent, ok := pathCache[virtualPath]; ok {
|
||||
parentFolder = parent
|
||||
} else {
|
||||
if exist, folder := fs.IsPathExist(virtualPath); exist {
|
||||
parentFolder = folder
|
||||
} else {
|
||||
util.Log().Warning("导入任务无法创插入文件[%s], 父目录不存在",
|
||||
object.RelativePath)
|
||||
folder, err := fs.CreateDirectory(context.Background(), virtualPath)
|
||||
if err != nil {
|
||||
util.Log().Warning("导入任务无法创建用户目录[%s], %s",
|
||||
virtualPath, err)
|
||||
continue
|
||||
}
|
||||
parentFolder = folder
|
||||
}
|
||||
|
||||
// 插入文件记录
|
||||
|
|
Loading…
Reference in a new issue