How to download a S3 File from Lambda in Python | Step by Step Guide

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ส.ค. 2024
  • In this video, I walk you through how to read a JSON file in S3 from a Lambda function with 3 easy steps. I start by taking note of the S3 bucket and key of our file. Secondly, I create a Lambda function with S3 Read permissions. Finally, I code up the Lambda function in Python.
    Looking to get hands on experience building on AWS with a REAL project? Check out my course - The AWS Learning Accelerator! courses.beabetterdev.com/cour...
    📚 MY RECOMMENDED READING LIST FOR SOFTWARE DEVELOPERS📚
    Clean Code - amzn.to/37T7xdP
    Clean Architecture - amzn.to/3sCEGCe
    Head First Design Patterns - amzn.to/37WXAMy
    Domain Driver Design - amzn.to/3aWSW2W
    Code Complete - amzn.to/3ksQDrB
    The Pragmatic Programmer - amzn.to/3uH4kaQ
    Algorithms - amzn.to/3syvyP5
    Working Effectively with Legacy Code - amzn.to/3kvMza7
    Refactoring - amzn.to/3r6FQ8U
    🎙 MY RECORDING EQUIPMENT 🎙
    Shure SM58 Microphone - amzn.to/3r5Hrf9
    Behringer UM2 Audio Interface - amzn.to/2MuEllM
    XLR Cable - amzn.to/3uGyZFx
    Acoustic Sound Absorbing Foam Panels - amzn.to/3ktIrY6
    Desk Microphone Mount - amzn.to/3qXMVIO
    Logitech C920s Webcam - amzn.to/303zGu9
    Fujilm XS10 Camera - amzn.to/3uGa30E
    Fujifilm XF 35mm F2 Lens - amzn.to/3rentPe
    Neewer 2 Piece Studio Lights - amzn.to/3uyoa8p
    💻 MY DESKTOP EQUIPMENT 💻
    Dell 34 inch Ultrawide Monitor - amzn.to/2NJwph6
    Autonomous ErgoChair 2 - bit.ly/2YzomEm
    Autonomous SmartDesk 2 Standing Desk - bit.ly/2YzomEm
    MX Master 3 Productivity Mouse - amzn.to/3aYwKVZ
    Das Keyboard Prime 13 MX Brown Mechanical- amzn.to/3uH6VBF
    Veikk A15 Drawing Tablet - amzn.to/3uBRWsN
    📚 References:
    Getting started with AWS: • Introduction to AWS | ...
    S3 to Lambda Trigger - • AWS S3 File Upload + L...
    Lambda Code - gist.github.com/djg07/8f8455c...
    ☁Topics covered include:
    - S3 Bucket
    - S3 Key
    - S3 GetObject
    - Lambda Function
    - Lambda S3 GetObject
    - Lambda Boto3
    - Lambda Test Event
    - Python JSON processing
    🌎 Find me here:
    Twitter - / beabetterdevv
    Instagram - / beabetterdevv
    Patreon - Donations help fund additional content - / beabetterdev
    #AWS
    #S3
    #Lambda

ความคิดเห็น • 48

  • @scottpaige7999
    @scottpaige7999 4 ปีที่แล้ว +1

    This was great. After days of searching your video was exactly what I was looking for thanks.

  • @thibaultl4413
    @thibaultl4413 2 ปีที่แล้ว

    You saved my live, man. Thank !

  • @flerlagekr
    @flerlagekr 4 ปีที่แล้ว +1

    Exactly what I needed!

    • @BeABetterDev
      @BeABetterDev  4 ปีที่แล้ว

      Glad I was able to help ken!

  • @danilodenadaisicari4630
    @danilodenadaisicari4630 2 ปีที่แล้ว +1

    Your videos are the best!! Keep going. Made my day better today hehe

    • @BeABetterDev
      @BeABetterDev  2 ปีที่แล้ว

      You're very welcome Danilo! YOUR comment made MY day better! :D

  • @ankitaburman5406
    @ankitaburman5406 3 ปีที่แล้ว +1

    Thank you sir for this tutorial you are a life saver ...❤️❤️

    • @BeABetterDev
      @BeABetterDev  2 ปีที่แล้ว +1

      You're very welcome Otakugirl!

  • @mrbeats7434
    @mrbeats7434 3 ปีที่แล้ว +1

    Well done man great job

  • @manuelrico80
    @manuelrico80 2 ปีที่แล้ว +1

    Thanks!

  • @psychobuddha5379
    @psychobuddha5379 4 ปีที่แล้ว +8

    I might be missing something, but this is a reading from the S3 file, not downloading the file. What if you want to actually download that or any other file to your local computer? Besides that, great videos, very concise and easy to understand.

    • @vidhinagda6445
      @vidhinagda6445 4 ปีที่แล้ว +4

      I am also trying to explore how can I automatically download a file to my local machine from S3 using Lambda Trigger

    • @GracianoVirginia
      @GracianoVirginia 3 ปีที่แล้ว +3

      The same concern I am looking how to download not reading

  • @DTX_
    @DTX_ 4 ปีที่แล้ว

    Love your videos man. Suggestion: GET request with API Gateway + Lambda + S3. Path Param of a file name and have lambda retrieve it from S3 and return it through API Gateway! :)

    • @BeABetterDev
      @BeABetterDev  4 ปีที่แล้ว

      Thanks Daniel! Will add that to my list!

  • @temophee
    @temophee 4 ปีที่แล้ว +4

    Hey, love the videos. Question: How different would the steps be if I wanted to upload/download an image file rather than JSON?

    • @BeABetterDev
      @BeABetterDev  4 ปีที่แล้ว +3

      Hey there!
      The process would be similar, you would just need to encode your image during upload, and decode it during download. I found an applicable tutorial that may be helpful for you (link below). It is using NodeJS but the concepts are the same. Hope this helps!
      medium.com/think-serverless/image-upload-and-retrieval-from-s3-using-aws-api-gateway-and-lambda-b4c2961e8d1

    • @sushmitha717
      @sushmitha717 ปีที่แล้ว

      Did you find how to do it using python? I m unable to do it. If you could help me, it will be great

  • @knandi73
    @knandi73 2 ปีที่แล้ว

    Very good tutorial.
    There is a CSV file in the local computer.
    It is uploaded to S3.
    Any changes made in the CSV file on the local computer, the changes should reflect in S3 automatically.
    How to do that?

  • @cwelsh01
    @cwelsh01 4 ปีที่แล้ว +3

    Can you do a video that takes this one step further and inserts the results of the JSON into an Aurora table?

    • @BeABetterDev
      @BeABetterDev  4 ปีที่แล้ว

      Thanks for the suggestion chris. I'll add your request to my list of topics to cover. Cheers.

  • @ankitaburman5406
    @ankitaburman5406 3 ปีที่แล้ว

    Hi can you also make a video on load this json record to a table

  • @NguyenHung-hb3sb
    @NguyenHung-hb3sb 3 ปีที่แล้ว

    Hello everyone, I have a task that need to read the content of a file on s3 like this video but the problem is the file was compressed to the .gz type. I cannot read its content directly, Could you please help me what scenario should I use to read the file? I use nodejs, lambda and s3 service.

  • @aatishacyrill7741
    @aatishacyrill7741 3 ปีที่แล้ว +1

    Hi, your videos are really helpful. I wanted to know how to copy files(csv) from s3 to a windows EC2 instance through a lambda function. What are the ways in which that could be done. I am very new to AWS.
    Any help will be appreciated.
    Thanks.

    • @BeABetterDev
      @BeABetterDev  3 ปีที่แล้ว

      Hi Aatisha, Thank you for your kind words.
      With regards to your question. Is this a one time operation? Or a functionality that you need to expose to your customers through an API? The approach you take will be different. Maybe if you can describe to me your use case and exactly what you are trying to do I can help more.
      Cheers and thanks for watching.

  • @karanchandra9789
    @karanchandra9789 2 ปีที่แล้ว

    Great , I have one question , Based on Put object on S3 , I want to trigger a Lambda function and that lambda will do some modification to object(type JSON) and will store modified object in another S3 bucket how I can do this ?
    Thanks in advance

  • @prasadkavuri8871
    @prasadkavuri8871 3 ปีที่แล้ว

    Hi sir I have some requirement.
    How to automate the code to copy the S3 bucket to S3 glacier

  • @ankitagarwal6014
    @ankitagarwal6014 2 ปีที่แล้ว

    My lambda function is unable to read file from S3 bucket which is private and have given s3fullaccess to lambda execution role

  • @roshm4484
    @roshm4484 3 ปีที่แล้ว

    good afternoon,
    how to combine multiple jason and insert into dynamo db using python.
    Please help me on this. @Be A Better Dev

  • @srikarreddysingareddy4190
    @srikarreddysingareddy4190 4 ปีที่แล้ว +1

    Can you please create a tutorial to copy the files from S3 to ec2 instance using lambda function. Thanks

    • @BeABetterDev
      @BeABetterDev  4 ปีที่แล้ว +1

      Thanks Srikar for the suggestion - I've added it to many backlog.

  • @vyvy093
    @vyvy093 9 หลายเดือนก่อน

    is there any way I can copy files from s3 to sharepoint by using lambda?

  • @vinoothmani4957
    @vinoothmani4957 4 ปีที่แล้ว

    Can you tell us how to extract the data using REST API from Lambda function (CSv or json)

    • @BeABetterDev
      @BeABetterDev  4 ปีที่แล้ว

      Hi Vinooth, the data should be present in the event object that is passed into your lambda. A simple print of the object should show you how you can access the payload.

  • @demianjennings689
    @demianjennings689 2 ปีที่แล้ว

    stupid question. When you click on JSON files in your s3 bucket, they open up in the browser. My computer wants to download them first before I open them. I'm running ubuntu20.04 and chrome and brave browser. Just wondering if you knew a quick solution.

  • @junshenglow5764
    @junshenglow5764 4 ปีที่แล้ว

    Hi, If my file stored in specific path in s3, how am i going to specific the path to retrieve my file?

    • @BeABetterDev
      @BeABetterDev  4 ปีที่แล้ว

      Hi Jun,
      You would just use the nested path separated by / characters when specifying the Key.

  • @PHardFoGovna
    @PHardFoGovna ปีที่แล้ว

    I was hoping this was downloading a file from S3 using Lambda. How does one accomplish this?

  • @MiteshMungse
    @MiteshMungse ปีที่แล้ว

    how to read excel file

  • @GracianoVirginia
    @GracianoVirginia 3 ปีที่แล้ว

    Very good video, but this is a reading from the S3 file, not downloading the file. I want to download my files to my local computer, is that possible?

    • @BeABetterDev
      @BeABetterDev  3 ปีที่แล้ว +1

      Hi Vicky,
      Lambda is a 'serverless' solution - so the machine that is reading the file from S3 is on the cloud.
      To download the file to your local computer, you can use almost the exact same approach except run the code from your machine. This requires a little bit of setup (see some of my other videos on IAM roles/policies), but can be easily done.
      Once you read the file from S3 like in the example above, you can write the file to your local disk. Here's an example I found that takes care of this last part: stackabuse.com/reading-and-writing-json-to-a-file-in-python/
      Hope this helps.

    • @GracianoVirginia
      @GracianoVirginia 3 ปีที่แล้ว +1

      @@BeABetterDev Oh wow thanks for your quick reply. I want to automate the process, manually I can go to the GUI, see the object in the bucket and download my object, but to automate this process I was thinking on a S3 event which triggers a lambda function that download the object to my "private data center"

    • @BeABetterDev
      @BeABetterDev  3 ปีที่แล้ว +3

      Hi Vicky. For a use case like this, I can think of two possible options:
      1) send the S3 put notification to an http endpoint that is hosted in your data center. This endpoint can be invoked and can be set up to download the file from S3 and save to a local destination.
      2) same idea as 1, but make the SNS topic deliver the message to an sqs queue, and have an on premise machine poll the queue regularly for messages. Alternatively you can use a Cron job that runs periodically and processes all the messages.
      I'm more familiar with #2 so it's probably what I would personally go with. However technically #1 seems simpler at face value.
      Hope this helps !
      Daniel

    • @GracianoVirginia
      @GracianoVirginia 3 ปีที่แล้ว +1

      @@BeABetterDev very useful options, thanks so much for your quick reply and support, new follower

    • @BeABetterDev
      @BeABetterDev  3 ปีที่แล้ว

      You're welcome !