windows - aws s3 命​​令在 Windows 任务计划程序触发的批处理文件中不起作用

标签 windows batch-file amazon-web-services amazon-s3

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

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

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

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

这是 Windows 2008 标准服务器。我已经安装了 AWS CLI 并使用命令“aws configure”进行了配置,并输入了我的访问 key 和 key 。这就是为什么我双击批处理文件时它会运行的原因。

Windows 任务计划程序以“[MyServer]/Administrator”帐户运行。

那么为什么“aws s3 sync”命令在任务调度程序触发时不起作用?

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

谢谢!

最佳答案

发生这种情况的原因是 aws 命令没有找到配置或凭证文件!它也发生在 Win2012 上。

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

所以解决方法是在你的脚本中添加类似这样的东西(BAT 或 PowerShell)

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

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

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

我在拖延试图弄清楚为什么会发生这种情况后才发现这一点。

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

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

这是我在没有告诉配置文件在哪里的情况下得到的错误:

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 变量后得到的错误:

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

关于windows - aws s3 命​​令在 Windows 任务计划程序触发的批处理文件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24015669/

相关文章:

linux - 如何将 Jenkins 作业从 Windows 本地计算机迁移到 Linux 服务器?

batch-file - 从多个目录复制具有特定扩展名的文件

java.io.Windows 文件系统?

java - 如何检查当前 Java 应用程序是否在我的 Windows PC 上的前台?

asp.net - 如何使用 Process Monitor 确定哪个 DLL 导致 BadImageFormatException

windows - 字符串匹配后输出行的批处理文件,文件名

windows - 我如何编写一个脚本来比较修改日期并查看哪个更新?

c# - 如何在没有 WSE 的情况下使用 SOAP 在 .NET 中签署 Amazon Web 服务请求

amazon-web-services - AWS Lambda S3 事件无限循环

amazon-web-services - 哪个服务正在执行 ebextensions