Home > database >  AWS save a file from lambda directory /tmp/ to S3 bucket
AWS save a file from lambda directory /tmp/ to S3 bucket

Time:10-20

How can I copy a file from my lambda /tmp/ to a bucket folder? I'm trying this:

import boto3

temp_input_filepath = r'/tmp/audio_file.wav'

with open(temp_input_filepath, 'wb') as handle:
    handle.write(mpeg_bytes)

try:
    s3 = boto3.resource('s3')
    bucket = s3.Bucket('wav-audio-bucket')
    key = 'wav-files/'   user_id_uuid   ".wav" #key
    bucket.upload_file(temp_input_filepath, key)
except:
    print ("Error writting file:", key)

But it's not working:

2021-10-15T12:13:38.563 03:00   Error writting file: wav-files/dgmgn_20211015abbf-d56d-47ac-8db2-b099b99cdcc4.wav

CodePudding user response:

Thanks @luk2302. Now it's working I created a policy Tutorial-managed-policies like this:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "logs:CreateLogStream",
                "s3:ListBucket",
                "s3:DeleteObject",
                "logs:PutLogEvents"
            ],
            "Resource": [
                "arn:aws:logs:*:*:*",
                "arn:aws:s3:::wav-audio-bucket/*",
                "arn:aws:s3:::wav-audio-bucket"
            ]
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": "logs:CreateLogGroup",
            "Resource": "arn:aws:logs:*:*:*"
        },
        {
            "Sid": "VisualEditor2",
            "Effect": "Allow",
            "Action": [
                "s3:GetAccountPublicAccessBlock",
                "s3:ListAllMyBuckets",
                "s3:ListBucket"
            ],
            "Resource": "*"
        }
    ]
}
  • Related