简体   繁体   English

如何使用 Lambda 函数将下载的文件上传到 s3 存储桶

[英]How to upload downloaded file to s3 bucket using Lambda function

I saw different questions/answers but I could not find the one that worked for me.我看到了不同的问题/答案,但找不到适合我的问题/答案。 Hence, I am really new to AWS, I need your help.因此,我对 AWS 真的很陌生,我需要你的帮助。 I am trying to download gzip file and load it to the json file then upload it to the S3 bucket using Lambda function.我正在尝试下载 gzip 文件并将其加载到 json 文件,然后使用 Lambda 函数将其上传到 S3 存储桶。 I wrote the code to download the file and convert it to json but having problem while uploading it to the s3 bucket.我编写了下载文件并将其转换为 json 的代码,但在将其上传到 s3 存储桶时出现问题。 Assume that file is ready as x.json.假设该文件已准备好作为 x.json。 What should I do then?那我该怎么办?

I know it is really basic question but still help needed :)我知道这是非常基本的问题,但仍然需要帮助:)

This code will upload to Amazon S3:此代码将上传到 Amazon S3:

import boto3

s3_client = boto3.client('s3', region_name='us-west-2')  # Change as appropriate
s3._client.upload_file('/tmp/foo.json', 'my-bucket', 'folder/foo.json')

Some tips:一些技巧:

  • In Lambda functions you can only write to /tmp/在 Lambda 函数中,您只能写入/tmp/
  • There is a limit of 512MB有 512MB 的限制
  • At the end of your function, delete the files (zip, json, etc) because the container can be reused and you don't want to run out of disk space在您的函数结束时,删除文件(zip、json 等),因为容器可以重复使用并且您不想用完磁盘空间

If your lambda has proper permission to write a file into S3, then simply use boto3 package which is an AWS SDK for python.如果您的 lambda 具有将文件写入 S3 的适当权限,则只需使用boto3包,它是适用于 Python 的 AWS 开发工具包。

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html

Be aware that if the lambda locates inside of VPC then lambda cannot access to the public internet, and also boto3 API endpoint.请注意,如果 lambda 位于 VPC 内部,则 lambda 无法访问公共互联网,也无法访问 boto3 API 端点。 Thus, you may require a NAT gateway to proxy lambda to the public.因此,您可能需要一个 NAT 网关来向公众代理 lambda。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 boto3 从 S3 存储桶中的表单上传文件,并在 lambda 中创建处理程序 - Upload a file from form in S3 bucket using boto3 and handler is created in lambda 我无法使用 lambda function 从 S3 存储桶读取 dat 文件 - I am not able to read dat file from S3 bucket using lambda function Lambda function 使用 Boto 删除 S3 存储桶 - Lambda function to delete an S3 bucket using Boto 如何使用事件触发的lambda从S3存储桶获取最新的文件名或文件 - How to get latest file-name or file from S3 bucket using event triggered lambda 无法使用Python Boto 3在AWS s3存储桶中上传文件 - unable to upload file in AWS s3 Bucket using Python Boto 3 将文本文件加载到 S3 存储桶时,如何使用 Lambda 函数调用 Glue 函数 (ETL) - How can I use a Lambda function to call a Glue function (ETL) when a text file is loaded to an S3 bucket XML上载到S3存储桶,通过Lambda / Python提取 - XML upload to S3 bucket, ingest with Lambda/Python 无法读取从 s3 存储桶下载的 json 文件 - Cannot read json file downloaded from s3 bucket 如何使用可用于 aws lambda 函数的 goofys 将 s3 存储桶上的文件安装到 aws ec2 实例? - How can I make files on s3 bucket mounted to aws ec2 instance using goofys available to aws lambda function? AWS Lambda put_object 函数未将文件发送到目标 s3 存储桶 - AWS Lambda put_object function not sending file to destination s3 bucket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM