【发布时间】:2019-08-28 14:49:52
【问题描述】:
这部分是问题,部分是回答我遇到的一些问题。 问题在底部。
我想使用 Cognito 将文件直接从浏览器上传到 S3。它适用于未经身份验证的用户,但每个用户都可以访问同一个存储桶或至少同一个文件夹。我希望每个用户只能访问他们的文件夹。
经过 3 天的努力,我能够让 Cognito 在一个紧凑的 php 文件中使用 JavaScript 和 PHP 使用自定义开发人员身份验证身份。
在服务器上,我使用getOpenIdTokenForDeveloperIdentity 接收IdentityId 和Token 用户名johnsmith。
然后我立即将 id 和令牌提供给客户端以对其进行身份验证并将文件上传到 S3:
<!-- BEGIN SERVER SIDE -->
<?php
session_start();
//Include AWS client libs
require ('vendor/autoload.php');
use Aws\CognitoIdentity\CognitoIdentityClient;
use Aws\Sts\StsClient;
/* Global Vars */
$aws_region = 'us-east-1';
$aws_key = 'JF4L3ELC4CAVQV4VAKIA';
$aws_secret = 'OKfoWZ91qZHBhIBzDZLINzHVs9Ymaxi689Ym3vT8';
$identity_pool_id = 'us-east-1:83024a7c-438e-aa29-8bac-ff6717d73ec5';
//Initialize a Cognito Identity Client using the Factory
$client = CognitoIdentityClient::factory(array('region' => $aws_region, 'key' => $aws_key, 'secret' => $aws_secret));
/* Acquire new Identity */
$identity = $client->getOpenIdTokenForDeveloperIdentity(array('IdentityPoolId' => $identity_pool_id, 'Logins' => array('my-custom-login' => 'johnsmith')));
//Obtain Identity from response data structure
$id = $identity->get('IdentityId');
$token = $identity->get('Token');
print_r($identity);
// Results in:
// [IdentityId] => us-east-1:c4db3eca-6ed0-af08-4aaa-a33aee2e994a
// [Token] => eyJraWQiOiJ1cy1lYXN0LTExIiwidHlwIjoiSldTIiwiYWxnIjoiUlM1MTIifQ.eyJzd...
?>
<!-- BEGIN CLIENT SIDE -->
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.436.0.min.js"></script>
<script>
var bucketName = 'mybucket';
//Get CognitoIdentityCredentials
AWS.config.region = 'us-east-1';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityId: '<?php echo $id; ?>',
IdentityPoolId: '<?php echo $identity_pool_id; ?>',
Logins: {
//'my-custom-login':'johnsmith', //This does not work. It throws Error 400: Please provide a valid public provider.
'cognito-identity.amazonaws.com':'<?php echo $token; ?>'
}
});
//S3 Upload
var s3 = new AWS.S3({
//region: 'us-east-1', //Bucket region - not required if same as AWS.config.region
apiVersion: '2006-03-01',
params: {Bucket: bucketName}
});
//Refresh and Upload
AWS.config.credentials.refresh(function(){
var IdentityId = s3.config.credentials.params.IdentityId;
var keyName = "cognito/"+IdentityId+"/it_works.txt";
var params = {Bucket: bucketName, Key: keyName, Body: 'Hello World!'};
s3.putObject(params, function (err, data) {
if (err)
console.log(err)
else
console.log("Successfully uploaded data to " + bucketName + "/" + keyName);
});
});
//Console: Successfully uploaded data to mybucket/cognito/us-east-1:c4db3eca-6ed0-af08-4aaa-a33aee2e994a/it_works.txt);
</script>
我的 IAM 权限政策是借用 from here 的,如下所示:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": ["s3:ListBucket"],
"Effect": "Allow",
"Resource": ["arn:aws:s3:::mybucket"],
"Condition": {"StringLike": {"s3:prefix": ["${cognito-identity.amazonaws.com:sub}/*"]}}
},
{
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Effect": "Allow",
"Resource": ["arn:aws:s3:::mybucket/${cognito-identity.amazonaws.com:sub}/*"]
}
]
}
这一切都很好,并在mybucket/cognito/us-east-1:c4db3eca-6ed0-af08-4aaa-a33aee2e994a/it_works.txt上传文件
我想要的是设置一个以用户名 johnsmith 命名的自定义子文件夹。
我的问题是:
有没有办法将用户名作为变量传递给 IAM 角色权限,以便它能够上传到mybucket/cognito/johnsmith/,而不是这个丑陋而长的mybucket/cognito/us-east-1:c4db3eca-6ed0-af08-4aaa-a33aee2e994a/?
那些 IdentityId 太长而且难以处理。我想我也必须将 IdentityId 保存在我的数据库中?
令牌中的信息是“可解码的”还是只是一个安全哈希。我注意到当我刷新它的后半部分时,当我更改用户名时,整个令牌都会发生变化。
请告诉我如何使用 Cognito 设置自定义子文件夹,而不是 IdentityId ${cognito-identity.amazonaws.com:sub}
我找到了this article,这表明您可以使用${aws:username}/*,但是如何将用户名传递到 OpenID 令牌或其他地方?
【问题讨论】:
标签: php amazon-web-services amazon-s3 amazon-cognito amazon-iam