我想使用 AWS 预签名 key 将图像上传到 S3 存储桶。这个 curl 命令有效:
curl -X PUT --upload-file file.txt "签名 key (URL)"
如何使用 dart/flutter 完成此操作?
最佳答案
如果您使用多部分表单发布,则 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;
}
}
关于amazon-web-services - Flutter 将图像加载到 S3 存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52048468/