Python Forum
Heeelp! I'm stuck... Girl is freakin' here :(
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Heeelp! I'm stuck... Girl is freakin' here :(
#1
I would like to accomplish the following using Python Lambda function:

- Pull subscription ID from the folder within in the S3 bucket
- Query the API in a loop for every subscription ID ( This is the API call: https://faktor.chargify.com/subscription...sages.json)
- Store the result of the API in a new folder in the S3 bucket

So far I was only able to make the following Python code read files from the S3 bucket:
import boto3
def lambda_handler(event, context):
s3 = boto3.client("s3")

if event:
print("Event : ", event)
file_obj = event["Records"][0]
filename = str(file_obj['s3']['object']['key'])
print("Filename: " , filename)
fileObj = s3.get_object(Bucket = "chargifytesting" , Key=filename)
file_content = fileObj["Body"].read().decode('utf-8')
print(file_content)
Reply
#2
I haven't run any of your code, but you need to indent after you use a colon.
import boto3
def lambda_handler(event, context):
    s3 = boto3.client("s3")
 
if event:
    print("Event : ", event)
    file_obj = event["Records"][0]
    filename = str(file_obj['s3']['object']['key'])
    print("Filename: " , filename)
    fileObj = s3.get_object(Bucket = "chargifytesting" , Key=filename)
    file_content = fileObj["Body"].read().decode('utf-8')
    print(file_content)
Try that and post your errors if it doesn't work

Also, you do not appear to be calling lambda_handler anywhere in your code.
Reply
#3
(Jan-08-2020, 03:48 PM)Clunk_Head Wrote: ou do not appear to be calling lambda_handler anywhere in your code
obviously the whole if block is part of the function - e.g. it uses event
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(Jan-08-2020, 04:14 PM)buran Wrote:
(Jan-08-2020, 03:48 PM)Clunk_Head Wrote: ou do not appear to be calling lambda_handler anywhere in your code
obviously the whole if block is part of the function - e.g. it uses event

Ahh, that changes the code a bit more then
import boto3
def lambda_handler(event, context):
    s3 = boto3.client("s3")
  
    if event:
        print("Event : ", event)
        file_obj = event["Records"][0]
        filename = str(file_obj['s3']['object']['key'])
        print("Filename: " , filename)
        fileObj = s3.get_object(Bucket = "chargifytesting" , Key=filename)
        file_content = fileObj["Body"].read().decode('utf-8')
        print(file_content)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020