简体   繁体   English

在 Python 脚本中传递 AWS 凭证

[英]Passing AWS Credentials in Python Script

I have a python script that gets called by a PHP.我有一个被 PHP 调用的 python 脚本。 The user that invokes this php script is apache and hence, the python file also gets invoked by apache .调用这个 php 脚本的用户是apache ,因此,python 文件也被apache调用。 So, it gives "Unable to locate credentials ".因此,它给出了“无法找到凭据”。 I've set the default credentials via awscli and when I invoke the python script as root, it works.我已经通过 awscli 设置了默认凭据,当我以 root 身份调用 python 脚本时,它可以工作。

This is my line of code :这是我的代码行:

client = boto3.client('ses', region_name=awsregion, aws_access_key_id='AJHHJHJHJ', aws_secret_access_key='asdasd/asdasd/asd')

But, this gives "Invalid Syntax" Error.但是,这会导致“无效语法”错误。 So, I tried this :所以,我试过这个:

client = boto3.Session(aws_access_key_id='ASDASD', aws_secret_access_key='asd/asdasd/asdasd')
client = boto3.client('ses', region_name=awsregion, aws_access_key_id='ASDASD', aws_secret_access_key='asd/asdasd/asdasd')

Gives the same error as above.给出与上面相同的错误。 Weird thing is that this same thing is mentioned in the documentation .奇怪的是, 文档中提到了同样的事情。 Even though it's not recommended, it should work.即使不推荐,它也应该有效。

Can somebody help me in fixing this?有人可以帮我解决这个问题吗?

Did you ever get this resolved?你有没有解决过这个问题? Here is how I connect to boto3 in my Python scripts:以下是我在 Python 脚本中连接到 boto3 的方式:

import boto3
from botocore.exceptions import ClientError
import re
from io import BytesIO
import gzip
import datetime
import dateutil.parser as dparser
from datetime import datetime
import tarfile
import requests
import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job

## Needed glue stuff
sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)

## 
## currently this will run for everything that is in the staging directory     of omniture

# set needed parms
myProfileName = 'MyDataLake'
dhiBucket = 'data-lake'
#create boto3 session
try:    
    session = boto3.Session(aws_access_key_id='aaaaaaaaaaaa',     aws_secret_access_key='abcdefghijklmnopqrstuvwxyz', region_name='us-east-1')aws_session_token=None, region_name=None, botocore_session=None
    s3 = session.resource('s3') #establish connection to s3
except Exception as conne:
    print ("Unable to connect:  " + str(conne))
    errtxt = requests.post("https://errorcapturesite", data={'message':'Unable to connect to : ' + myProfileName, 'notify':True,'color':'red'})
    print(errtxt.text) 
    exit()

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

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