aws s3命令在Windows任务计划程序触发的批处理文件中不起作用 [英] aws s3 command does not work in a batch file triggered by a Windows task scheduler

查看:149
本文介绍了aws s3命令在Windows任务计划程序触发的批处理文件中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件C:\ upload_to_s3.bat.在此文件中,有一行:

I have a batch file C:\upload_to_s3.bat. In this file, there is a line:

aws s3 sync D:\ S3 \ batch1 \ s3://MyBucket/batch1-排除* .bat

aws s3 sync D:\S3\batch1\ s3://MyBucket/batch1 --exclude *.bat

我有Windows任务计划程序"S3 Hourly Sync",每小时运行一次,以触发运行C:\ upload_to_s3.bat.但是此命令不会执行任何操作-文件上传从未发生.

I have Windows task scheduler "S3 Hourly Sync" that runs every hour to trigger to run C:\upload_to_s3.bat. But this command does not do anything - the file upload never happened.

如果我双击C:\ upload_to_s3.bat,它将运行完美.

It runs perfectly if I double click on C:\upload_to_s3.bat.

这是Windows 2008 Standard服务器.我已经安装了AWS CLI,并使用命令"aws configure"进行了配置,然后输入了访问密钥和秘密密钥.这就是为什么如果我双击批处理文件,它就会运行的原因.

This is Windows 2008 Standard server. I have installed AWS CLI and configured with the command "aws configure", and entered my access key and secret key. That is why it runs if I double click the batch file.

Windows任务计划程序以"[MyServer]/管理员"帐户运行.

The Windows Task Scheduler is run as "[MyServer]/Administrator" account.

那么当任务计划程序触发"aws s3 sync"命令时为什么不起作用?

So why doesn't the "aws s3 sync" command work when triggered by the task scheduler?

我是否需要在"aws s3 sync"之前添加一行以首先设置凭据?如果是这样,怎么办?

Do I need to add a line before "aws s3 sync" to set credentials first? If so, how?

谢谢!

推荐答案

之所以发生这种情况,是因为 aws 命令找不到配置文件或凭据文件! Win2012也是如此.

The reason this is happening, is because aws command is not finding the the config or the credentials file! And it happens for Win2012 too.

问题是Windows任务计划将%USERPROFILE%设置为C:\ Users \ Default而不是任务在其下运行的实际用户! ??? AWS正在使用它来查找配置和凭证文件.

The problem is that Windows Task Schedule sets the %USERPROFILE% to C:\Users\Default instead of the actual user the task is running under!! ??? And AWS is using that to find the config and credentials files.

因此解决方法是在脚本(BAT或PowerShell)中放置类似的内容

So the fix is to put something like this in your script (either BAT or PowerShell)

:: in a BAT script
set userprofile=C:\Users\%username%

# in a PowerShell
$env:USERPROFILE = "c:\users\$env:USERNAME"

从计划任务运行时,即使%homepath%变量也为空.

Even the %homepath% variable is blank when running from a Scheduled Task.

我是在拖延试图弄清楚为什么发生这种情况之后才发现的.

I just discovered this after procrastinating trying to figure out why this was happening for us.

set AWS_DEFAULT_PROFILE=namedprofile
set AWS_CONFIG_FILE=C:\Users\xxxx\.aws\config

直到我在脚本中添加了上面的AWS_CONFIG_FILE行之后,AWS才能够真正看到配置文件!但是,它现在找不到凭据,而且我在任何地方都看不到如何设置它.

Only until I added the AWS_CONFIG_FILE line above in my script was AWS able to actually see the config file! However it can not find the credentials now, and I don't see anywhere how to set that.

这是我不知道配置文件在哪里的错误:

This is the error I get without telling where the config file is:

Traceback (most recent call last):
  File "aws", line 27, in <module>
  File "aws", line 23, in main
  File "awscli\clidriver.pyc", line 50, in main
  File "awscli\clidriver.pyc", line 176, in main
  File "awscli\clidriver.pyc", line 157, in _create_parser
  File "awscli\clidriver.pyc", line 91, in _get_command_table
  File "awscli\clidriver.pyc", line 111, in _build_command_table
  File "botocore\session.pyc", line 680, in emit
  File "botocore\hooks.pyc", line 226, in emit
  File "botocore\hooks.pyc", line 209, in _emit
  File "awscli\customizations\preview.pyc", line 71, in mark_as_preview
  File "awscli\clidriver.pyc", line 349, in service_model
  File "awscli\clidriver.pyc", line 366, in _get_service_model
  File "botocore\session.pyc", line 256, in get_config_variable
  File "botocore\session.pyc", line 277, in _found_in_config_file
  File "botocore\session.pyc", line 349, in get_scoped_config
botocore.exceptions.ProfileNotFound: The config profile (backup) could not be found

然后这是设置 AWS_CONFIG_FILE 变量后出现的错误:

And then this is the error I get after setting the AWS_CONFIG_FILE variable:

Unable to locate credentials
Completed 1 part(s) with ... file(s) remaining

这篇关于aws s3命令在Windows任务计划程序触发的批处理文件中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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