Code Monkey home page Code Monkey logo

Comments (3)

Josue10599 avatar Josue10599 commented on June 19, 2024 2

I found out how to solve this problem, two fields must be added, one in Policy and the other in Body, looking like this:

final Dio _client = Dio();
_client.interceptors.add(LogInterceptor());
final int length = await image.length();
final String filename = '${DateTime.now().millisecondsSinceEpoch}.jpg';
final String key = path + filename;
final Policy policy = Policy.fromS3PreSignedPost(
  key,
  bucketId,
  500,
  accessKey,
  length,
  sessionToken,
  region: region,
);
final List<int> signKey =
    SigV4.calculateSigningKey(secretKey, policy.datetime, region, service);
final String signature = SigV4.calculateSignature(signKey, policy.encode());
final FormData formData = FormData.fromMap({
  'key': policy.key,
  'acl': 'public-read',
  'X-Amz-Algorithm': 'AWS4-HMAC-SHA256',
  'X-Amz-Credential': policy.credential,
  'X-Amz-Date': policy.datetime,
  'Policy': policy.encode(),
  'X-Amz-Signature': signature,
  'x-amz-security-token': sessionToken,
  'Content-Type': 'image/jpeg',
  'file': await MultipartFile.fromFile(
    image.path,
    filename: filename,
  )
});
Response response = await _client.post(post, data: formData);
_client.close();
String linkImage = response.headers.map['location'].first;
class Policy {
  String expiration;
  String region;
  String bucket;
  String key;
  String credential;
  String datetime;
  String sessionToken;
  int maxFileSize;

  Policy(this.key, this.bucket, this.datetime, this.expiration, this.credential,
      this.maxFileSize, this.sessionToken,
      {this.region = AWS_REGION_FTALK});

  factory Policy.fromS3PreSignedPost(
    String key,
    String bucket,
    int expiryMinutes,
    String accessKeyId,
    int maxFileSize,
    String sessionToken, {
    String region,
  }) {
    final datetime = SigV4.generateDatetime();
    final expiration = (DateTime.now())
        .add(Duration(minutes: expiryMinutes))
        .toUtc()
        .toString()
        .split(' ')
        .join('T');
    final cred =
        '$accessKeyId/${SigV4.buildCredentialScope(datetime, region, 's3')}';
    final policy = Policy(
        key, bucket, datetime, expiration, cred, maxFileSize, sessionToken,
        region: region);
    return policy;
  }

  String encode() {
    final bytes = utf8.encode(toString());
    return base64.encode(bytes);
  }

  @override
  String toString() {
    return '''
{ "expiration": "${this.expiration}",
  "conditions": [
    {"bucket": "${this.bucket}"},
    ["starts-with", "\$key", "${this.key}"],
    ["starts-with", "\$Content-Type", "image/"],
    {"acl": "public-read"},
    ["content-length-range", 1, ${this.maxFileSize}],
    {"x-amz-credential": "${this.credential}"},
    {"x-amz-algorithm": "AWS4-HMAC-SHA256"},
    {"x-amz-date": "${this.datetime}" },
    {"x-amz-security-token": "${this.sessionToken}" }
  ]
}
''';
  }
}

from amazon-cognito-identity-dart-2.

furaiev avatar furaiev commented on June 19, 2024

Pls take a look at this working code, it seems like this is the same as you trying to implement
https://github.com/conghua2411/FlutterDemoEverything/blob/e07cd5e1c9f751395a050a9a37ed09e821ee607b/lib/aws_cognito_dev_upload_s3/aws_cognito_dev_upload_s3.dart

from amazon-cognito-identity-dart-2.

Josue10599 avatar Josue10599 commented on June 19, 2024

Por favor, dê uma olhada neste código funcional, parece que é o mesmo que você está tentando implementar
https://github.com/conghua2411/FlutterDemoEverything/blob/e07cd5e1c9f751395a050a9a37ed09e821ee607b/lib/aws_cognito_dev_upload_s3/aws_cognito_dev_upload_s3/aws_cognito_dev_upload_s3/aws_cognito

Unfortunately it had the same effect, when uploading the image and opening it on AWS it looks like this:
Captura de Tela 2020-02-28 às 08 27 26

I need metadata to look like this:
Captura de Tela 2020-02-28 às 08 30 45

I have the need to change the metadata, because when opening the link, the image is downloaded automatically, and instead I need the image to be opened when clicking on the link, due to my web application that also includes the images. If you have any suggestions on what to do, I will be very grateful!

from amazon-cognito-identity-dart-2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.