FlutterアプリはFirebaseをバックエンドとして使用していますが、メディアファイル(写真とビデオ)をs3バケットに保存する必要があります。ミッションは、イメージピッカーから取得したメディアをs3にアップロードし、URLを取得することです。URLは、Firebaseデータベースに文字列として保存できます。
Dart 2のawsライブラリまたはapiが不足していることが問題です。パブで3つ見つかりましたが、そのうち2つはDart 2と互換性がなく、1つは開発中でした。誰かがDart 2を使用してフラッターでこれを実装しましたか?どんな提案でも大歓迎です。ありがとうございました。
私が見つけたパッケージは(pub.dartlang.org)でした:aws_client、aws_interop、Amazon_s3
You can use package [Amazon_s3_cognito][1] to upload and delete the images to Amazon s3.
I am the author of the plugin and we are using this plugin successfully in many of our projects.
import 'package:Amazon_s3_cognito/Amazon_s3_cognito.Dart';
import 'package:Amazon_s3_cognito/aws_region.Dart';
String uploadedImageUrl = await AmazonS3Cognito.uploadImage(
_image.path, BUCKET_NAME, IDENTITY_POOL_ID);
//Use the below code to upload an image to Amazon s3 server
//I advise using this method for image upload.
String uploadedImageUrl = await AmazonS3Cognito.upload(
_image.path,
BUCKET_NAME,
IDENTITY_POOL_ID,
IMAGE_NAME,
AwsRegion.US_EAST_1,
AwsRegion.AP_SOUTHEAST_1)
_image.path = path of the image you want to upload (file.path method in flutter)
IMAGE_NAME = this image uploads to s3 server with the name you give here.
//use below code to delete an image
String result = AmazonS3Cognito.delete(
BUCKET_NAME,
IDENTITY_POOL_ID,
IMAGE_NAME,
AwsRegion.US_EAST_1,
AwsRegion.AP_SOUTHEAST_1)
For fetching images you could use [cached_network_image][2] package
The CachedNetworkImage can be used directly or through the ImageProvider.
CachedNetworkImage(
imageUrl: "http://via.placeholder.com/350x150",
placeholder: (context, url) => new CircularProgressIndicator(),
errorWidget: (context, url, error) => new Icon(Icons.error),
),`enter code here`
Future<String> _getFilePath(Asset asset,ListingImage listingImage) async{
try{
if(!isUploadCancelled){
// getting a directory path for saving
final directory = await getTemporaryDirectory();
String path = directory.path;
File file = File(path + "/temp_" + listingImage.index.toString() + "_"+DateTime.now().microsecondsSinceEpoch.toString());
listingImage.file = file;
file = await file.writeAsBytes( asset.imageData.buffer.asUint8List(asset.imageData.offsetInBytes, asset.imageData.lengthInBytes));
return file.path;
}else{
return null;
}
}catch(exceptioon){
return null;
}
}
[1]: https://pub.dev/packages/Amazon_s3_cognito
[2]: https://pub.dev/packages/cached_network_image
Flutter Multipart を使用できます。
// open a bytestream
var stream = new http.ByteStream(DelegatingStream.typed(_image.openRead()));
// get file length
var length = await _image.length();
// string to uri
var uri = Uri.parse(apiUrl);
// create multipart request
var request = new http.MultipartRequest("POST", uri);
NetworkUtils.addAuthHeaders(request);
// multipart that takes file
var multipartFile = new http.MultipartFile('file', stream, length,
filename: basename(_image.path),
contentType: new MediaType("image", "jpg"));
// add file to multipart
request.files.add(multipartFile);
request.fields.addAll(body);
// send
var response = await request.send();
print(response.statusCode);
// listen for response
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
}