Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 186 Vote(s) - 3.65 Average
  • 1
  • 2
  • 3
  • 4
  • 5
aws s3 command does not work in a batch file triggered by a Windows task scheduler

#1
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 --exclude *.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.

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

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.

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

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

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

Thank you!

Reply

#2
Include the following lines in your batch file:

`set AWS_ACCESS_KEY_ID=your_access_key_id`

`set AWS_SECRET_ACCESS_KEY=your_secret_access_key`

[AWS Documentation](

[To see links please register here]

)
Reply

#3
i was able to solve the problem by adding the following 3 lines in the batch file BEFORE your s3 command


aws configure set AWS_ACCESS_KEY_ID <your acess key id>
aws configure set AWS_SECRET_ACCESS_KEY <your secret access key here>
aws configure set default.region eu-west-1


replace the stuff between angled brackets with your data and the region with your bucket's region. and yes ,there is no equal sign between "AWS_ACCESS_KEY_ID" and the key.
Reply

#4
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.

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.

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"


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

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

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

Reply

#5
In windows bat file what works is a combination of user2415376 and docesam answers.

Here it is:

set userprofile=C:\Users\%username%
aws configure set AWS_ACCESS_KEY_ID <your_key>
aws configure set AWS_SECRET_ACCESS_KEY <your_secret>
aws configure set default.region <your_region>

other aws commands here
Reply

#6
Alternatively, we can copy the **.aws** folder to the users account folder (*C:\Users\ %username%*) that Task Scheduler uses to run the tasks with the AWS CLI scripts.
The **.aws** folder contains the **config** and **credential** files, created with the '*aws configure*' command.
Reply

#7
1. On the Actions tab, set the action program to `c:\windows\system32\cmd.exe` with arguments to `/C C:\upload_to_s3.bat` The /C tells cmd to close after command execution. CMD.EXE will load the environment for the user in a command shell, and then run the batch file.
2. On the General tab, make sure it is set up to run as the user that is set up to run aws. You mentioned you checked this. I mention it here as a reminder, if one is going to be using a different account. Also, make sure that account has the "Log On as a Service" right.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through