Author - StudySection Post Views - 78 views
S3 bucket

Brief guide to S3 bucket in python

In this post, we will discuss some basic operations performed on S3 buckets in python. In this discussion, we will consider that you have some basic knowledge of AWS and the S3 bucket. Firstly before we start we need to understand that we need some packages required, to perform the below operations.

Install the Boto3:

Boto3 is the Amazon Web Services Software Development Kit, which allows Python developers to write software that makes use of services like Amazon S3.
pip install boto3

How to Upload Files in S3 bucket in python?

In this section, you’ll learn how to read a file from a local system and update it to an S3 object.
def UploadFilesOnBucket(bucketName, folderName, srcLocation):
try:
keyName = folderName +os.sep+ os.path.basename(srcLocation)
print(keyName + "\nUploaded\n")
S3_BUCKET.Bucket(bucketName).upload_file(Filename=srcLocation, Key=keyName)
except Exception as e:
print(e)

upload_file is a predefined function which requires 2 parameters: firstly the source location of the file from where the file is placed and the second parameter is the location of the destination.

How to Delete Files in S3 bucket in python?

In this section, you’ll learn how to delete a file from an S3 object.
def deleteFromS3bucket(
sessionObj, bucketName, fileDeleteOnS3Bucket, destinationPathForS3Bucket):
try:
s3Obj = sessionObj.resource("s3")
your_bucket = s3Obj.Bucket(bucketName)
s3 = sessionObj.resource("s3")
obj = s3.Object(bucketName, fileDeleteOnS3Bucket)
obj.delete()
except Exception as e:
print(e)

obj.delete() is a predefined function which requires 2 parameters: firstly the bucket name and the second parameter is the location of the file.
How to get all files from the S3 bucket in python?

In this section, you’ll learn how to get listing all the files from an S3 object.
def getFilesListing(sessionObj,bucketName):
try:
s3Obj = sessionObj.resource('s3')
your_bucket = s3Obj.Bucket(bucketName)
i=1
for s3_file in your_bucket.objects.all():
print(s3_file.key)
i=i+1
except Exception as e:
print(e)

s3Obj.Bucket() is a predefined function which requires the name of the bucket after which we iterate over the array to print the listing of the FIle as shown above.

English is a commonly used language in almost all fields today. Having a certification in the English language can enhance your career aspects. StudySection offers both basic level and advanced level English certification exams to test and certify the English language skills.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri