4

我想使用 AWS AppSync 上传图片。按照我在这里看到的是我如何配置我的 appsync 应用程序。

架构:

input PictureInput {
 ede_number: String!
 file: S3ObjectInput!
}

enum Visibility {
 public
 private
}

type S3Object {
 bucket: String!
 region: String!
 key: String!
}

input S3ObjectInput {
 bucket: String!
 region: String!
 localUri: String
 visibility: Visibility
 key: String
 mimeType: String
}

type Picture {
 picture_id: String!
 ede_number: String!
 file: S3Object
}

type Mutation {
 addPicture(picture: PictureInput!): Picture
}

映射解析器:

{
 "version": "2017-02-28",
 "operation": "PutItem",
 "key": {
    "picture_id":  { "S" : "${util.autoId()}"}
  },
 #set( $attribs = $util.dynamodb.toMapValues($ctx.args.picture) )
 #set( $attribs.author =   $util.dynamodb.toString($context.identity.username) )
 #set( $attribs.company = $util.dynamodb.toString($context.identity.claims.get("custom:company")) )
 #set( $file = $context.args.picture.file )
 #set( $attribs.file = $util.dynamodb.toS3Object($file.key, $file.bucket, $file.region) )
 #set( $attribs.created_at = $util.dynamodb.toString($util.time.nowISO8601()) )

 "attributeValues": $util.toJson($attribs)
}

在我的 react native 应用程序中,以下是配置 AWS AppSync 客户端的方法:

Amplify.configure({
 Auth: {
   mandatorySignIn: true,
   region: config.REGION,
   userPoolId: config.COGNITO_GROUP_ID,
   identityPoolId: config.COGNITO_IDENTITY_ID,
   userPoolWebClientId: config.COGNITO_CLIENT_ID,
 },
});

const client = new AWSAppSyncClient({
 url: config.APPSYNC_URL,
 region: config.REGION,
 auth: {
   type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS,
   jwtToken: async () =>
   (await Auth.currentSession()).getIdToken().getJwtToken(),
   credentials: () => Auth.currentCredentials(),
},
complexObjectsCredentials: () => Auth.currentCredentials(),
});

最后,这就是我进行突变的方式:

  let pickerResult = await ImagePicker.launchCameraAsync({
    aspect: [4, 3],
  });

   const picture = {
    ede_number: farm.ede_number,
    file: {
      localUri: pickerResult.uri,
      bucket: config.BUCKET,
      key: uuid(),
      region: config.REGION,
    },
  };

  addPicture({
    variables: {
      picture,
    },
    optimisticResponse: {
      __typename: 'Mutation',
      addPicture: {
        __typename: 'Picture',
        ...picture,
        file: {
          __typename: 'S3Object',
          ...picture.file,
        },
      },
    },
  });

在 DynamoDb 表中添加了一个条目,但在 s3 存储桶中没有任何内容:(

当我尝试这个

Auth.currentCredentials().then(cred => console.log(cred));

我在控制台中有以下错误消息:

"cannot get guest credentials when mandatory signin enabled"

我已将我的 cognito 用户添加到具有 S3 完全访问权限的 arn 角色的组中。

我读过这个例子:https ://github.com/aws-samples/aws-amplify-graphql

而且我看不出我的错误在哪里。

谢谢,

4

0 回答 0