Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.9k views
in Technique[技术] by (71.8m points)

amazon web services - AWS Lambda read a file in the S3 bucket using python

I have to read an xml file in the s3 bucket but each day will be a different name as I can read one or more files via lambda using Python.

reading in bucket s3.

Can someone help me.

file bucket s3

s3://pasta1/file1.xml
s3://pasta1/file2.xml
s3://pasta1/file3.xml

how i do to read wth python so no I couldn't ,I wanted to read the three files inside a for . Eu n?o consigo converter essa linha feita em Python para Lambda usando Boto3:

parser =  ET.iterparse(filenames)

error:
OSError: [Errno 36] File name too long: '<?xml version="1.0"






import boto3
from botocore.client import Config

S3_REGION="us-east-1"
bucket="my-bucket"
name="pasta1/file1.xml"

client = boto3.client('s3',region_name=S3_REGION,config=Config(signature_version='s3v4'))
list = client.list_objects_v2(Bucket=bucket,Prefix=name)
print("passo 1")
parser =  ET.iterparse(filenames)
for i in list["Contents"]:
    if i['Key'] == name: print("achou")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following example, download all objects in a specified S3 bucket. which are directly in the root of the bucket.

import boto3

def download_all_files():
    #initiate s3 resource
    s3 = boto3.resource('s3')
    your_bucket = s3.Bucket('your_bucket')
    for s3_object in your_bucket.objects.all():
        filename = s3_object.key
        your_bucket.download_file(s3_object.key, filename)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...