阿里云oss
This commit is contained in:
35
common_fun/file_func.go
Normal file
35
common_fun/file_func.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package cf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// EnsureDirExists 确保目录存在,如果不存在则创建
|
||||
func EnsureDirExists(dirPath string) error {
|
||||
// 判断路径是否存在
|
||||
_, err := os.Stat(dirPath)
|
||||
if os.IsNotExist(err) {
|
||||
// 目录不存在,递归创建
|
||||
err = os.MkdirAll(dirPath, os.ModePerm)
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建目录失败,目录:%s,err: %w", dirPath, err)
|
||||
}
|
||||
} else if err != nil {
|
||||
// 其他错误(如权限问题)
|
||||
return fmt.Errorf("检查目录失败,目录:%s,err: %w", dirPath, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnsureParentDirExists 确保文件的目录存在,如果不存在则创建
|
||||
func EnsureParentDirExists(filePath string) error {
|
||||
// 提取父级目录路径
|
||||
dir := filepath.Dir(filePath)
|
||||
err := EnsureDirExists(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user