Khuphela izinto ze-S3 ngePython kunye neBoto 3

Kule posi sibonisa imizekelo yendlela yokukhuphela iifayile kunye nemifanekiso kwi-aws S3 ibhakethi usebenzisa iPython neBoto 3 ithala leencwadi.

IBoto yi-AWS SDK yePython. Inika ngokulula ukusebenzisa imisebenzi enokunxibelelana neenkonzo ze-AWS ezinje ngeebhakethi ze-EC2 kunye ne-S3.



Dowload Izinto ze-S3 ngePython kunye neBoto 3

Kulo mzekelo ulandelayo, sikhuphela ifayile enye kwibhakethi elichaziweyo le-S3.


Kuqala kufuneka senze umxhasi we-S3 usebenzisa 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 indlela ithatha iiparameter ezintathu:


Ipharamitha yokuqala ligama lebhakethi kwi-S3. Okwesibini yifayile (igama kunye nolwandiso) esifuna ukulikhuphela kwaye ipharamitha yesithathu ligama lefayile esifuna ukuyigcina njenge.



Khuphela zonke izinto ze-S3 kwiBhakethi elichaziweyo

Kumzekelo olandelayo, sikhuphela zonke izinto kwibhakethi elichaziweyo le-S3.

Ikhowudi snippet ithatha ukuba iifayile zikhona ngqo kwingcambu yebhakethi hayi kwifolda engaphantsi.

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)


Khuphela zonke izinto kwiFolda encinci yeS3

Le khowudi ilandelayo ibonisa indlela yokukhuphela iifayile ezikwifolda engaphantsi kwibhakethi le-S3.


Masithi iifayile zikwibhakethi nakwindawo elandelayo:

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)

Izalathiso

Boto 3 Amaxwebhu