Flutter 将图像加载到 S3 存储桶

use*_*999 1 amazon-s3 amazon-web-services dart flutter

我想使用 AWS 预签名密钥将图像上传到 S3 存储桶。此 curl 命令有效:

curl -X PUT --upload-file file.txt "签名密钥 (URL)"

我如何通过 dart / flutter 实现这一目标?

小智 5

如果您使用 Multipart Form post,则 HTTP 请求的 http 边界部分存储在 S3 中文件的第一个字节中。别。

此功能有效:

  Future<void> uploadImage(File imageFile, String url, String imagetype) async {
    final length = await imageFile.length();
    final path = imageFile.path;
    print('Uploading image length: $length path:$path to url:$url');
    try {
      var response = await http.put(url, body: imageFile.readAsBytesSync(), headers: {"Content-Type": "image/" + imagetype});
      print("Uploading image status code: ${response.statusCode}");
      print("Uploading image result: ${response.body}");
      return;
    } catch (error) {
      print('Error uploading:' + error.toString());
      throw error;
    }
  }  
Run Code Online (Sandbox Code Playgroud)