Friday, February 4, 2022

[SOLVED] Putting objects into a bucket in boto3

Issue

#!/usr/bin/env python3
import sys
import boto3
s3 = boto3.resource("s3")
bucket_name = sys.argv[1]
object_name = sys.argv[2]
try:
 response = s3.Object(bucket_name,
object_name).put(Body=open(object_name, 'rb'))
 print (response)
except Exception as error:
 print (error)

That's my put_bucket function that puts objects into buckets but I need it done with little to no user input once I call the main program, basically automatic.

import sys
import boto3
ec2 = boto3.resource('ec2', region_name = 'eu-west-1')
s3 = boto3.resource('s3')
keyname = 'AlexBpem.pem'
user_data = '''#!/bin/bash
yum update -y
yum install httpd -y
systemctl enable httpd
systemctl start httpd'''
s3.create_bucket(Bucket='ec2-assignbuke2',CreateBucketConfiguration={'LocationConstraint': 'eu-west-1'})
sg = ec2.create_security_group(GroupName='MyWebServer', Description = 'WebServer', VpcId='vpc-0dea879f34afff60d')

instance = ec2.create_instances(
 ImageId='ami-0fc970315c2d38f01',
 MinCount=1,
 MaxCount=1,
 InstanceType='t2.nano',
 KeyName = 'AlexBpem',
 UserData = user_data, 
 SecurityGroupIds=[ sg.group_id ],
 SubnetId = 'subnet-06add059500af7905' 
)

for bucket_name in sys.argv[1:]:
 try:
     response = s3.create_bucket(Bucket=ec2-assignbuke2,
CreateBucketConfiguration={'LocationConstraint': 'eu-west-1'})
     print (response)
 except Exception as error:
     print (error)

Is my main program, how would I go abouts using (def) functions within my code to make this call automatic.


Solution

To convert your inline 'upload to S3' code into a function, you can do this:

#!/usr/bin/env python3
import sys
import boto3
s3 = boto3.resource("s3")

def upload_to_s3(filename, bucket, key):
  try:
    response = s3.Object(bucket, key).put(Body=open(filename, 'rb'))
    print (response)
    return response
  except Exception as error:
    print (error)

To call this function, you can do this:

file = '/home/users/james/dogs/toto.png'
bkt = 'mybucket'
key = 'dogs/tot.png'
rsp = upload_to_s3(file, bkt, key)


Answered By - jarmod
Answer Checked By - Marilyn (WPSolving Volunteer)