当前位置:网站首页>Golang创建文件权限问题解决方法
Golang创建文件权限问题解决方法
2022-07-16 22:39:00 【Crisp_LF】
Golang创建文件权限问题解决方法
前言:
最近在写一个关于excel的功能,其中有一个需求就是将每天上传的excel表格归纳到当天的文件夹中,这需要时间判断然后每天创建对应的文件夹,我在本地实现这个功能之后就上传了我的代码,但是线上却出了问题,每次导入文件之后,没有生成对应的文件夹。在排查了配置,代码等一系列问题之后,发现问题出现在创建权限上。
1.分析
在本地运行代码,文件夹不会牵扯到linux系统权限问题,所以我们的程序可以正常的运行,但是当我们将自己程序跑在服务器上就不一样了,这里会牵扯到linux文件权限。
原因是:需要打开一个文件夹,那么必须要有当前文件夹的执行权限
**x**,Linux 下设计一切皆文件,文件夹也是一个特殊的文件如果使用
**os.MkdirAll**方法创建文件夹时,必须基于文件夹所有者x执行权限以及w写权限,为什么需要写权限,可以通过vim去打开一个文件夹,可以看到文件夹里的信息(记得吗,文件夹也是文件),比如我一个文件夹通过 vim 打开的信息:
" ============================================================================
" Netrw Directory Listing (netrw v155)
" /home/g10guang/Templates/blog
" Sorted by name
" Sort sequence: [\/]$,\<core\%(\.\d\+\)\=\>,\.h$,\.c$,\.cpp$,\~\=\*$,*,\.o$,\.obj$,\.info$,\.swp$,\.ba
" Quick Help: <F1>:help -:go up dir D:delete R:rename s:sort-by x:special
" ==============================================================================
../
./
.git/
.sass-cache/
_posts/
_site/
g10guang.github.io/
.gitignore
.gitmodules
404.html
Gemfile
Gemfile.lock
_config.yml
about.md
index.md
- 文件夹中记录着里面有哪些文件以及文件夹,其中
**xxx/**有**/**结尾的是文件夹,其他的是文件,所以在文件夹中创建一个文件夹需要改变文件夹信息,需要有写文件夹的权限
2.创建指定权限文件方法
1.方法一:
改变 **umask** 后再创建文件,其后再把 **umask** 改为原来的 umask
import (
"os"
"fmt"
"syscall"
)
func main() {
mask := syscall.Umask(0) // 改为 0000 八进制
defer syscall.Umask(mask) // 改为原来的 umask
err := os.MkdirAll("test/test1/test2", 0766)
if err != nil {
fmt.Println(err)
}
}
2.方法二
先创建文件,然后再改变文件的权限
//创建文件并给予权限
err := os.MkdirAll("uploads/import_excel/", 0777)
if err != nil {
fmt.Println("创建目录失败")
err := os.Chmod("uploads/", 0777)
if err != nil {
fmt.Println("改变uploads权限失败")
}
err = os.Chmod("uploads/import_excel/", 0777)
if err != nil {
fmt.Println("改变import_excel权限失败")
}
}
golang 还不支持递归更改多个文件夹的权限,所有需要一个一个调用。
边栏推荐
- MySQL's redolog and binlog
- Electron如何打包使其支持龙芯和方腾、兆芯平台的麒麟系统
- 没有老板的乐视员工过上“神仙日子”,靠什么?
- Redis distributed cache - redis master-slave
- The relationship between filedescriptor and open file in liunx
- Installation and usage of yarn tool in nodejs compilation environment
- 20220707 线程学习 继承
- php通过form表单上传Excel文件并把Excel数据导入到数据库中
- 自定义序列化协议,这种方式的灵活程度比较高,但是不够通用
- 2022 latest Chinese Camtasia studio computer recording screen tool
猜你喜欢

2022最新汉化camtasia studio电脑录制屏幕工具

CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes)(A-C)

With responsible AI | three certificates, aopeng China has also obtained ISO9001 and 27701 certification

Delphi 485 Modbus RTU reading and writing RFID example source code

MySQL one basic syntax

Visionmaster communicates MODBUS with youao robot ur5e

微信小程序開發學習3(WXSS模板和全局配置)

Easy operation of cloud platform

QT tools - MOC, RCC, UIC

Kept - solve the problem that the primary and standby nodes seize the VIP at the same time
随机推荐
数据库的主从分离
MySQL---ONE 基础语法
WordPress主题分享:Flatsome主题v3.15.7免费下载 2022年最新版
2022-07-16 第五小組 修身課 學習筆記(every day)
Implementation of word2vec skip gram model
ES6之箭头函数
KubeSphere 部署
bufferbloat 与通货膨胀
php通过form表单上传Excel文件并把Excel数据导入到数据库中
Configuring VIM (4) from scratch -- some skills of keyboard mapping
Explanation of the taste of snow vegetables -- searching for Literature
Envoy lifecycle management
2022-07-15 网工进阶(十九)BGP-状态机、对等体之间的交互原则、影响对等体关系建立的因素、对等体表、路由表、详细路由表、路由属性
Special instructions for PLC part of visionmaster multi process operation triggered by external PLC
fiddler抓不到PC端微信小程序的包
Interviewer: after working for two years, you can't solve such a simple algorithm problem?
Business is too busy. Is there really no reason to have time for automation?
25篇性能测试文章专题
MySQL's redolog and binlog
Beautiful Soup模块是什么