Predictive Hacks

Different Ways to Upload Data to S3 Using Boto3

The following script shows different ways of how we can get data to S3.

import boto3

# Initialize interfaces
s3Client = boto3.client('s3')
s3Resource = boto3.resource('s3')

# Create byte string to send to our bucket
putMessage = b'Hi! I came from Boto3!'

# put_object
response = s3Client.put_object(
    Body = putMessage,
    Bucket = 'gpipis-bucket',
    Key = 'boto3put.txt'
)

print(response)

# copy
toCopy = {
    'Bucket': 'gpipis-bucket',
    'Key': 'boto3put.txt'
}

s3Resource.meta.client.copy(toCopy, 'gpipis-bucket', 'boto3copy.txt')

# copy_object
response = s3Client.copy_object(
    Bucket = 'gpipis-bucket',
    CopySource = '/gpipis-bucket/boto3put.txt',
    Key = 'boto3copyobject.txt'
)

print(response)

# upload_file
boto3Upload = 'boto3upload.txt'

s3Resource.meta.client.upload_file(boto3Upload, 'gpipis-bucket', boto3Upload)

# upload_fileobj
with open(boto3Upload, 'rb') as fileObj:
    response = s3Client.upload_fileobj(fileObj, 'gpipis-bucket', 'boto3uploadobj.txt')
    print(response)

'''
response = s3Client.put_bucket_accelerate_configuration(
    Bucket='gpipis-bucket',
    AccelerateConfiguration={
        'Status': 'Enabled'
    }
)
print(response)
'''

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s