在AppSync中,当您使用Cognito用户池作为身份验证时,您可以获得自己的身份
identity:
{ sub: 'bcb5cd53-315a-40df-a41b-1db02a4c1bd9',
issuer: 'https://cognito-idp.us-west-2.amazonaws.com/us-west-2_oicu812',
username: 'skillet',
claims:
{ sub: 'bcb5cd53-315a-40df-a41b-1db02a4c1bd9',
aud: '7re1oap5fhm3ngpje9r81vgpoe',
email_verified: true,
event_id: 'bb65ba5d-4689-11e8-bee7-2d0da8da81ab',
token_use: 'id',
auth_time: 1524441800,
iss: 'https://cognito-idp.us-west-2.amazonaws.com/us-west-2_oicu812',
'cognito:username': 'skillet',
exp: 1524459387,
iat: 1524455787,
email: 'myemail@nope.com' },
sourceIp: [ '11.222.33.200' ],
defaultAuthStrategy: 'ALLOW',
groups: null }
Run Code Online (Sandbox Code Playgroud)
但是,当您使用AWS_IAM auth时,您会得到
identity:
{ accountId: '12121212121', //<--- my amazon account ID
cognitoIdentityPoolId: 'us-west-2:39b1f3e4-330e-40f6-b738-266682302b59',
cognitoIdentityId: 'us-west-2:a458498b-b1ac-46c1-9c5e-bf932bad0d95',
sourceIp: [ '33.222.11.200' ],
username: 'AROAJGBZT5A433EVW6O3Q:CognitoIdentityCredentials',
userArn: 'arn:aws:sts::454227793445:assumed-role/MEMORYCARDS-CognitoAuthorizedRole-dev/CognitoIdentityCredentials',
cognitoIdentityAuthType: 'authenticated',
cognitoIdentityAuthProvider: '"cognito-idp.us-west-2.amazonaws.com/us-west-2_HighBob","cognito-idp.us-west-2.amazonaws.com/us-west-2_HighBob:CognitoSignIn:1a072f08-5c61-4c89-807e-417d22702eb7"' }
Run Code Online (Sandbox Code Playgroud)
文档说这是预期的,https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference.html.但是,如果您使用AWS_IAM
连接到Cognito(需要具有未经身份验证的访问权限),您应该如何获得用户的用户名,电子邮件,子邮件等?使用AWS_IAM
Auth类型时,我需要访问用户的声明.
我正在使用Node来获取S3的presignedRUL以便将图像放入S3存储桶。
var aws = require('aws-sdk');
// Request presigned URL from S3
exports.S3presignedURL = function (req, res) {
var s3 = new aws.S3();
var params = {
Bucket: process.env.S3_BUCKET,
Key: '123456', //TODO: creat unique S3 key
//ACL:'public-read',
ContentType: req.body['Content-Type'], //'image/jpg'
};
s3.getSignedUrl('putObject', params, function(err, url) {
if(err) console.log(err);
res.json({url: url});
});
};
Run Code Online (Sandbox Code Playgroud)
这将成功检索以下形式的预签名网址:
回到客户端(Web应用程序),我使用angular来生成HTTP请求。我曾经用过$ http和ngFileUpload,但都缺少类似的成功。这是我的ngFileUpload代码。
Upload.upload({
url: responce.data.url, //S3 upload url including bucket name
method: 'PUT',
'Content-Type': file.type, //I …
Run Code Online (Sandbox Code Playgroud) amazon-s3 http-status-code-403 angularjs pre-signed-url ng-file-upload
想象一下,你有一个aws资源,如
Resources:
IdentityPool:
Type: "AWS::Cognito::IdentityPool"
Properties:
IdentityPoolName: ${self:custom.appName}_${self:provider.stage}_identity
CognitoIdentityProviders:
- ClientId:
Ref: UserPoolClient
Run Code Online (Sandbox Code Playgroud)
"AWS :: Cognito :: IdentityPool"的Ref返回此资源的id.现在假设我想在多行字符串中引用该id.我试过了
Outputs:
AmplifyConfig:
Description: key/values to be passed to Amplify.configure(config);
Value: |
{
'aws_cognito_identity_pool_id': ${Ref: IdentityPool}, ##<------ Error
'aws_sign_in_enabled': 'enable',
'aws_user_pools_mfa_type': 'OFF',
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用Fn:Sub但没有运气.
AmplifyConfig:
Description: key/values to be passed to Amplify.configure(config);
Value:
Fn::Sub
- |
{
'aws_cognito_identity_pool_id': '${Var1Name}',
'aws_sign_in_enabled': 'enable',
}
- Var1Name:
Ref: IdentityPool
Run Code Online (Sandbox Code Playgroud)
有什么办法吗?
我对亚马逊的app-sync的离线和同步功能感兴趣,但我想知道它是否可以在没有dynamoDB作为后端的情况下使用.用VTL编写的适用于dynamoDB的graphQL解析器看起来很糟糕.看起来使用mongo后端会好得多.那可能吗?
我正在使用AWS Amplify库为AppSync项目注册并执行Auth.这使用Cognito.但是,当新用户通过Amplify/Cognito注册时,新用户不会分配到cognito池中的任何特定组.我使用Amplify高阶组件进行登录/注册.
import { withAuthenticator } from 'aws-amplify-react';
Run Code Online (Sandbox Code Playgroud)
我将一个组件包裹起来
class Authenticator extends React.Component {
//... basically empty component, only exists so I can wrap it w/ the HOC
}
export default withAuthenticator(Authenticator)
Run Code Online (Sandbox Code Playgroud)
Amplify在index.js中设置
import config from './aws-exports';
import Amplify from 'aws-amplify';
Amplify.configure(config);
Run Code Online (Sandbox Code Playgroud)
aws-exports.js由AWS Mobile Hub CLI自动生成.好像...
const awsmobile = {
'aws_app_analytics': 'enable',
'aws_cognito_identity_pool_id': 'us-west-2:XXX',
'aws_cognito_region': 'us-west-2',
'aws_content_delivery': 'enable',
'aws_content_delivery_bucket': 'flashcards-hosting-mobilehub-XXX',
'aws_content_delivery_bucket_region': 'us-west-2',
'aws_content_delivery_cloudfront': 'enable',
'aws_content_delivery_cloudfront_domain': 'XXX.cloudfront.net',
'aws_mandatory_sign_in': 'enable',
'aws_mobile_analytics_app_id': 'XXX',
'aws_mobile_analytics_app_region': 'us-east-1',
'aws_project_id': 'XXX',
'aws_project_name': 'flash-cards',
'aws_project_region': 'us-west-2',
'aws_resource_name_prefix': 'flashcards-mobilehub-XXX', …
Run Code Online (Sandbox Code Playgroud) amazon-web-services aws-mobilehub aws-cognito aws-appsync aws-amplify
我正在尝试在 axios 中为后端实现一个前端,该后端在curl 中给出了前端示例。下面的 axios 等价物是什么?我不确定如何表示 -u 标志。
curl -X POST http://0.0.0.0:9000/auth -i -u test@example.com:123456
Run Code Online (Sandbox Code Playgroud) 在这个问题的答案的基础上 ,Mongoose中的嵌套数组
我想知道是否有一种方法可以在mongoose模式中使用一个数组,该模式只接受两种类型的模式定义文档中的一种作为数组中的元素.这是什么意思?考虑定义为......的模式
var MyDocSchema = new Schema({
name: String,
stuff: [Stuff],
});
Run Code Online (Sandbox Code Playgroud)
把东西定义为
var Stuff = new Schema({
name: String,
value: Number,
});
Run Code Online (Sandbox Code Playgroud)
这有效,但如果我想要我的东西:数组能够包含Stuff或OtherStuff ....
var MyDocSchema = new Schema({
name: String,
stuff: [Stuff *or* OtherStuff ],
});
Run Code Online (Sandbox Code Playgroud)
其中Stuff与之前相同,OtherStuff定义为
var OtherStuff = new Schema({
count: Number,
when: Date,
});
Run Code Online (Sandbox Code Playgroud) 我只是用自制软件安装golang,我在导入远程软件包时遇到问题.
当我尝试安装包含的demo.go时
import "github.com/bradfitz/gomemcache/memcache"
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
$ go install
demo.go:3:8: cannot find package "github.com/bradfitz/gomemcache/memcache" in any of:
/usr/local/Cellar/go/1.4/libexec/src/github.com/bradfitz/gomemcache/memcache (from $GOROOT)
/Users/white/go/src/github.com/bradfitz/gomemcache/memcache (from $GOPATH)
Run Code Online (Sandbox Code Playgroud)
对于我未经训练的眼睛,看起来它只是在我的GOPATH上看当地.
假设我有一个 mongoDB 数据库,我在其中创建了一个属于用户的文档,其中文档和用户都存储在数据库中。
让我们进一步说用户对象包含对文档的引用。
创建一个新文档可能看起来像这样。
exports.create = function (req, res, next) {
//make a new document
var newDoc = new Document({
title: req.body.title,
content: req.body.content,
});
User.findById(req.user._id, function (err, user) {
if (err)
return res.send(400);
user.documents.push(newDocument._id);
user.save(function(err){
if (err)
return res.json(400, err);
newDoc.save(function(err) {
if (err)
return res.json(400, err); //<--- What if you have an error here??
return res.json(200, {document:'successfully created'});
});
});
});
};
Run Code Online (Sandbox Code Playgroud)
如果您在最后一个 if(err) 出现错误,您将向用户添加对新文档的引用,但不会创建该文档。
当然,这是一个非常简单的案例。大量相互引用的文档可能会变得更加复杂。在 Mongo/Mongoose 中处理这个最干净的方法是什么?
使用我尝试运行的 AWS CLI
aws cloudformation create-stack --stack-name FullstackLambda --template-url https://s3-us-west-2.amazonaws.com/awsappsync/resources/lambda/LambdaCFTemplate.yam --capabilities CAPABILITY_NAMED_IAM --region us-west-2
Run Code Online (Sandbox Code Playgroud)
但我得到了错误
An error occurred (ValidationError) when calling the CreateStack operation: S3 error: Access Denied
Run Code Online (Sandbox Code Playgroud)
我已经设置了我的凭证
aws configure
Run Code Online (Sandbox Code Playgroud)
PS我从AppSync文档中获得了create-stack命令(https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-lambda-resolvers.html)
我在使用javascript将svg绘制到画布上时遇到了一些麻烦.我希望这可以工作...... HTML
<canvas class="buttonCanvas" id="handCanvas" height="60px" width="60px" />
Run Code Online (Sandbox Code Playgroud)
使用Javascript
function drawHandCanvas(){
var canvas = document.getElementById('handCanvas');
var ctx = canvas.getContext('2d');
var img= document.createElement('img');
img.src='images/handCursor.svg';
img.width = 60; img.height = 60;
ctx.drawImage(img,0,0);
}
drawHandCanvas();
Run Code Online (Sandbox Code Playgroud)
但事实并非如此.
如果我添加一个SVG元素,我可以让它工作.HTML
<canvas class="buttonCanvas" id="handCanvas" height="60px" width="60px" />
<img id="handSVG" src="images/handCursor.svg"/>
Run Code Online (Sandbox Code Playgroud)
使用Javascript
function drawHandCanvas(){
var canvas = document.getElementById('handCanvas');
var ctx = canvas.getContext('2d');
var img=document.getElementById('handSVG');
img.width = 60; img.height = 60;
setTimeout(function(){
ctx.drawImage(img,0,0);
img.hidden='true';
}, 10);
}
drawHandCanvas();
Run Code Online (Sandbox Code Playgroud)
请注意,在第二种方法中,我必须使用setTimeout方法使其工作,并在我的向下结束时添加一个img元素,我在画布上绘制后隐藏它.超级hacky!如果我只使用window.onload而不是setTimeout,它就不起作用.它将隐藏img,但drawImage()不执行任何操作,可能是因为当窗口已经完成加载时画布尚未就绪.有什么想法吗?
从yml(或json)模板在AWS CloudFormation中创建资源时,是否可以在数组上进行迭代以使模板简短易读。例如,我有一个Appsync项目,在其中必须创建一堆几乎相同的AWS类型“ AWS :: AppSync :: Resolver”的解析器。在我用于Cloud Formation的YML模板中,1这样的资源可能看起来像这样
Resource:
GraphQlAddPostsResolver:
Type: "AWS::AppSync::Resolver"
DependsOn: GraphQlSchema
Properties:
ApiId:
Fn::GetAtt: [GraphQlApi, ApiId]
TypeName: Mutation #<---
FieldName: addPost #<----
DataSourceName:
Fn::GetAtt: [GraphQlLambdaDataSource, Name]
RequestMappingTemplate: |
{
"version" : "2017-02-28",
"operation": "Invoke",
"payload": {
"field": "addPost", #<---
"context": $util.toJson($context)
}
}
ResponseMappingTemplate: |
$util.toJson($context.result)
Run Code Online (Sandbox Code Playgroud)
我可能有十几个或更多这些解析器,唯一的区别是我用表示的地方<----
。有什么办法可以遍历一组值,比如
- Field: addPost
Type: Mutation
- Field: allPosts
Type: Query
- Field: getPost
Type: Query
## etc...
Run Code Online (Sandbox Code Playgroud) yaml resolver amazon-web-services aws-cloudformation aws-appsync