Skip to content

Instantly share code, notes, and snippets.

@daniel7byte
Created February 17, 2023 15:48
Show Gist options
  • Save daniel7byte/79de7555bff7dc7edf58284633d12609 to your computer and use it in GitHub Desktop.
Save daniel7byte/79de7555bff7dc7edf58284633d12609 to your computer and use it in GitHub Desktop.
import os
import win32clipboard # You have to install with: pip install pywin32
# this program will change the default credential of aws in .aws/credentials file
# example input in your clipboard
# [791718719052_AWSPowerUserAccess]
# aws_access_key_id=ABCDEFGHIJKLMNOPQRST
# aws_secret_access_key=ABCDEFGHIJKLMNOPQRST
# aws_session_token=ABCDEFGHIJKLMNOPQRST
# get clipboard data
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
# delete the first line of the data and add [default] to the first line
data_list = data.split("\n")
data_list.pop(0)
new_data = "[default]\n" + "".join(data_list)
# print the new data
print(new_data)
# ask for confirmation
confirm = input('Do you want to change the default credential? (y/n)')
if confirm != 'y':
print("Canceled!")
exit()
# change the default credential
home = os.path.expanduser("~")
creds_path = os.path.join(home, ".aws", "credentials")
with open(creds_path, "w") as f:
f.write(new_data)
print("Done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment