首页 文章

AWS - 无法从EC2窗口访问S3存储桶

提问于
浏览
2

我已经启动了EC2 - Windows实例创建了一个S3存储桶,创建了一个角色S3-FullAccess并分配给EC2实例 .

从EC2实例浏览器,我可以访问我的角色的元数据:http://169.254.169.254/latest/meta-data/iam/security-credentials/EC2-S3-access

{
  "Code" : "Success",
  "LastUpdated" : "2018-04-10T04:47:11Z",
  "Type" : "AWS-HMAC",
  "AccessKeyId" : "myaccess"
  "SecretAccessKey" : "mysecretkey",
  "Token" : "mytoken",
  "Expiration" : "2018-04-10T11:10:43Z"
}

如果我试图从S3存储桶访问文件:https://s3.ap-south-1.amazonaws.com/mybucket/test.jar

得到以下错误:

<Error>
    <Code>AccessDenied</Code>
    <Message>Access Denied</Message>
    <RequestId>5EDB0A49E36E0E50</RequestId>
    <HostId>cPFNEbsfwXA=</HostId>
</Error>

角色JSON:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": "*"
        }
    ]
}

我是AWS的新手

EDIT

正如建议我使用CLI命令,得到以下错误:引用https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html

C:\Users\Administrator>aws s3 cp https://s3.ap-south-1.amazonaws.com/mybucket/test.jar C:\downloads

usage: aws s3 cp <LocalPath> <S3Uri> or <S3Uri> <LocalPath> or <S3Uri> <S3Uri>
Error: Invalid argument type

C:\Users\Administrator>aws s3 cp https://mybucket/test.jar C:\downloads

usage: aws s3 cp <LocalPath> <S3Uri> or <S3Uri> <LocalPath> or <S3Uri> <S3Uri>
Error: Invalid argument type

C:\Users\Administrator>aws s3 cp https://mybucket/test.jar . --recursive

usage: aws s3 cp <LocalPath> <S3Uri> or <S3Uri> <LocalPath> or <S3Uri> <S3Uri>
Error: Invalid argument type

Worked

C:\Users\Administrator>aws s3 cp s3://mybucket/test.jar C:\downloads

shouldn't use https://, used s3://, it's working

2 回答

  • 1

    访问对象 through a URL in your browser 时,您没有传递任何用户凭据 . 因此,Amazon S3不知道您是谁,并且是对象的 denying access .

    最好的方法是 access the objects via an API call ,来自编程语言SDK或使用AWS Command-Line Interface (CLI),它具有可以将文件复制到Amazon S3或从Amazon S3复制文件的 aws s3 cp 命令 .

    如果您必须通过Web浏览器进行访问,同时保持对象的私有性,那么您的应用程序将需要生成一个限时的pre-signed URL,以便在指定的时间范围内授予对象的访问权限 .

  • 0

    您创建的EC2角色将允许在EC2上运行的任何SDK访问S3存储桶,而不是从浏览器访问 .

    如果您想使用浏览器访问S3文件(无论是从您的笔记本电脑的EC2(或)),并限制存储桶内容的可见性,最好的方法是使用presigned urls .

    为对象创建预签名URL时,必须提供安全凭据,指定存储桶名称,对象密钥,指定HTTP方法(下载对象的GET)以及到期日期和时间 . 预签名URL仅在指定的持续时间内有效 . 接收预签名URL的任何人都可以访问该对象 . 例如,如果您的存储桶中有视频且存储桶和对象都是私有的,则可以通过生成预先签名的URL与其他人共享视频 .

相关问题