<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: SHRUTIKA MAHINDRAKAR</title>
    <description>The latest articles on DEV Community by SHRUTIKA MAHINDRAKAR (@shrutika_mahindrakar_5806).</description>
    <link>https://dev.to/shrutika_mahindrakar_5806</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2735320%2Fc7344a39-1a31-452d-b39e-c89a85636c79.jpg</url>
      <title>DEV Community: SHRUTIKA MAHINDRAKAR</title>
      <link>https://dev.to/shrutika_mahindrakar_5806</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shrutika_mahindrakar_5806"/>
    <language>en</language>
    <item>
      <title>Getting Started with Uploading Files to AWS S3 Using Python</title>
      <dc:creator>SHRUTIKA MAHINDRAKAR</dc:creator>
      <pubDate>Sun, 19 Jan 2025 17:34:11 +0000</pubDate>
      <link>https://dev.to/shrutika_mahindrakar_5806/getting-started-with-uploading-files-to-aws-s3-using-python-4mj8</link>
      <guid>https://dev.to/shrutika_mahindrakar_5806/getting-started-with-uploading-files-to-aws-s3-using-python-4mj8</guid>
      <description>&lt;p&gt;Introduction:&lt;br&gt;
Amazon S3 (Simple Storage Service) is one of the most widely-used cloud storage solutions, ideal for storing and retrieving files of all types. Whether you're looking to back up important data or power a web application, understanding how to work with S3 is an essential skill for any developer.&lt;/p&gt;

&lt;p&gt;In this post, we’ll explore a straightforward Python script that will help you upload files to an S3 bucket in just a few simple steps. Ready? Let’s get started!&lt;/p&gt;

&lt;p&gt;Getting Started&lt;br&gt;
To interact with AWS services like S3, we'll use the boto3 library. It’s Amazon’s official Python SDK, and it simplifies working with AWS services by providing clean, Pythonic methods to interact with them.&lt;/p&gt;

&lt;p&gt;Step 1: Install the Required Library&lt;br&gt;
Before starting, ensure that Python is installed on your system. Then, install the boto3 library by running the following command in your terminal:&lt;/p&gt;

&lt;p&gt;bash&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;br&gt;
pip install boto3&lt;br&gt;
This library will serve as our bridge to communicate with AWS S3.&lt;/p&gt;

&lt;p&gt;Step 2: Set Up AWS Credentials&lt;br&gt;
To access your AWS account programmatically, you’ll need to configure credentials. Here’s how:&lt;/p&gt;

&lt;p&gt;Log in to the AWS Management Console.&lt;/p&gt;

&lt;p&gt;Navigate to the IAM (Identity and Access Management) section.&lt;/p&gt;

&lt;p&gt;Create a new user with programmatic access and assign the AmazonS3FullAccess policy to the user.&lt;/p&gt;

&lt;p&gt;Download the access key and secret key provided.&lt;/p&gt;

&lt;p&gt;Use the AWS CLI to configure the credentials locally:&lt;/p&gt;

&lt;p&gt;bash&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;br&gt;
aws configure&lt;br&gt;
When prompted, enter the access key, secret key, and default region (e.g., us-east-1).&lt;/p&gt;

&lt;p&gt;Step 3: Write the Python Script&lt;br&gt;
Now let’s create a Python script that uploads a file to your S3 bucket.&lt;/p&gt;

&lt;p&gt;python&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;br&gt;
import boto3&lt;br&gt;
from botocore.exceptions import NoCredentialsError&lt;/p&gt;

&lt;p&gt;def upload_to_s3(file_name, bucket, object_name=None):&lt;br&gt;
    """&lt;br&gt;
    Upload a file to an S3 bucket.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:param file_name: Name of the file to upload
:param bucket: S3 bucket name
:param object_name: S3 object name (optional)
:return: None
"""
s3 = boto3.client('s3')
try:
    s3.upload_file(file_name, bucket, object_name or file_name)
    print(f"File '{file_name}' successfully uploaded to bucket '{bucket}'")
except FileNotFoundError:
    print(f"Error: The file '{file_name}' was not found.")
except NoCredentialsError:
    print("Error: AWS credentials not available.")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Step 4: Run the Script&lt;br&gt;
Replace the placeholder values (e.g., file_name and bucket) in the script:&lt;/p&gt;

&lt;p&gt;python&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;br&gt;
upload_to_s3('example.txt', 'your-s3-bucket-name')&lt;br&gt;
Save the script as upload_to_s3.py.&lt;/p&gt;

&lt;p&gt;Run the script in your terminal:&lt;/p&gt;

&lt;p&gt;bash&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;br&gt;
python upload_to_s3.py&lt;br&gt;
If everything is configured correctly, the file will be uploaded to your specified S3 bucket. 🎉&lt;/p&gt;

&lt;p&gt;Lessons Learned&lt;br&gt;
Handle Errors Gracefully&lt;br&gt;
Issues like missing files or incorrect credentials are common when working with AWS. Adding proper error handling ensures your script is robust and user-friendly.&lt;/p&gt;

&lt;p&gt;The Power of S3&lt;br&gt;
S3 is not just for file storage—it can also host static websites, serve as a data lake, or act as the backbone of a disaster recovery plan.&lt;/p&gt;

&lt;p&gt;Ease of Use with boto3&lt;br&gt;
Amazon’s SDK makes interacting with AWS services much easier. With just a few lines of Python, you can achieve powerful results.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
Uploading files to AWS S3 is a fundamental task that opens the door to many possibilities in cloud computing. Using Python and boto3, you’ve taken your first step toward mastering AWS.&lt;/p&gt;

&lt;p&gt;Try this out, and let me know if you have questions or ideas for expanding this script. Happy coding!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
