自动化gsutil命令 [英] Automating gsutil commands

查看:444
本文介绍了自动化gsutil命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使一些gsutils命令自动化,但是努力查看认证文件的存放位置以及如何重复使用(如果发生了什么情况).

I'm trying to automate some gsutils commands, but struggling to see where the authentication files are kept and how to re-use (if thats what happens).

我已经在bash中完成了gcloud初始化过程...

I've gone through the gcloud init process in bash...

curl https://sdk.cloud.google.com | bash
gcloud init

我运行时一切正常

  'gsutil ls'

现在我正在尝试使该过程自动化,因此这将在添加到crontab的新服务器上运行(而不是每次都创建一个新配置).

Now I'm trying to automate the process, so this would work on a new server adding into a crontab on it (rather than creating a new config each time).

我看到提到要设置环境变量GOOGLE_APPLICATION_CREDENTIALS,因此我将我的凭据从网络登录名复制到了文件中并尝试过,例如尝试以其他用户身份进行测试

I saw a mention of setting env variable GOOGLE_APPLICATION_CREDENTIALS, so I copied my credentials from web login to a file and tried it, eg trying as a different user to test

export GOOGLE_APPLICATION_CREDENTIALS=/home/user/.gsutil/mycreds

然后是gsutil ls,但失败.

and then gsutil ls, but fails.

所以我认为我对整个凭据的理解有点不对.我假设某个地方有一个文件最初是由gcloud创建的,可以使用,但是我在任何地方都看不到它?

So I assume I've got the whole credentials thing a bit wrong. I'm assuming there is a file somewhere that was originally created by gcloud which I could use, but I can't see it anywhere ?

我已经在此处查看了答案,但是没有根据最近的评论,现在看来是最新的.

I've looked at the answer here but doesn't seem up to date now, as per last comment.

编辑:我已按照Zacharys的步骤进行操作,即gcloud auth activate-service-account --key-file = myfilelocation

Edit: I have followed Zacharys steps, gcloud auth activate-service-account --key-file=myfilelocation

但是,现在有了'gsutil ls'.

However, with 'gsutil ls' I now get..

You are attempting to perform an operation that requires a project id, with none configured. Please re-run gsutil config and make sure to follow the instructions for finding and entering your default project id.

所以我的下一个问题是,它在哪里寻找项目ID?如果我运行gsutil config,似乎会创建一组新的auth,然后创建另一个错误,因此请删除该错误.

So my next question would be, where is it looking for the project id ? If I run gsutil config, it seems to create a new set of auth which then creates another error, so have removed that.

推荐答案

您应该能够做到这一点,而不必深入研究gsutil的身份验证的实现.

You should be able to do this without diving in too deep to the implementation of authentication for gsutil.

如果您使用的是独立gsutil(如果通过此方法安装了 ),链接的问题中的说明仍然有效(正如Travis指出的那样.)

If you're using standalone gsutil (if you installed via this method), the instructions in the linked question are still valid (as Travis points out).

如果您想继续使用通过Cloud SDK提供的gsutil,则应使用服务帐户.服务帐户是在无头计算机或非交互式上下文中进行身份验证的首选方法.

If you'd like to continue using the gsutil supplied via the Cloud SDK, you should use service accounts. Service accounts are the preferred method of authenticating on headless machines or in non-interactive contexts.

您的流程如下所示:

  1. 通过 Google Cloud开发者控制台创建服务帐户.
  2. 在远程计算机上,安装Cloud SDK和gsutil.如果您不是交互式安装,最好跳过curl ... | bash方法.而是下载此安装档案文件,解压缩它,然后运行install.sh脚本.该脚本具有选项(在--help中可见);如果您为所有这些选项指定了选项,则不会提示您.
  3. 将服务帐户复制到远程计算机.运行gcloud auth activate-service-account --key-file=/path/to/service-account.json.
  4. 运行gsutil.您应该经过适当的身份验证.
  1. Create a service account via the Google Cloud Developers Console.
  2. On the remote machine, install the Cloud SDK and gsutil. If you're not installing interactively, it's better to skip the curl ... | bash method. Instead, download this install archive, extract it, and run the install.sh script. This script has options (visible with --help); if you specify choices to all of these options, it won't prompt you.
  3. Copy the service account to the remote machine. Run gcloud auth activate-service-account --key-file=/path/to/service-account.json.
  4. Run gsutil. You should be appropriately authenticated.

这篇关于自动化gsutil命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆