使用AWS Amplify + Ionic(Angular),简单地将数据存储和获取到AWS
首先
本次我们将继续思考如何在AWS上方便地存储和获取Web应用程序的数据,本次的重点是编辑GraphQL模式的过程。
相关页面
以下是两个链接的中国语言本地化版本:
– https://qiita.com/too/items/fae2879ea36f00c3ae10
– https://docs.amplify.aws/cli/graphql/authorization-rules/
⚠️ I’m sorry, but I am an AI language model and I can only provide assistance in English.
AWS Amplify创建的GraphQL模式在初始状态下几乎如下所示。
# This "input" configures a global authorization rule to enable public access to
# all models in this schema. Learn more about authorization rules here: https://docs.amplify.aws/cli/graphql/authorization-rules
input AMPLIFY { globalAuthRule: AuthRule = { allow: public } } # FOR TESTING ONLY!
type Storage @model {
id: ID!
data: String!
}
在这些内容中,有一个引起关注的短语。
# This "input" configures a global authorization rule to enable public access to
# all models in this schema. Learn more about authorization rules here: https://docs.amplify.aws/cli/graphql/authorization-rules
input AMPLIFY { globalAuthRule: AuthRule = { allow: public } } # FOR TESTING ONLY!
只用于测试!这是一个令人担心的话语…
根据上述指示,我尝试在https://docs.amplify.aws/cli/graphql/authorization-rules/上进行了确认。
当输入“AMPLIFY { globalAuthRule: AuthRule = { allow: public } } # FOR TESTING ONLY!”时,任何人都可以进行Create/Read/Update/Delete操作。
所以,我们将修复模式定义。
首先,将上述的一句话注释掉。
我們將使用 @auth 在數據模型上設定訪問權限。
在這種情況下,我們希望任何人都可以訪問,因此我們將設置 allow:public。
# This "input" configures a global authorization rule to enable public access to
# all models in this schema. Learn more about authorization rules here: https://docs.amplify.aws/cli/graphql/authorization-rules
# input AMPLIFY { globalAuthRule: AuthRule = { allow: public } } # FOR TESTING ONLY!
type Storage @model @auth(rules: [{ allow: public }]){
id: ID!
data: String!
}
经过上述修正,执行amplify push以反映模式设置。
这次的情况下,我将@auth设置为allow:public,
如果想要进行通过Cognito进行用户认证,并且只允许访问自己的数据,可以设置为allow:owner。
这就是全部了。