How to upload image to s3 bucket in flutter
Posted By : Saurabh Singh | 30-Nov-2022
About flutter:
Flutter is an open-source UI software development kit (SDK) that was primarily created by Google. It is used to develop cross-platform applications for Android, iOS, Linux, macOS, Windows, and the web from a single codebase.
1. About multipart request.
Using the HTTP package in flutter, we will able to send only string, int, list,date and double data-types. If you want to send images/videos/files to the server then we use multipart requests for that.
2. Code.
2.1)Creating Project in Flutter
2.1.1) open android studio and click on file > new >new flutter project
After that you need to provide some basic details about your project like project name, description organization, etc then hit the finish button. It will create a new project in Flutter.
2.1.2) open new pubspec.yaml file and install plugin(required).
2.1.3) http package
2.1)code for Multipart Request
uploadToS3 (image)async{
Try{
var postUri = Uri.parse("${postImageToS3}"); /// url
var request = http.MultipartRequest("POST", postUri);
Map<String, String> headers = { "access-token": "$ {dataStorage.read("authToken")}"}; /// header
request.headers.addAll(headers);
request.files.add(
await http.MultipartFile.fromPath('file', image)); /// file that you need to send s3
var response = await request.send();
var responsed = await http.Response.fromStream(response);
final responseData = json.decode(responsed.body);
if (response.statusCode == 200) {
/// if response.statuscode is 200 then you will get a image id from aws
var imagedetails= jsonDecode(responsed.body);
/// s3 image id
print(imagedetails["imageId"]);
}
else if (response.statusCode == 401) {
Get.off(() => LoginScreen());
}
else if (response.statusCode == 400) {
log("errorCode ${response.statusCode}");
Get.rawSnackbar(
message: "Error in upLoading Image",
snackPosition: SnackPosition.BOTTOM,
snackStyle: SnackStyle.GROUNDED,
backgroundColor: Colors.red
);
}
else if (response.statusCode == 500) {
Get.rawSnackbar(
message: "Something Went Wrong from Server",
snackPosition: SnackPosition.BOTTOM,
margin: EdgeInsets.zero,
snackStyle: SnackStyle.GROUNDED,
backgroundColor: Colors.red);
}
} on SocketException {
Get.rawSnackbar(
message: "Internet Exception",
snackPosition: SnackPosition.BOTTOM,
margin: EdgeInsets.zero,
snackStyle: SnackStyle.GROUNDED,
backgroundColor: Colors.red);
} catch (e) {
Get.rawSnackbar(
message: " $e Error Occured",
snackPosition: SnackPosition.BOTTOM,
margin: EdgeInsets.zero,
snackStyle: SnackStyle.GROUNDED,
backgroundColor: Colors.red);
} finally {
}
}
/// simple pass image to above method then it will allow you to upload image to s3 then get image id from aws on 200 response
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Saurabh Singh
Saurabh is working as a Mobile Application Developer with an overall 2+ years of experience. His areas of expertise are Flutter and Android Development. He is a detail-oriented person with good problem-solving skills.