Amazon web services AWS AppSync、Cognito&;S3
我想使用AWS AppSync上传图片。下面是我在这里看到的配置appsync应用程序的方法 模式:Amazon web services AWS AppSync、Cognito&;S3,amazon-web-services,amazon-s3,aws-appsync,aws-amplify,Amazon Web Services,Amazon S3,Aws Appsync,Aws Amplify,我想使用AWS AppSync上传图片。下面是我在这里看到的配置appsync应用程序的方法 模式: input PictureInput { ede_number: String! file: S3ObjectInput! } enum Visibility { public private } type S3Object { bucket: String! region: String! key: String! } input S3ObjectInput { buck
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本机应用程序中,以下是如何配置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 bucket中没有添加任何内容:(
当我尝试这个的时候
Auth.currentCredentials().then(cred => console.log(cred));
控制台中有以下错误消息:
"cannot get guest credentials when mandatory signin enabled"
我已经将我的Cognoto用户添加到一个具有S3完全访问权限的arn角色的组中
我读过这个例子:
我不知道我的错误在哪里
谢谢,该评论大致来源于您的“mandatorySignIn:true”标志。这需要在客户端尝试获取凭据时提供某种形式的登录令牌。我有两个后续问题:1)您是否在没有先登录的情况下,使用Amplify与任何其他AWS服务进行对话?2) 如果没有,您是否创建了您正在使用Cognito的AdminCreateUser API的用户?如果您登录到他们的控制台并查看用户页面,您可以看到用户状态。如果您看到该用户强制更改密码,则可能导致此情况。该用户未处于强制更改密码状态。我将深入学习aws代码。我有以下问题:Auth.js:1172{error:error:ParamValidator.fail(blob:…})的params中缺少必需的键'IdentityPoolId'