Failed to upload csv/json file to aws s3 through flutter app
So, I am developing an app using flutter sdk in android studio which will capture the motion sensor data and geolocator data. These data will be stored into a file (csv/ json/ bin) and that file will be uploaded into that specific bucket every 1 hour through my flutter app.
I have tried to upload data into S3 bucket using http class but this is not working. Again, everywhere there are many resources for uploading images. There are less resources for uploading file. I have also tried to use aws lambda but all tutorials are about uploading images.
Can anyone suggest me any way/tutorial/documentation about how can I upload files through my flutter to AWS S3 bucket? A code snippet is given below about how I am trying to upload data into s3 bucket.
`
DatabaseHelper db = DatabaseHelper();
static const String _csvHeader = "Accelerometer, UserAccelerometer, Gyroscope, latitude-longitude\n";
List entries = [];
addEntry() async{
var res = await db.queryGeoAll(); //to get all data of geolocator from database
var geoCSV = mapListToCsv(res); //converting them into a csv file
entries.add("$accelerometer, $userAccelerometer, $gyroscope, $geoCSV");
}
clearEntries() {
accelerometer= null;
userAccelerometer= null;
gyroscope= null;
latitudeData= null;
location = null;
entries.clear();
}
void uploadSensor() async{
String csvContent = entries.join("\n");
print(csvContent);
Uri uri = Uri.parse("Bucket Adress");
http.MultipartRequest request = new http.MultipartRequest("POST", uri);
request.files.add(new http.MultipartFile.fromString("csv", _csvHeader + csvContent));
try {
return await request.send().then((response) {
if (response.statusCode == 200) {
clearEntries();
return true;
} else {
return false;
}
});
} catch (e) {
print(e.toString());
//return false;
}
}`
Here, I am getting an error like:
I/flutter ( 394): SocketException: Failed host lookup: 's3.us-east-1.aws.amazonaws.com' (OS Error: No address associated with hostname, errno = 7)
I have already added the following line to Android Manifest file
<uses-permission android:name="android.permission.INTERNET"/>
@rubana28 check out amplify_storage_s3 There is an upload Example with a "File".
@rubana28 check out amplify_storage_s3 There is an upload Example with a "File".
@Rayv1 According to the documentation, I can upload any file which is already inside my mobile. But what I am doing in my app is I am capturing the motion sensor's data, then storing them into a list and finally, after converting them into multipartFile, I am uploading the file into S3 bucket according to the following line.
` Uri uri = Uri.parse("Bucket Adress");
http.MultipartRequest request = new http.MultipartRequest("POST", uri);
request.files.add(new http.MultipartFile.fromString("csv", _csvHeader + csvContent));`
is this a way to upload file or do I have to follow other way?
Is this a bucket managed by Amplify or is the bucket created and managed externally? If you are managing the bucket using Amplify then @Rayv1's suggestion and using the Flutter AWS SDK is the way to go.
Otherwise, S3 does support a REST API but you'll have to follow all of the instructions listed there.
Is this a bucket managed by Amplify or is the bucket created and managed externally? If you are managing the bucket using Amplify then @Rayv1's suggestion and using the Flutter AWS SDK is the way to go.
Otherwise, S3 does support a REST API but you'll have to follow all of the instructions listed there.
My bucket is managed by Amplify. Thank you for the clarification.
It does look like you will have to write your csvContent to a local file before using the Amplify Flutter SDK to upload it. https://flutter.dev/docs/cookbook/persistence/reading-writing-files
@kjones and then the local file has to be uploaded according to https://pub.dev/packages/amplify_storage_s3/example example?
@kjones and then the local file has to be uploaded according to https://pub.dev/packages/amplify_storage_s3/example example?
That is correct. A smaller snippet from the Amplify docs: https://docs.amplify.aws/lib/storage/upload/q/platform/flutter
@kjones and then the local file has to be uploaded according to https://pub.dev/packages/amplify_storage_s3/example example?
That is correct. A smaller snippet from the Amplify docs: https://docs.amplify.aws/lib/storage/upload/q/platform/flutter
Thank you @kjones for the clarification.