I am running a python script that reads the 'data.json' and provide the InstanceIds of the PrivateIP. here's the json file (data.json) that has 2 PrivateIPs with their own separate AWSAccount:
{
"ServerIPList" : [{
"PrivateIP": "17.11.11.11",
"HostName": "ip-17-11-11-11.ec2.internal",
"Region": "us-east-1",
"AccountID": "123456789123"
},
{
"PrivateIP": "18.22.22.22",
"HostName": "ip-18-22-22-22.ec2.internal",
"Region": "us-east-1",
"AccountID": "567891234567"
}],
}
Now, when i run the script using a FOR loop, it supposes to run on iteration where it keeps describing the first PrivateIP and saves the output(InstanceId) to the empty list []. It, then, describes another PrivateIP. However, it successfully convert the InstanceID of first PrivateIP but threw an error while describing the second PrivateIP. Any thought where am i missing the logic. here's my code:
import json, boto3
EC2_list = []
PrivateIP = []
AccountID = []
path = json.loads{'data.json'}
for item in input_data['ServerIPList']:
PrivateIP.append(item['PrivateIP'])
AccountID.append(item['AccountID'])
if "," in instanceIds:
instanceIds_list = instanceIds.split(",")
else:
instanceIds_list = instanceIds
if "," in AccountID:
AccountID_list = AccountID.split(",")
else:
AccountID_list = AccountID
for i in PrivateIP_list:
for account in range(len(AccountID)):
RoleArn = f"arn:aws:iam::{AccountID[account]}:role/Cloudbees"
print(RoleArn)
response = sts.assume_role(RoleArn=RoleArn, RoleSessionName="learnaws-test-session")
ec2_client = boto3.client('ec2', region_name=region_Name,
aws_access_key_id=response['Credentials']['AccessKeyId'],
aws_secret_access_key=response['Credentials']['SecretAccessKey'],
aws_session_token = response['Credentials']['SessionToken'])
ec2 = boto3.resource('ec2', region_name=region_Name,
aws_access_key_id=response['Credentials']['AccessKeyId'],
aws_secret_access_key=response['Credentials']['SecretAccessKey'],
aws_session_token = response['Credentials']['SessionToken'])
EC2Response = ec2_client.describe_instances(Filters=[
{
'Name': 'private-ip-address',
'Values': [
i,
]
}
])
print("This is the actual response :")
#print(EC2Response)
for instance in EC2Response['Reservations'][0]['Instances']:
instanceIds = instance['InstanceId']
print(instanceIds)
EC2_list.append(instanceIds)
output is :
Traceback (most recent call last):
File "ec2InstanceState.py", line 260, in <module>
for instance in EC2Response['Reservations'][0]['Instances']:
IndexError: list index out of range
arn:aws:iam::123456789123:role/Cloudbees
This is the actual response :
"i-xxxxxxxxxxxxx"
arn:aws:iam::567891234567:role/Cloudbees
This is the actual response : <BLANK> NO INSTANCEID -------????
Related
I keep receiving key error 'Notes'. Notes is in a nested response. How do I solve this? I have included a sample of the json.
Traceback added by request.
Traceback (most recent call last):
File "/Users/xxxxxx/Documents/code/trade show w notes", line 16, in <module>
target = "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" % (contact["company_name"], contact["location"], contact["summary"], contact["job_title"], contact["name"], contact["job_industry"], contact["email"], contact["first_name"], contact["last_name"], contact["notes"])
KeyError: 'notes'
\
"data": [
{
"team_id": 53806,
"name": "Nicholas Bancroft Cooke",
"first_name": "Nicholas",
],
"email": null,
"metadata": null,
"qualification": [
{
"qualification_id": 17573056,
"qualification": "connected",
"notes": null,
\\
page = 1
url = "https://teams-api.grip.events/1/team/53806/event/123721/member/236388/contact/inbound_lead/reviewed?page=1"
headers = {
'authorization': 'Bearer eaf3bd4b-6861-4ca2-a86e-3a96c73deac0',
}
data = ["company_name", "job_title", "name", "job_industry", "summary", "notes", "location", "first_name", "last_name", "email"]
with open("list31.txt", "w", encoding='utf-8') as f: #added " encoding='utf-8' "
for page in range(1, 1000):
response = requests.get(url=url, headers=headers).json()
contacts = response["data"]
for contact in contacts:
target = "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" % (contact["company_name"], contact["location"], contact["summary"], contact["job_title"], contact["name"], contact["job_industry"], contact["email"], contact["first_name"], contact["last_name"], contact["notes"])
f.write(target + "\n")
print(target)
If you are referring to the notes key which is under qualification.
Then it should be: contact["qualification"][0]["notes"]
Here contact["qualification"] is a list. if by any chance contact["qualification"] is an empty list it will raise an IndexError. you need to handle it with a try-catch or if-clause
I'm new to this and have spent 3 weeks on this error. I'm trying to get the response from a lambda function of the extracted text in an image in an s3 bucket. I successfully upload the image into the bucket from me expo project using this code:
try {
const response = await fetch(data.uri);
const blob = await response.blob();
const assetType = 'Mobile Device';
await Storage.put(`${username}/${assetType}/${UUID}`, blob, {
contentType: 'image/jpeg',
progressCallback,
});
} catch (err) {
console.log("Error uploading file:", err);
}
this will invoke the lambda function to extract the needed text
import json
import boto3
from urllib.parse import unquote_plus
import re
def lambda_handler(event, context):
bad_chars = [';', ':', '!', "*", ']', "[" ,'"', "{" , "}" , "'",","]
file_obj = event["Records"][0]
bucketname = str(file_obj["s3"]["bucket"]["name"])
filename = unquote_plus(str(file_obj["s3"]["object"]["key"], encoding='utf-8'))
textract = boto3.client('textract', region_name='ap-south-1')
print(f"Bucket: {bucketname} ::: Key: {filename}")
response = textract.detect_document_text(
Document={
'S3Object': {
"Bucket": bucketname,
"Name": filename,
}
}
)
result=[]
processedResult=""
for item in response["Blocks"]:
if item["BlockType"] == "WORD":
result.append(item["Text"])
element = item["Text"] + " "
processedResult += element
print (processedResult)
res=[]
imei=""
#Extracting imei number and removing the bad characters
str_IMEI= str(re.findall('(?i)imei*?[1]*?[:]\s*\d{15}',processedResult))
imei= str(re.findall('[^\]]+',str_IMEI))
imei = str(imei.split(' ')[-1])
for i in bad_chars :
imei = imei.replace(i, '')
if len(imei)>0 :
res.append(str(imei))
else:
res.append('')
#Extracting imei2 number and removing the bad characters
str_IMEI2= str(re.findall('(?i)imei*?[2]*?[:]\s*\d{15}',processedResult))
imei2= str(re.findall('[^\]]+',str_IMEI2))
imei2 = str(imei2.split(' ')[-1])
for i in bad_chars :
imei2 = imei2.replace(i, '')
if len(imei2)>0 :
res.append(str(imei2))
else:
res.append('')
#Extracting model number and removing the bad characters
str_Model = str(re.findall('(?i)model*?[:]\s*[\w|\d|_,-|A-Za-z]+\s*[\w|\d|_,-|A-Za-z]+',processedResult))
result = str(str_Model.split(' ')[-1])
for bad_char in bad_chars :
result = result.replace(bad_char, '')
if len(result) >0 :
res.append(result)
else:
res.append('')
print(res)
return {
'statusCode': 200,
#'body': JSON.stringify(result)
'body': res
}
and then use RESTapi to get the response from the lambda
try{
const targetImage = UUID + '.jpg';
const response = await fetch(
'https://6mpfri01z0.execute-api.eu-west-2.amazonaws.com/dev',
{
method: 'POST',
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(targetImage)
}
)
const OCRBody = await response.json();
console.log('OCRBody', OCRBody);
} catch (err) {
console.log("Error extracting details:", err);
}
but i keep getting this error
OCRBody Object {
"errorMessage": "string indices must be integers",
"errorType": "TypeError",
"requestId": "8042d6f7-44de-4a54-8209-fe93ca8570ec",
"stackTrace": Array [
" File \"/var/task/lambda_function.py\", line 10, in lambda_handler
file_obj = event[\"Records\"][0]
",
],
}
PLEASE HELP.
As the error says, you are trying to slice a string type with a some kind of key as if it were a dict type:
>>> str_name = 'Jhon Doe'
>>> str_name['name']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: string indices must be integers
Normaly, this issue could cause, that you do not receive the correct data in header.
In all of my Lambda function, is use something like this:
bucket = event['Records'][0]['s3']['bucket']['name']
Event is most of the time a valid dict within Python, so you have two possibilities here:
Option 1
Before proceeding, to something like
if 'Records' in event:
if 's3' in event['Records'][0]:
if 'bucket' in ...
if 'name' in ...
Only if all condition are true, you should proceed.
Option 1
do a simple
print(event)
and check your event, when your Lambda will be failing.
The issue here could cause that the Lambda is triggered before the S3 object is available.
What you can do is to add a S3 trigger which will be invoked automatically the Lambda, as soon as the S3 object was uploaded. See here
https://docs.aws.amazon.com/AmazonS3/latest/userguide/notification-how-to-event-types-and-destinations.html
I've been trying to make an API call to get all the users from an Apple Store.
After using this script I only get a limited number of results:
import json
from appstoreconnectapi import AppStoreConnect
ISSUER_ID = "..."
KEY_ID = "..."
asc = AppStoreConnect(KEY_ID, "./key.p8", ISSUER_ID)
res = asc.fetch(uri='/v1/users', method='get', post_data=None)
print (res)
with open('output.json', 'w') as out:
out.write(json.dumps(res, indent=4))
After 50 result showed this is what i get at the end of my JSON file:
"meta": {
"paging": {
"total": 356,
"limit": 50
}
}
JSON file:https://1drv.ms/w/s!AizscpxS0QM4hJl99vVfUMvEjgXV3Q
i can extract TECH-XXX
#!/usr/bin/python
import sys
import json
sys.stdout = open('output.txt','wt')
datapath = sys.argv[1]
data = json.load(open(datapath))
for issue in data['issues']:
if len(issue['fields']['subtasks']) == 0:
print(issue['key'])
For every issue without subtasks (TECH-729
TECH-731) i want to extract TECH from
project": {
"avatarUrls": {
"16x16": "https://jira.corp.company.com/secure/projectavatar?size=xsmall&pid=10001&avatarId=10201",
"24x24": "https://jira.corp.company.com/secure/projectavatar?size=small&pid=10001&avatarId=10201",
"32x32": "https://jira.corp.company.com/secure/projectavatar?size=medium&pid=10001&avatarId=10201",
"48x48": "https://jira.corp.company.com/secure/projectavatar?pid=10001&avatarId=10201"
},
"id": "10001",
"key": "TECH",
"name": "Technology",
"self": "https://jira.corp.company.com/rest/api/2/project/10001"
},
and customfield_10107.id
i tried with print(issue['customfield_10107']['id']) and got
./tasks1.py 1.json
Traceback (most recent call last):
File "./tasks1.py", line 11, in <module>
print(issue['customfield_10107']['id'])
KeyError: 'customfield_10107'
key exists under issue and customfield_10107 exists in issue['fields']:
for issue in response["issues"]:
# For issues without subtasks
if len(issue['fields']['subtasks']) == 0:
# Print custom field id
if 'customfield_10107' in issue['fields']:
custom_field = issue['fields']['customfield_10107']
print custom_field['id']
# Print key
if 'key' in issue:
print issue['key']
I am making REST calls on a server. The first REST call gets all the projects and from that I store the project's IDs in an array.
Below is the JSON.
For e.g. it would return something like this:
[
{
"expand": "description,lead,url,projectKeys",
"self": "http://localhost:8080/rest/api/2/project/10101",
"id": "10101",
"key": "GR1",
"name": "Group1Project",
"avatarUrls": {
"48x48": "http://localhost:8080/secure/projectavatar?avatarId=10324",
"24x24": "http://localhost:8080/secure/projectavatar?size=small&avatarId=10324",
"16x16": "http://localhost:8080/secure/projectavatar?size=xsmall&avatarId=10324",
"32x32": "http://localhost:8080/secure/projectavatar?size=medium&avatarId=10324"
},
"projectTypeKey": "software"
}
]
Then I'm looping through that array and making another REST call for each project id(10101).
This gives me groups/users against that project.
For example:
{
"self": "http://localhost:8080/rest/api/2/project/10000/role/10100",
"name": "Developers",
"id": 10100,
"actors": [
{
"id": 10207,
"displayName": "group2",
"type": "atlassian-group-role-actor",
"name": "group2",
"avatarUrl": "http://localhost:8080/secure/useravatar?size=xsmall&avatarId=10123"
}
]
}
I want to get all the project IDs where name == group2.
Following is my Python code for all of this but it's not working.
import requests
ids = []
response = requests.get('http://localhost:8080/rest/api/2/project',
auth=('*', '*'))
data = response.json()
for line in data:
ids.append(line["id"])
print(ids)
# Check if group exists in Project roles.
# If it does, then save the project name in the list of arrays.
projectNames = []
for id in ids:
url = 'http://localhost:8080/rest/api/2/project/'+id+'/role/10100'
response = requests.get(url,
auth = ('*', '*'))
data = response.json()
if data.displayName == 'group2':
projectNames.append(["id"])
Could you please help me out how to do this?
Thank you.
Tayyab,
You need to do this. It will work.
for actor in data['actors']:
if actor['displayName']=='group2':
projectNames.append(id)
projectNames = []
for id in ids:
url = 'http://localhost:8080/rest/api/2/project/'+id+'/role/10100'
response = requests.get(url,
auth = ('*', '*'))
data = response.json()
for actor in data["actors"]:
if actor["displayName"] and actor["displayName"] == "group2":
projectNames.append(actor["id"])
projectNames = set()
for id in ids:
url = 'http://localhost:8080/rest/api/2/project/'+id+'/role/10100'
response = requests.get(url,
auth = ('*', '*'))
data = response.json()
group2_actors = [actor['id'] for actor in data['actors']
if actor['displayName'] == 'group2']
if len(group2_actors) > 0:
projectNames.update(group2_actors)
projectNames is a set of unique actor ids with displayName == group2.
some_json = {}
result = [actor['id'] for actor in some_json['actors'] if actor['name']=='group2']
so result for that second json will be [10207]