Mon application Flutter utilise Firebase comme backend mais j'ai besoin de stocker des fichiers multimédias (photos et vidéos) dans mon compartiment s3. La mission est de télécharger les médias récupérés du sélecteur d'images dans s3 et de récupérer l'URL, qui peut ensuite être stockée sous forme de chaîne dans ma base de données Firebase.
Le problème est la rareté des bibliothèques aws ou api pour Dart 2. J'en ai trouvé 3 en pub, mais 2 d'entre elles étaient incompatibles avec Dart 2 & 1 était en cours de développement. Quelqu'un a-t-il implémenté cela en flutter en utilisant Dart 2? Toutes suggestions sont les bienvenues. Je vous remercie.
Les packages que j'ai trouvés étaient (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
Vous pouvez utiliser Flutter Multipart , quelque chose comme ça
// 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);
});
}