Dhawunirodha S3 Zvinhu NePython neBoto 3

Mune ino posvo tinoratidza mienzaniso yekuti unga dhawunirodha sei mafaera uye mifananidzo kubva kune aws S3 bhaketi uchishandisa Python neBoto 3 raibhurari.

Boto ndeye AWS SDK yePython. Inopa zvirinyore kushandisa mashandiro ayo anogona kuyanana neAWS masevhisi senge EC2 uye S3 migomo.

Dowload S3 Zvinhu NePython neBoto 3

Mumuenzaniso unotevera, tinotora imwe faira kubva pane yakatarwa S3 bhaketi.

Kutanga isu tinofanirwa kugadzira mutengi weS3 tichishandisa boto3.client(s3).

import boto3 BUCKET_NAME = 'my_s3_bucket' BUCKET_FILE_NAME = 'my_file.json' LOCAL_FILE_NAME = 'downloaded.json' def download_s3_file():
s3 = boto3.client('s3')
s3.download_file(BUCKET_NAME, BUCKET_FILE_NAME, LOCAL_FILE_NAME)

I download_file nzira inotora matatu parameter:

Yekutanga paramende izita rebucket muS3. Yechipiri ifaira (zita uye kuwedzerwa) isu tinoda kurodha pasi uye chechitatu paramende izita refaira ratiri kuda kuchengetedza se.

Dhawunirodha Zvese S3 Zvinhu mune Yakatarwa Bhaketi

Mumuenzaniso unotevera, tinotora zvinhu zvese mune yakatarwa S3 bhaketi.

Iyo kodhi snippet inofungidzira kuti mafaera ari akananga mumudzi webhaketi uye kwete mune diki-dura.

import boto3 def download_all_files():
#initiate s3 resource
s3 = boto3.resource('s3')
# select bucket
my_bucket = s3.Bucket('bucket_name')
# download file into current directory
for s3_object in my_bucket.objects.all():
filename = s3_object.key
my_bucket.download_file(s3_object.key, filename)

Dhawunirodha Zvese Zvinhu muA Sub-Folder S3 Bhaketi

Iyi inotevera kodhi inoratidza maitiro ekurodha pasi mafaera ari mune sub-folda mune S3 bhakiti.

Ngatitii mafaera ari mune anotevera bhaketi nenzvimbo:

BUCKET_NAME = 'images'

PATH = pets/cats/

import boto3 import os def download_all_objects_in_folder():
s3_resource = boto3.resource('s3')
my_bucket = s3_resource.Bucket('images')
objects = my_bucket.objects.filter(Prefix='pets/cats/')
for obj in objects:
path, filename = os.path.split(obj.key)
my_bucket.download_file(obj.key, filename)

Zvinyorwa

Boto 3 Gwaro