【问题标题】:Amazon S3 object modify/replaceAmazon S3 对象修改/替换
【发布时间】:2018-11-04 02:14:05
【问题描述】:

enter image description here是否可以使用 lambda 清除 S3 内部的 S3 对象?

1) 我有清理文件数据的 python 脚本 -

import re
import os
sourcepath = os.listdir('InputFiles/')
for file in sourcepath:
    inputfile = 'InputFiles/'+ file
    print ('Conversion is going for :'+ inputfile)
    with open(inputfile,'r') as inputfile:
         filedata = inputfile.read()
    freq = 0
    destinationpath = 'Outputfile/'+ file
    filedata = re.sub('[^a-zA-Z0-9\n\.]', ' ', filedata)
    with open (destinationpath,'w') as file:
         file.write(filedata)
    print filedata

你能在 AWS lambda 里面写这个逻辑吗

【问题讨论】:

    标签: python amazon-web-services amazon-s3 amazon-ec2 aws-lambda


    【解决方案1】:

    是的,你可以。请查看 Boto3 S3 参考(http://boto3.readthedocs.io/en/latest/guide/s3.html#s3-guide):

    这里有一些帮助您入门:

    import boto3
    s3 = boto3.resource('s3')
    your_bucket = s3.Bucket('name_of_your_bucket')
    
    for item_in_bucket in your_bucket.objects.all():
         # do stuff
    

    需要注意的几点:

    • 确保运行 lambda 函数的角色有权访问 S3
    • 确保增加 lambda 超时配置(最多 5 分钟),以便您有足够的时间清理存储桶中的所有对象。
    • 如果超过 5 分钟,您可以考虑使用带有 Step Functions 的 AWS 状态机和模块化 lambda 操作来清理和存储文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-14
      • 1970-01-01
      • 2012-06-23
      • 2023-03-22
      • 2013-04-10
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多