【问题标题】:Cognito: upload file to S3 into a custom user subfolder from the browserCognito:从浏览器将文件上传到 S3 到自定义用户子文件夹
【发布时间】:2019-08-28 14:49:52
【问题描述】:

这部分是问题,部分是回答我遇到的一些问题。 问题在底部。

我想使用 Cognito 将文件直接从浏览器上传到 S3。它适用于未经身份验证的用户,但每个用户都可以访问同一个存储桶或至少同一个文件夹。我希望每个用户只能访问他们的文件夹。

经过 3 天的努力,我能够让 Cognito 在一个紧凑的 php 文件中使用 JavaScript 和 PHP 使用自定义开发人员身份验证身份。

在服务器上,我使用getOpenIdTokenForDeveloperIdentity 接收IdentityIdToken 用户名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


    【解决方案1】:

    用户名没有 IAM 策略变量,但如果你想付出一些努力,我认为你可以做点什么。

    1. 更新 Lambda。使用 Cognito SDK 做一个ListUsers。您可以提供 sub 并取回用户名。将数据写入格式为mybucket/username/sub的文件夹
    2. 更新存储桶策略,在用户名所在的位置放置通配符。

    例如:

    Resource": ["arn:aws:s3:::mybucket/*/${cognito-identity.amazonaws.com:sub}/*"]
    

    您仍然可以使用 sub 作为文件夹名称,但它将位于用户名文件夹下,因此浏览文件夹会很容易。

    【讨论】:

      【解决方案2】:

      很遗憾,这是不可能的。您看到的用户名变量仅适用于 IAM 用户名。 IAM 策略无法从 cognito 用户池中读取任何内容。您看到的 sub 变量与用户池中出现的 sub 完全不同。 sub 实际上来自身份池,它没有任何用户名。也许 AWS 将来会允许这样做,但目前这是不可能的。

      但是,您可以在创建应用程序时简单地向用户隐藏它。如果您通过控制台登录,您只会在存储桶路径中看到这个大子变量。我已经构建了一个类似的应用程序,我只是向用户显示此变量中的所有内容,就好像它是他们的根文件夹一样。

      【讨论】:

        猜你喜欢
        • 2015-06-18
        • 2019-03-17
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-14
        • 2014-10-21
        • 1970-01-01
        相关资源
        最近更新 更多