简体   繁体   English

Boto3 S3实例的AWS凭证放在何处

[英]Where to put AWS credentials for Boto3 S3 instance

I am trying to run a simple python script that goes through all of my AWS buckets and print outs the buckets name. 我正在尝试运行一个简单的python脚本,该脚本遍历我的所有AWS存储桶并打印出存储桶名称。 I am trying to figure out where to put my AWS credentials for authorization. 我试图弄清楚将AWS凭证放在何处进行授权。 In the documentation it says to put them in a specific aws config file 在文档中说将它们放在特定的AWS配置文件中

Alternatively, you can create the credential file yourself. 或者,您可以自己创建凭证文件。 By default, its location is at ~/.aws/credentials: 默认情况下,其位置为〜/ .aws / credentials:

When running a standalone python script, lets call it s3_test.py, where do I put the credentials ? 运行独立的python脚本时,可以将其称为s3_test.py,我将凭据放在哪里? On my local machine, do I create a file called credentials at ~/.aws/ , or can I just put my credentials in the script itself ? 在我的本地计算机上,我是否要在〜/ .aws /中创建一个名为凭据的文件,还是可以将我的凭据放入脚本本身中?

import boto3

s3 = boto3.resource('s3')

bucket_name = 'bucketnamehere'

for bucket in s3.buckets.all():
    print(bucket.name)

Run the AWS CLI command aws configure in your terminal and enter your credentials, region, etc. 在终端中运行AWS CLI命令aws configure并输入您的凭证,区域等。

Also see the AWS credentials docs for more options. 另请参阅AWS凭证文档以了解更多选项。

Always keep secrets out of your source code whenever possible (it's always possible). 尽可能将秘密保密在源代码之外(总是可能的)。

Follow their recommendation to put your credentials at ~/.aws/credentials , or run aws configure as Taylor Wood's answer, but never put credentials in your code. 遵循他们的建议,将您的凭据放在~/.aws/credentials ,或运行aws configure作为Taylor Wood的答案,但不要在您的代码中放置凭据。 It's a big security risk. 这是一个很大的安全风险。

该配置文件应该位于“ C:/Users/Username/.aws”中。

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

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