Search code examples
flutterhttpamazon-s3multipart

MultipartRequest uploads successfully to s3, but data appears to be invalid


This seems like a pretty straight forward procedure in Flutter. But when I download the video I have uploaded to s3, I can not get the video to play back. Video player tells me the data is corrupt. The uploadUri is a presigned s3 link. And the filePath is the correct path to my video. I get a 200 response from the send.

  Future<void> postVideo( String filePath, String uploadUri ) async {
    var request = http.MultipartRequest('PUT', Uri.parse( uploadUri ) );
    request.files.add(
        await http.MultipartFile.fromPath(
            'video',
            filePath,
            contentType: MediaType('video','mp4')
    ));
    final response = await request.send();
    if (response.statusCode == 201 || response.statusCode == 200 ) {
      print( 'submit video response: ' + response.toString() );
    } else {
      throw Exception('Failed to post video');
    }
  }

Any thoughts?


Solution

  • here is what worked, not sure what the magic was..

    Future<void> postVideo( String filePath, String uploadUri ) async {
    File file = File(filePath);
    List<int> imageData = file.readAsBytesSync();
    
    var response = await http.put(uploadUri, body: imageData, headers: {
      "Content-Type": "octet-stream",
      "Content-Disposition": 'attachment; filename="resource"',
      "Content-Encoding": "identity",
      "Content-Length": file.lengthSync().toString()
    });
    
    if (response.statusCode == 201 || response.statusCode == 200 ) {
      print( 'submit video response: ' + response.toString() );
    } else {
      throw Exception('Failed to post story');
    }
    

    }