amplify-flutter icon indicating copy to clipboard operation
amplify-flutter copied to clipboard

Failed to upload csv/json file to aws s3 through flutter app

Open rubana28 opened this issue 5 years ago • 9 comments

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 avatar Dec 16 '20 20:12 rubana28

@rubana28 check out amplify_storage_s3 There is an upload Example with a "File".

Rayv1 avatar Dec 16 '20 21:12 Rayv1

@rubana28 check out amplify_storage_s3 There is an upload Example with a "File".

rubana28 avatar Dec 16 '20 21:12 rubana28

@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?

rubana28 avatar Dec 16 '20 23:12 rubana28

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.

kjones avatar Dec 16 '20 23:12 kjones

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.

rubana28 avatar Dec 17 '20 01:12 rubana28

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 avatar Dec 17 '20 01:12 kjones

@kjones and then the local file has to be uploaded according to https://pub.dev/packages/amplify_storage_s3/example example?

rubana28 avatar Dec 17 '20 02:12 rubana28

@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 avatar Dec 17 '20 03:12 kjones

@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.

rubana28 avatar Dec 17 '20 19:12 rubana28