Flutter/Dart:自动将上传到S3存储桶的图像设置为"image/jpeg"? [英] Flutter/Dart: Automatically set images Uploaded to S3 Bucket as "image/jpeg"?

查看:85
本文介绍了Flutter/Dart:自动将上传到S3存储桶的图像设置为"image/jpeg"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我从Flutter应用程序将图像文件上传到存储桶时,该对象就会在元数据中自动显示为"binary/octet-stream".而不是"image/jpeg"我需要的类别.

Whenever I upload image files to my bucket from my Flutter app, the object automatically appears in metadata as "binary/octet-stream" rather than the "image/jpeg" category which I need.

我正在使用的代码如下:

The code I'm using looks like this;

String filename}) async {
    final endpoint = 'https://$bucket.s3-$region.amazonaws.com ';
    final uploadDest = '$destDir/${filename ?? path.basename(file.path)}';

    final stream = http.ByteStream(Stream.castFrom(file.openRead()));
    
    final length = await file.length();

    final uri = Uri.parse(endpoint);
    final req = http.MultipartRequest("POST", uri);
    final multipartFile = http.MultipartFile('file', stream, length,
        filename: path.basename(file.path)
        );

    final policy = Policy.fromS3PresignedPost(uploadDest, bucket, accessKey, 15, length, region: region);
    final key = SigV4.calculateSigningKey(secretKey, policy.datetime, region, 's3');
    final signature = SigV4.calculateSignature(key, policy.encode());

    req.files.add(multipartFile);
    req.fields['key'] = policy.key;
    req.fields['acl'] = 'public-read';
    req.fields['X-Amz-Credential'] = policy.credential;
    req.fields['X-Amz-Algorithm'] = 'AWS4-HMAC-SHA256';
    req.fields['X-Amz-Date'] = policy.datetime;
    req.fields['Policy'] = policy.encode();
    req.fields['X-Amz-Signature'] = signature;

如何获取自动以"image/jpeg"格式上传的照片?以便可以在浏览器中查看它们,而不是将其下载到我的桌​​面上?

How do I get the photos automatically uploaded as "image/jpeg" so that they can be viewed in a browser instead of downloaded to my desktop?

推荐答案

您可以附加媒体 contentType:new MediaType('image','jpeg')类型,如下所示

You can append a media contentType: new MediaType('image', 'jpeg') type as shown below

new http.MultipartFile.fromBytes('file', await File.fromUri("<path/to/file>").readAsBytes(), contentType: new MediaType('image', 'jpeg'))

别忘了导入 import'package:http_parser/http_parser.dart';

这篇关于Flutter/Dart:自动将上传到S3存储桶的图像设置为"image/jpeg"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆