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)
}

私の反応ネイティブアプリでは、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"

S3 フルアクセス権を持つ arn ロールを持つグループに自分の cognito ユーザーを追加しました。

この例を読みました: https://github.com/aws-samples/aws-amplify-graphql

そして、どこが私の間違いなのかわかりません。

ありがとう、

4

0 に答える 0