qiniu / python-sdk

Qiniu Resource (Cloud) Storage SDK for Python
MIT License
546 stars 259 forks source link

Ubuntu下使用systemd出现qiniu_pythonsdk_hostscache权限问题 #433

Closed xunjieliu closed 1 year ago

xunjieliu commented 1 year ago

环境

如何重现

测试代码:

import time
import uuid

from qiniu import put_file
from qiniu.http import ResponseInfo

def if_token_valid(qiniu_token: str) -> bool:
    """
    判断七牛token是否有效
    :param qiniu_token: 七牛token
    :return: 是否有效
    """
    info: ResponseInfo
    try:
        ret, info = put_file(up_token=qiniu_token, key=str(uuid.uuid4()), file_path=__file__, version='v2')
    except Exception:
        return False

    return (ret is not None) or (info.status_code == 401)

if __name__ == "__main__":
    token = "some-valid-token"
    while True:
        result = if_token_valid(token)

        print(f"Qiniu token是否有效:{result}")
        time.sleep(10)

/etc/systemd/system/qiniu-cache-test.service:

[Unit]
Description=Qiniu cache test
After=network.target

[Service]
Type=simple
User=user
Group=user
Environment=PYTHONPATH=/path/to/your/project
Restart=on-failure
RestartSec=5s
ExecStart=/path/to/your/project/venv/bin/python /path/to/your/project/cache_test.py
ExecReload=/path/to/your/project/venv/bin/python /path/to/your/project/cache_test.py

[Install]
WantedBy=multi-user.target

启动测试:

sudo systemctl start qiniu-cache-test.service

报错内容

4月 23 16:25:40 your-hostname systemd[1]: Stopped Qiniu cache test.
4月 23 16:25:40 your-hostname systemd[1]: Started Qiniu cache test.
4月 23 16:25:40 your-hostname python[27930]: Traceback (most recent call last):
4月 23 16:25:40 your-hostname python[27930]:   File "/path/to/your/project/cache_test.py", line 29, in <module>
4月 23 16:25:40 your-hostname python[27930]:     result = if_token_valid(token)
4月 23 16:25:40 your-hostname python[27930]:   File "/path/to/your/project/cache_test.py", line 22, in if_token_valid
4月 23 16:25:40 your-hostname python[27930]:     ret, info = put_file(up_token=qiniu_token, key=str(uuid.uuid4()), file_path=__file__, version='v2')
4月 23 16:25:40 your-hostname python[27930]:   File "/path/to/your/project/qiniu/services/storage/uploader.py", line 87, in put_file
4月 23 16:25:40 your-hostname python[27930]:     ret, info = _form_put(up_token, key, input_stream, params, mime_type,
4月 23 16:25:40 your-hostname python[27930]:   File "/path/to/your/project/qiniu/services/storage/uploader.py", line 108, in _form_put
4月 23 16:25:40 your-hostname python[27930]:     url = config.get_default('default_zone').get_up_host_by_token(up_token, hostscache_dir)
4月 23 16:25:40 your-hostname python[27930]:   File "/path/to/your/project/qiniu/region.py", line 43, in get_up_host_by_token
4月 23 16:25:40 your-hostname python[27930]:     up_hosts = self.get_up_host(ak, bucket, home_dir)
4月 23 16:25:40 your-hostname python[27930]:   File "/path/to/your/project/qiniu/region.py", line 111, in get_up_host
4月 23 16:25:40 your-hostname python[27930]:     bucket_hosts = self.get_bucket_hosts(ak, bucket, home_dir)
4月 23 16:25:40 your-hostname python[27930]:   File "/path/to/your/project/qiniu/region.py", line 176, in get_bucket_hosts
4月 23 16:25:40 your-hostname python[27930]:     self.set_bucket_hosts_to_cache(key, bucket_hosts, home_dir)
4月 23 16:25:40 your-hostname python[27930]:   File "/path/to/your/project/qiniu/region.py", line 194, in set_bucket_hosts_to_cache
4月 23 16:25:40 your-hostname python[27930]:     self.host_cache_to_file(home_dir)
4月 23 16:25:40 your-hostname python[27930]:   File "/path/to/your/project/qiniu/region.py", line 217, in host_cache_to_file
4月 23 16:25:40 your-hostname python[27930]:     with open(path, 'w') as f:
4月 23 16:25:40 your-hostname python[27930]: PermissionError: [Errno 13] Permission denied: '/.qiniu_pythonsdk_hostscache.json'
4月 23 16:25:40 your-hostname systemd[1]: qiniu-cache-test.service: Main process exited, code=exited, status=1/FAILURE
4月 23 16:25:40 your-hostname systemd[1]: qiniu-cache-test.service: Failed with result 'exit-code'.

源码中出错代码位于qiniu/region.py:

    def host_cache_file_path(self):
        return os.path.join(self.home_dir, ".qiniu_pythonsdk_hostscache.json")

    def host_cache_to_file(self, home_dir):
        path = self.host_cache_file_path()
        with open(path, 'w') as f:
            compat.json.dump(self.host_cache, f)
        f.close()
xunjieliu commented 1 year ago

应该可以通过设置put_filehostscache_dir参数解决