Amazon S3 Simple Storage in Flutter

Amazon S3 (Simple Storage Service) is a cloud storage service offered by Amazon Web Services (AWS), allowing you to store and retrieve files. This documentation provides a step-by-step guide on how to integrate and use Amazon S3 storage in your Flutter app.


1. Prerequisites

1.AWS account with Amazon S3 access.

2.Flutter development environment set up.

2. Setting Up Amazon S3

1.Create an Amazon S3 Bucket:

.Log in to the AWS Management Console
.Create a new S3 bucket to store your files.

2.Set Up AWS Credentials:

.Create an IAM user with appropriate S3 permissions.
.Generate AWS access keys (Access Key ID and Secret Access Key).

Installing Dependencies

In your pubspec.yaml file, add the aws_s3 package:

aws_s3: ^0.3.1

Uploading Files to S3

Initialize AWS Configuration:

    import 'package:aws_s3/aws_s3.dart';

        final awsS3 = AwsS3(
          aws: Aws(
            region: 'YOUR_AWS_REGION',
            accessKey: 'YOUR_ACCESS_KEY',
            secretKey: 'YOUR_SECRET_KEY',
          ),
          bucketName: 'YOUR_BUCKET_NAME',
        );
        
    

Upload a File to S3:

    Future uploadFileToS3(String filePath, String s3ObjectName) async {
        try {
          await awsS3.uploadFile(
            filePath: filePath,
            s3ObjectName: s3ObjectName,
          );
          print('File uploaded successfully');
        } catch (e) {
          print('Error uploading file: $e');
        }
      }
        
    

Downloading Files from S3

    Future downloadFileFromS3(String s3ObjectName, String localFilePath) async {
        try {
          await awsS3.downloadFile(
            s3ObjectName: s3ObjectName,
            localFilePath: localFilePath,
          );
          print('File downloaded successfully');
        } catch (e) {
          print('Error downloading file: $e');
        }
      }