授权用户的S3存取权限

S3桶我想要保存的对象,如图像或video,并希望它被保护,并只能由授权用户访问我应该做什么。 一种方法是让具有特定时间的令牌的URL在一段时间后过期。 还有其他的方式来做到这一点。

Amazon S3中的对象默认为私有。

如果您只希望授权用户授予访问特定文件的权利,您可以select以下两种方式:

  • 使用AWS凭证,或
  • 使用预先签名的url

如果希望应用程序能够访问这些对象,则可以将AWS凭据提供给您的应用程序。 这可以作为IAM用户 (应用程序也可以是用户 ),也可以通过应用程序假定的IAMangular色 (例如,将angular色分配给EC2实例,然后应用程序自动提供凭据)。 然后,您将授予 IAM用户/angular色权限以访问特定的存储桶或存储桶中的path。

或者,如果您希望授予对特定对象(例如用户的照片)的select性访问权限,则可以使用预先签名的URL 。 这些是提供对Amazon S3对象访问权限的有时限的凭据。 应用程序可以用几行代码生成预先签名的URL 。 然后可以将URL插入到网页中(例如在<img>标签中)以提供对私人对象的临时访问。 当时间到期时 ,URL将不再起作用。

这是一个解决scheme的示例,它允许您的系统在内部访问AWS S3对象的内容,而无需将访问令牌分配给该对象。

 <?php if (!defined('MY_AWS_AUTOLOADER_FILE_LOCATION')) { // Replace /var/www/html/aws/aws-autoloader.php with wherever your actual file is // Make sure you've already downloaded and unzipped the aws-autoloader.php file // Can be found at http://docs.aws.amazon.com/aws-sdk-php/v3/download/aws.zip define('MY_AWS_AUTOLOADER_FILE_LOCATION', '/var/www/html/aws/aws-autoloader.php'); } if (!defined('MY_AWS_REGION_ID')) { // Replace us-west-2 with whatever your actual region ID is define('MY_AWS_REGION_ID', 'us-west-2'); } if (!defined('MY_AWS_ACCESS_KEY_ID')) { // Replace abcdefghijk with whatever your actual access key is define('MY_AWS_ACCESS_KEY_ID', 'abcdefghijk'); } if (!defined('MY_AWS_SECRET_ACCESS_KEY')) { // Replace lmnopqrstuvwxyz123456789 with whatever your actual secret key is define('MY_AWS_SECRET_ACCESS_KEY', 'lmnopqrstuvwxyz123456789'); } // Set the required environmental variables in case they // haven't already been set in .htaccess or elsewhere putenv('AWS_ACCESS_KEY_ID=' . MY_AWS_ACCESS_KEY_ID); putenv('AWS_SECRET_ACCESS_KEY=' . MY_AWS_SECRET_ACCESS_KEY); // Register the s3:// stream wrapper aws_register_stream_wrapper(); // Now, the URI of an S3 object can be accessed internally, without having assigned // a special access token to it. Assuming your bucket's name is my-bucket and the // object's file key inside that bucket is images/example.jpg you can build // the object's URI like so, which would assign a value of // s3://my-bucket/images/example.jpg // to $object_uri (making it internally accessible via that same URI): $object_uri = aws_render_s3_uri('my-bucket', 'images/example.jpg'); /** * Registers the s3:// stream wrapper */ function aws_register_stream_wrapper() { $region = MY_AWS_REGION_ID; // Simple security checks in case someone has messed with something // they shouldn't have if (!empty($region) && is_string($region)) { $client = aws_render_s3_client(); $client->registerStreamWrapper(); } } /** * @param $version (string) * The version of the AWS API to use * * @return (object) * An AWS S3 client */ function aws_render_s3_client($version = 'latest') { require_once(MY_AWS_AUTOLOADER_FILE_LOCATION); $s3_client = new Aws\S3\S3Client([ 'key' => MY_AWS_ACCESS_KEY_ID, 'secret' => MY_AWS_SECRET_ACCESS_KEY, 'region' => MY_AWS_REGION_ID, 'version' => $version, ]); return $s3_client; } /** * @param $bucket (string) * The name of the AWS bucket in which $file_key resides * * @param $file_key (string) * The name of the file to be retrieved, relative to the AWS bucket * * @return (string) * The URL to an AWS S3 object URL using the s3:// stream wrapper */ function aws_render_s3_uri($bucket, $file_key) { $uri = "s3://{$bucket}/{$file_key}"; return $uri; }