IAM Part#3
Topics
Working with Access keys.
IAM Roles
What is the AWS CLI?
How to download and install CLI?
IAM Operation with CLI
Working with CLI in Linux Machine
Working with CLI in AWS Cloud shell.
Download the key
from flask import Flask, render_template, request, redirect, flash, url_for
import boto3
from botocore.exceptions import NoCredentialsError, PartialCredentialsError, ClientError
import os
app = Flask(__name__)
app.secret_key = 'your_secret_key'
# AWS S3 Configuration
S3_BUCKET = "ccitnewbucketcreated"
S3_REGION = "eu-west-1"
# Initialize S3 client
s3_client = boto3.client(
's3',
aws_access_key_id='your_access_key_id',
aws_secret_access_key='your_secret_access_key',
region_name=S3_REGION
)
# Check if the bucket exists, if not, create it
def create_bucket_if_not_exists():
try:
# Check if the bucket exists
s3_client.head_bucket(Bucket=S3_BUCKET)
except ClientError as e:
# If the bucket does not exist, create it
if e.response['Error']['Code'] == 'NoSuchBucket':
try:
s3_client.create_bucket(
Bucket=S3_BUCKET,
CreateBucketConfiguration={'LocationConstraint': S3_REGION}
)
flash(f"Bucket '{S3_BUCKET}' created successfully.")
except Exception as e:
flash(f"Error creating bucket: {e}")
else:
flash(f"Error accessing bucket: {e}")
# Home and File Upload Route
@app.route('/', methods=['GET', 'POST'])
def upload_file():
# Create the bucket if it doesn't exist
create_bucket_if_not_exists()
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part in the request')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
try:
s3_client.upload_fileobj(
file,
S3_BUCKET,
file.filename
)
flash(f"File '{file.filename}' uploaded successfully to S3.")
return redirect(url_for('upload_file'))
except NoCredentialsError:
flash("Credentials not available.")
except PartialCredentialsError:
flash("Incomplete credentials provided.")
except Exception as e:
flash(f"Error uploading file: {e}")
# List files in the bucket to display them on the page
files = []
try:
response = s3_client.list_objects_v2(Bucket=S3_BUCKET)
if 'Contents' in response:
files = [file['Key'] for file in response['Contents']]
except Exception as e:
flash(f"Error retrieving files: {e}")
return render_template('upload.html', files=files)
# Route to delete a file from S3
@app.route('/delete/<filename>', methods=['POST'])
def delete_file(filename):
try:
s3_client.delete_object(Bucket=S3_BUCKET, Key=filename)
flash(f"File '{filename}' deleted successfully from S3.")
except Exception as e:
flash(f"Error deleting file: {e}")
return redirect(url_for('upload_file'))
if __name__ == '__main__':
app.run(debug=True)
1. A role is a set of permission that grant access to actions and resources in AWS. These permissions are attached to the role,not to an IAM user or a group.
2. An IAM user can use a role in the same AWS account or a different account.
3. An IAM user is similar to an IAM user, role is also an AWS identity with permission policies that determine what the identity can and cannot do in aws.
4. A role is not uniquely associated with a single person,it can be used by anyone who needs it.
5. You can use the roles to delegate access to users,applications or services that generally do not have access to you aws resources
Step6: Now this code those two lines not required , it will come to Role based with out accesskey and secrete key.