ã¢ãã¤ã«ãã¡ã¼ã¹ã室㮠@rejasupotaro ã§ãã
AWS Summit 2014 ã§AWS Mobile Servicesã®ã¢ãããã¼ããããã¾ãããããã®ä¸ã§ãæ°ã«ãªã£ãAmazon Cognitoã«ã¤ãã¦èª¿ã¹ã¦ã¿ã¾ããã
Amazon Cognitoã¨ã¯
ä»ã©ãã®ã¢ãã¤ã«ã¢ããªã§ã¯ãåç´ã«èªè¨¼ãã¦APIãå©ãã ãã§ã¯ãªãããµã¼ãã¹ã«ãã°ã¤ã³ãã¦ããªãç¶æ ã§ãä¸æãªIDãæããã¦ãã¼ã¿ã管çããããã²ã¹ãç¶æ ããä¼å¡ç»é²ãããã¨ãã«ãã¼ã¿ãå¼ãç¶ãã ãããµã¼ãã¹éã§ã·ã³ã°ã«ãµã¤ã³ãªã³ãæä¾ããããããã¤ã¹éã§è¨å®ãå ±æã§ããããã«ãããªã©ãã¦ã¼ã¶ã¼ãã©ã®ããã¤ã¹ãããµã¼ãã¹ã«ã¢ã¯ã»ã¹ãã¦ãã¹ãã¬ã¹ãªã使ããããã«ããããã«æ§ã ãªå·¥å¤«ãåããå¿ è¦ãããã¾ãã
Introduction to Amazon Cognito
ãããã®å®è£ ã¯è¤éã«ãªããã¡ã§ãã¢ããªãä½ã£ã¦ããã¨æã£ããã¢ã«ã¦ã³ã管çã®ããã¿ä½ãã«å¤ãã®æéãåããã¦ãããã¨ãããã¨ãããã¾ãã
ãããªåé¡ã解決ããããã«éçºããã¦ããã®ã Amazon Cognito ã§ãã
- ä¸æã®ã¢ã¤ãã³ãã£ãã£ã®ç®¡ç
- ãªãã©ã¤ã³æ©è½
- ããã¤ã¹éã§ã®ä¿åã¨åæ
- ã·ã¼ã ã¬ã¹ãªã²ã¹ãã¢ã¯ã»ã¹
ä¸è¨ã®ãããªä»ã®ã¢ãã¤ã«ã¢ããªã«ã¯æ¬ ãããªã便å©ãªæ©è½ããããã¯ã¨ã³ãã®ã³ã¼ãã®è¨è¿°ãã¤ã³ãã©ã¹ãã©ã¯ãã£ã®ç®¡çããããã¨ãªããå®è£ ã»éç¨ããããã¨ãã§ãã¦ãã¢ãã¤ã«ã¢ããªéçºè ã¯åªããã¢ããªã±ã¼ã·ã§ã³ä½é¨ãä½æãããã¨ã«éä¸ã§ãããã¨ããã¾ãã
ããã¯èª¿ã¹ãã«ã¯ãããã¾ãããã
Cognitoãå§ãã
AWSããã¸ã¡ã³ãã³ã³ã½ã¼ã«ã® Services > Cognito > Create New Identity Pool ããæ°ããIdentity Poolãä½ããã¨ãã§ãã¾ãã
Amazon Cognitoã¯ã¾ã ãã¬ãã¥ã¼ãªã®ã§ãä»ã®ã¨ããã¯ãã¼ã¸ãã¢ãªã¼ã¸ã§ã³ããé¸æã§ãã¾ããã
Identity Poolã¯ãã¼ã¿ãä¿åããã³ã³ããã§ã1ã¤ã®ã¢ããªã«é¢é£ä»ãããã¨ããè¤æ°ã®ã¢ããªã«é¢é£ä»ãããã¨ãã§ãã¾ããIdentity Poolã®ä¸ã®Datasetã¨å¼ã°ãããã¼ãã«ã«ãkey-valueå½¢å¼ã§ãã¼ã¿ãä¿åããããã«ãªã£ã¦ãã¾ããã¦ã¼ã¶ã¼ãã¨ã®Datasetã®ãµã¤ãºã®ä¸éã¯20MBã§ããããã«ã¦ã¼ã¶ã®1ã¤ã®Datasetã®ãµã¤ãºã®ä¸éã¯1MBã«ãªã£ã¦ãã¦ããã¼ã®æ大æ°ã¯1024åã«ãªã£ã¦ãã¾ãã
帯åå¹ ãéããã¦ããå ´åã«ãä½åº¦ããªãã©ã¤ãã¦ããããªã¼ã®å¯¿å½ã¨ãã¼ã¿ãã©ã³ãæ¶è²»ãããªãããã«1ã¤ã®Datasetã®ä¸éã1MBã«å¶éãã¦ããããã§ãã
次ã«Roleã®è¨å®ã§ããæªèªè¨¼ã®ã¦ã¼ã¶ã¼ã¨ãã°ã¤ã³ã¦ã¼ã¶ã¼ã®ããããã«å¯¾ãã¦Roleãå²ãå½ã¦ããã¨ãã§ãã¾ãã
ããã«ãã£ã¦ãS3ãDynamoDBãªã©ã«ã¦ã¼ã¶ã¼å°ç¨ã®é åãç¨æãã¦ã¯ã©ã¤ã¢ã³ãããç´æ¥èªã¿æ¸ãããããªã©ãAWSãªã½ã¼ã¹ã¸ã®ã¢ã¯ã»ã¹ãæè»ã«å¶å¾¡ãããã¨ãã§ãã¾ãã
è¨å®ã¯ããã§å®äºã§ããAmazon Cognito APIãç´æ¥å©ããã¨ãã§ãã¾ããããã£ãããªã®ã§Amazon Mobile SDKã使ãã¾ãã Download Starter Code (Android and iOS) ããèªåã®accountIdãidentityPoolIdãunauthRoleArnãauthRoleArnãçµã¿è¾¼ã¾ãããµã³ãã«ã³ã¼ãããã¦ã³ãã¼ããããã¨ãã§ãã¾ãã
AndroidããCognitoã使ã£ã¦ã¿ã
è¨å®å®äºãã¼ã¸ã«èªåã®accountIdãidentityPoolIdãunauthRoleArnãauthRoleArnãçµã¿è¾¼ã¾ããCogniteCredentialsProviderã®åæåã®ã³ã¼ããããã®ã§ãã³ãããã¾ãã
CognitoCredentialsProvider cognitoProvider = new CognitoCredentialsProvider( context, "xxxxxxxxxx", "us-east-1:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX", "arn:aws:iam::XXXXXXXXXX:role/YourRoleName", "arn:aws:iam::XXXXXXXXXX:role/YourRoleName");
æ¸ãè¾¼ãå¤ã決ã¾ã£ã¦ããã¨ãã«ã¯ãä¿å®æ§ã®åä¸ãéç解æãæ´»ããããã«Datasetã®æä½ã¯ã©ã¹ãä½ã£ã¦ããããããã¼ã¿ãæä½ããã®ãå®ç³ã§ãããä¸æ¦åããã¦ã¿ãã ããªã®ã§æååããã¿æ¸ããã¦ããã¾ãã
CognitoSyncClient syncClient = new DefaultCognitoSyncClient(context, COGNITO_POOL_ID, cognitoProvider); profileDataset = syncClient.openOrCreateDataset("profile");
æ¸ãè¾¼ãã¨ã㯠Dataset#put
ã§èªã¿è¾¼ãã¨ã㯠Dataset#get
ã§ããputããã¨Androidã§ã¯ãã¼ã«ã«ãã£ãã·ã¥ã®SQLiteã«ä¿åãã㦠Dataset#synchronize
ã§ãµã¼ãã¼ã¨åæãã¾ãã
profileDataset.put("name", name); String name = profileDataset.get("name");
ãã¼ã¿ã®åæ
Dataset#synchronize
ãå¼ã¶ã¨ãªã¢ã¼ãã®å¤æ´å·®åãpullããã¦ãããããããã¤ã¹ã§ã®å¤æ´å·®åããµã¼ãã«pushããããã¿ã«ãªã£ã¦ãã¾ãã
dataset.syncronize(syncCallback);
pullããéã«ãã¼ã«ã«ãã£ãã·ã¥ã¨æ¯è¼ãã¦ãã³ã³ããªã¯ããèµ·ãã£ãã¨ãã¯SyncCallbackã§ãã³ããªã³ã°ã§ãã¾ããSyncCallbackã®ä»¥ä¸ã®ã¡ã½ããããªã¼ãã¼ã©ã¤ããã¾ãã
- onSuccess
- onFailure
- onConflict
- onDatasetDeleted
- onDatasetsMerged
private Dataset.SyncCallback syncCallback = new Dataset.SyncCallback() { @Override public void onSuccess(Dataset dataset, List<Record> records) { // Recordã¯keyãvalueãlastModifiedByãdeviceLastModifiedDateãlastModifiedDateãsyncCountãæã£ã¦ãã¾ã } } @Override public boolean onConflict(Dataset dataset, List<SyncConflict> syncConflicts) { // SyncConflictã¯getLocalRecordã¨getRemoteRecordãæã£ã¦ã㦠// æåã§Recordãæ¯è¼ãã¦ã©ã¡ããæ¡ç¨ãããé¸ã¶ãã¨ãã§ãã¾ã return false; } ... };
onConflictã¯ããã©ã«ãã§ã¯ææ°ã®å¤æ´ãæ¡ç¨ããããã«ãªã£ã¦ãã¾ãã
SDKã®Cognitoã®å®è£
aws/aws-sdk-android-v2 㨠aws/amazon-cognito-android ã¯GitHubã§ã½ã¼ã¹ãå ¬éããã¦ãã¾ãã
amazon-cognito-androidãèªãã§ã¿ãã¨ãããä¸ã®ãããªæ§æã«ãªã£ã¦ãã¾ããã
CognitoCredentialsProviderãidentityId ãaccessKey ãsecretKeyãsessionTokenãªã©ã®èªè¨¼æ å ±ãSharedPreferencesã§ç®¡çãã¦ãã¾ãã
SQLiteLocalStorageã§ã¯datasetsãã¼ãã«ã¨recordsãã¼ãã«ã管çãã¦ãã¾ãã
Dataset#synchronized
ãå¼ã°ããã¨RemoteDataStorageã¨LocalStorageãmergeããã¦ãAmazonCognitoSyncServiceClientçµç±ã§éä¿¡ããã¾ããRemoteDataStorageã¨LocalStorageã¯internalããã±ã¼ã¸ã®ä¸ã«ãã£ã¦ãéçºè
ã¯ã©ãã«ä¿åãããã¨ãããã¨ãæèããã«ã³ã¼ããæ¸ããã¨ãã§ãã¾ãã
å¿
è¦ãªãã¼ããã·ã§ã³ã« ACCESS_NETWORK_STATE
ããã£ãã®ã§è¿½ã£ã¦ã¿ãã Dataset#synchronizeOnConnectivity
ã¨ããã¡ã½ããã¯ãåæãè¦æ±ãããã¨ãã«ãªãã©ã¤ã³ã ã£ãå ´åã«ãåã³ãããã¯ã¼ã¯ã«æ¥ç¶ãããã¾ã§å¦çããã³ãã£ã³ã°ã«ãã¦ãåæ¥ç¶ãããéã«åæãå®è¡ããããã«ãªã£ã¦ãã¾ããã
ããã«ãã£ã¦ããã¤ã¹ããªã³ã©ã¤ã³ããªãã©ã¤ã³ãã«é¢ããããã¼ã¿ãä¿åãããã¨ãã§ãã¾ãã
ã¾ã¨ã
Amazon Cognitoã¯ã¢ã«ã¦ã³ã管çã®è¤éãªå¦çãè©ä»£ãããããã®ã«æç¨ãªãµã¼ãã¹ã ã¨æãã¾ãã
ä»ã®ã¨ãããã°ã¤ã³ãããã¤ããFacebookã¨Googleã¨Amazonã®ã¿ãªã®ã§ãä»»æã®ãã°ã¤ã³ãããã¤ãã使ããããã«ãªã£ããã課éç¶æ ã«ããã¦ã¼ã¶ã¼ã«ã®ã¿é²è¦§æ¨©éãä¸ããã¨ãããããªã¢ã¯ã»ã¹å¶å¾¡ãããããã«ç´°ããRoleã®è¨å®ãã§ããããã«ãªã£ããããã¨ãããã«ä¾¿å©ã«ãªãããªã¨æãã¾ãã
Amazon Cognitoã¯ã¾ã ãã¬ãã¥ã¼ãªã®ã§ãæå¾ ããªããAmazon Mobile Servicesã®æ£å¼ãªãªã¼ã¹ãå¾ ã¡ã¾ãã