简体   繁体   English

为什么 boto3 在 EC2 上的 Django 中无法识别我的 AWS_ACCESS_KEY_ID?

[英]Why boto3 dose not recognize my AWS_ACCESS_KEY_ID in Django on EC2?

I was connecting the S3 to Django on EC2.我正在将 S3 连接到 EC2 上的 Django。
I confirmed that it works on the my computer (window),我确认它可以在我的电脑(窗口)上运行,
but when I uploaded it to AWS EC2 Ubuntu and ran it,但是当我将它上传到AWS EC2 Ubuntu并运行它时,
I saw the following message.我看到了以下消息。 when i ran python manage.py commands当我运行python manage.py命令时

File "/home/ubuntu/django/e/lib/python3.6/site-packages/botocore/session.py", line 821, in create_client aws_secret_access_key)) **botocore.exceptions.PartialCredentialsError: Partial credentials found in explicit, missing: aws_secret_access_key**

But I think I set it up correctly.但我认为我设置正确。 in my settings.py在我的settings.py


AWS_S3_HOST = 's3.me-south-1.amazonaws.com'
AWS_S3_REGION_NAME= config('AWS_S3_REGION_NAME')
AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_kEY = config('AWS_SECRET_ACCESS_kEY')
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')

I tried我试过

  1. Give IAM permission on EC2在 EC2 上授予 IAM 权限
  2. Delete EC2 retry删除 EC2 重试
  3. set env var via export通过export设置环境变量
  4. throw away virtualenv and try扔掉 virtualenv 并尝试
  5. install awscli and configure it安装 awscli 并配置它
  6. s3 bucket policy configure s3 存储桶策略配置
  7. tried to write it inline because it might not be able to refer to the .env file, but it gave me same message.尝试内联编写它,因为它可能无法引用 .env 文件,但它给了我相同的消息。

I struggled with this problem all day today.我今天整天都在与这个问题作斗争。 When it comes to AWS Config, I think I've tried everything I can.说到 AWS Config,我想我已经尽力了。 If you have any guesses about the cause of this problem, please give me a hint.如果您对这个问题的原因有任何猜测,请给我一个提示。

And I only think boto3 looks up keys in a peculiar way on EC2我只认为 boto3 在 EC2 上以一种特殊的方式查找密钥

AWS role setting worked correctly :) And i notice that I should set setting.py AWS_SECRET_ACCESS_kEY and AWS_ACCESS_KEY_ID None when i am using EC2 with IAM role AWS 角色设置工作正常 :) 我注意到当我将 EC2 与 IAM 角色一起使用时,我应该设置 setting.py AWS_SECRET_ACCESS_kEYAWS_ACCESS_KEY_ID None

The real way to resolve this issue is by using EC2 InstanceProfiles linked to an IAM role STS permissions granted to EC2.解决此问题的真正方法是使用链接到授予 EC2 的 IAM 角色 STS 权限的 EC2 InstanceProfiles。

It's a bad practice to store your AWS credentials on an instance and you should definitely avoid it.将您的 AWS 凭证存储在实例上是一种不好的做法,您绝对应该避免这种做法。

Create an IAM role with the appropriate permissions and attach it to the instance by selecting "Modify IAM role" when right clicking on it in the console.创建具有适当权限的 IAM 角色,并通过在控制台中右键单击“修改 IAM 角色”选择“修改 IAM 角色”将其附加到实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM