【发布时间】:2024-05-01 14:00:01
【问题描述】:
作为用户,gsutil 运行良好。 从 crontab(用户)调用 gsutil 时效果很好。
作为 root,gsutil 说:
Caught non-retryable exception while listing gs://....: ServiceException: 401 Anonymous users does not have storage.objects.list access to bucket ...."
从 Anacron (root) 调用时,gsutil 不起作用。 从 Anacron 调用的其他脚本运行良好。
~/.boto 文件包含凭据,位于用户 HOME 目录中。 所以这可能是导致异常的原因。
我尝试设置 BOTO_CONFIG,但没有改变结果:
$ gsutil -D ls 2>&1 | grep config_file_list
config_file_list: ['/home/wolfv/.boto']
$ sudo gsutil -D ls 2>&1 | grep config_file_list
config_file_list: []
$ BOTO_CONFIG="/root/.boto"
$ sudo gsutil -D ls 2>&1 | grep config_file_list
config_file_list: []
如何设置 gsutil 以从 Anacron 运行?
$ gsutil -D
gsutil version: 4.22
checksum: 2434a37a663d09ae21d1644f64ce60ca (OK)
boto version: 2.42.0
python version: 2.7.13 (default, Jan 12 2017, 17:59:37) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)]
OS: Linux 4.9.11-200.fc25.x86_64
multiprocessing available: True
using cloud sdk: True
config path: /home/wolfv/.boto
gsutil path: /home/wolfv/Downloads/google-cloud-sdk/platform/gsutil/gsutil
compiled crcmod: True
installed via package manager: False
editable install: False
Command being run: /home/wolfv/Downloads/google-cloud-sdk/platform/gsutil/gsutil -o GSUtil:default_project_id=redacted -D
config_file_list: ['/home/wolfv/.config/gcloud/legacy_credentials/redacted/.boto', '/home/wolfv/.boto']
config: [('debug', '0'), ('working_dir', '/mnt/pyami'), ('https_validate_certificates', 'True'), ('debug', '0'), ('working_dir', '/mnt/pyami'), ('content_language', 'en'), ('default_api_version', '2'), ('default_project_id', 'redacted')]
UPDATE_1
export BOTO_CONFIG 为终端工作:
$ sudo -s
[root] # export BOTO_CONFIG=/home/wolfv/.boto
[root] # gsutil -D ls 2>&1 | grep config_file_list
config_file_list: ['/home/wolfv/.boto']
[root] # vi /root/.bashrc
add this line to end of .bashrc:
export BOTO_CONFIG=/home/wolfv/.boto
exit
open new terminal and test the new BOTO_CONFIG in bash.rc
$ sudo -s
[root] # gsutil -D ls 2>&1 | grep config_file_list
config_file_list: ['/home/wolfv/.boto']
exit
不幸的是,在 /root/.bashrc 中导出 BOTO_CONFIG 并没有帮助 Anacron 调用 gsutil。 备份日志显示 Anacron 调用了备份脚本,而备份脚本调用 gsutil 失败。
在哪个初始化脚本中设置路径 BOTO_CONFIG 是否重要? 要使 Anacron (root) 可以永久访问该路径,应在哪个文件中设置 BOTO_CONFIG?:
/etc/profile
/root/.bash_profile
/root/.bashrc
UPDATE_2
我的凭据现在无效,可能是由于我所做的一些更改。 这是我对 houglum 对 BOTO_CONFIG 的建议的尝试。
首先授权登录以解决这个问题:
$ gcloud auth login
Your browser has been opened to visit:
https://accounts.google.com/o/oauth2/auth?redirect_uri=http%3A%2F%2Flocalhost%3A8085%2F&prompt=select_account&response_type=code&client_id=redacted.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth&access_type=offline
Created new window in existing browser session.
WARNING: `gcloud auth login` no longer writes application default credentials.
If you need to use ADC, see:
gcloud auth application-default --help
You are now logged in as [edacted].
Your current project is [redacted]. You can change this setting by running:
$ gcloud config set project PROJECT_ID
内联定义 BOTO_CONFIG 不起作用:
$ BOTO_CONFIG=/home/wolfv/.boto gsutil ls
Your credentials are invalid. Please run
$ gcloud auth login
导出 BOTO_CONFIG 不起作用:
$ export BOTO_CONFIG=/home/wolfv/.boto; gsutil ls
Your credentials are invalid. Please run
$ gcloud auth login
采购 bashrc 不起作用:
$ ls /home/wolfv/.bashrc
/home/wolfv/.bashrc
$ . /home/wolfv/.bashrc; gsutil ls
Your credentials are invalid. Please run
$ gcloud auth login
UPDATE_3
如果我从 .boto 中删除我的凭据并改用 auth login(基于 Your credentials are invalid. Please run $ gcloud auth login),我的凭据就可以工作
$ gcloud auth login redacted@email.com
WARNING: `gcloud auth login` no longer writes application default credentials.
If you need to use ADC, see:
gcloud auth application-default --help
You are now logged in as [redacted@email.com].
Your current project is [redacted-123]. You can change this setting by running:
$ gcloud config set project PROJECT_ID
使用 auth login 后,gsutil 在终端工作:
$ gsutil ls
gs://redacted/
gs://redacted/
gs://redacted/
调用 gsutil 的备份脚本也可以从终端运行:
$ ~/scripts/backup_to_gcs/backup_to_gcs.sh
backup_to_gcs.sh in progress ...
backup_to_gcs.sh completed successfully
但是,从 crontab 调用时,backup_to_gcs.sh 会失败。
如何从 crontab 运行 gsutil?
UPDATE_4
这是在我的 anacron 文件中:
1 10 anacron_test_id BOTO_PATH=/home/wolfv/.config/gcloud/legacy_credentials/wolfvolpi@gmail.com/.boto:/home/wolfv/.boto /home/wolfv/scripts/backup_to_gcs/backup_to_gcs.sh
anacron 按预期运行 backup_to_gcs.sh 脚本,但备份失败。 从命令行调用 backup_to_gcs.sh 脚本时,它工作正常。
可能是因为 gsutil 以用户身份运行,但不以 root 身份运行:
$ gsutil ls
gs://wolfv/
gs://wolfv-test-log/
gs://wolfv2/
gs://wolfvtest/
$ BOTO_PATH=/home/wolfv/.config/gcloud/legacy_credentials/wolfvolpi@gmail.com/.boto:/home/wolfv/.boto gsutil ls
gs://wolfv/
gs://wolfv-test-log/
gs://wolfv2/
gs://wolfvtest/
$ sudo BOTO_PATH=/home/wolfv/.config/gcloud/legacy_credentials/wolfvolpi@gmail.com/.boto:/home/wolfv/.boto gsutil ls
sudo: gsutil: command not found
$ sudo gsutil ls
sudo: gsutil: command not found
两天前,root 能够运行 gsutil。 从那以后,我使用 dnf 历史回滚来卸载不同的软件。 这会影响 gsutil 身份验证吗?
UPDATE_5
我按照https://cloud.google.com/storage/docs/authentication#gsutilauth 上的说明进行操作 使用服务帐号
$ gcloud auth activate-service-account --key-file=/home/wolfv/REDACTED.json
已激活服务帐户凭据:[REDACTED@appspot.gserviceaccount.com]
但是,root 仍然无法运行 gsutil:
$ sudo gsutil ls
sudo: gsutil: command not found
$ gsutil ls -la gs://wolfvtest/test_lifecycle/
CommandException: You have multiple types of configured credentials (['Oauth 2.0 User Account', 'OAuth 2.0 Service Account']), which is not supported. One common way this happens is if you run gsutil config to create credentials and later run gcloud auth, and create a second set of credentials. Your boto config path is: ['/home/wolfv/.boto', '/home/wolfv/.config/gcloud/legacy_credentials/my-project@appspot.gserviceaccount.com/.boto']. For more help, see "gsutil help creds".
帮助引用的页面不再提及“auth”https://developers.google.com/cloud/sdk/gcloud/#gcloud.auth
所以我的凭据太多了:
$ gsutil -D
...
config_file_list: ['/home/wolfv/.boto', '/home/wolfv/.config/gcloud/legacy_credentials/my-project@appspot.gserviceaccount.com/.boto']
root 是否使用了这些凭据(对于 anacron)? 它们不在根目录中。 anacron 所需的凭据是否应该在根目录中?
UPDATE_5 我在How to authorize root to run gsutil?上安装Fedora 26后再次尝试
【问题讨论】:
标签: google-cloud-storage gsutil