从Python访问Redshift时出现“凭据无效”错误
我正在尝试编写一个Python脚本来访问Amazon Redshift,以便在Redshift中创建表并将数据从S3复制到Redshift表。从Python访问Redshift时出现“凭据无效”错误
我的代码是:
import psycopg2
import os
#import pandas as pd
import requests
requests.packages.urllib3.disable_warnings()
redshift_endpoint = os.getenv("END-point")
redshift_user = os.getenv("user")
redshift_pass = os.getenv("PASSWORD")
port = 5439
dbname = 'DBNAME'
conn = psycopg2.connect(
host="",
user='',
port=5439,
password='',
dbname='')
cur = conn.cursor()
aws_key = os.getenv("access_key") # needed to access S3 Sample Data
aws_secret = os.getenv("secret_key")
#aws_iam_role= os.getenv('iam_role') #tried using this too
base_copy_string= """copy %s from 's3://mypath/%s'.csv
credentials 'aws_access_key_id= %s aws_access_secrect_key= %s'
delimiter '%s';""" # the base COPY string that we'll be using
#easily generate each table that we'll need to COPY data from
tables = ["employee"]
data_files = ["test"]
delimiters = [","]
#the generated COPY statements we'll be using to load data;
copy_statements = []
for tab, f, delim in zip(tables, data_files, delimiters):
copy_statements.append(base_copy_string % (tab, f, aws_key, aws_secret, delim)%)
#create Table
cur.execute(""" create table employee(empname varchar(30),empno integer,phoneno integer,email varchar(30))""")
for copy_statement in copy_statements: # execute each COPY statement
cur.execute(copy_statement)
conn.commit()
for table in tables + ["employee"]:
cur.execute("select count(*) from %s;" % (table,))
print(cur.fetchone())
conn.commit() # make sure data went through and commit our statements permanently.
当我在cur.execute运行此命令我得到一个错误(copy_statement)
**Error:** error: Invalid credentials. Must be of the format: credentials 'aws_iam_role=...' or 'aws_access_key_id=...;aws_secre
t_access_key=...[;token=...]'
code: 8001
context:
query: 582
location: aws_credentials_parser.cpp:114
process: padbmaster [pid=18692]
有没有在我的代码有问题吗?还是它是一个AWS access_key问题?
我甚至使用iam_role尝试,但我得到一个错误:
IAM role cannot assume role even in Redshift
我必须通过附加S3FullAccess政策管理IAM角色权限。
脚本中存在一些错误。
1)更改如下base_copy_string:
base_copy_string= """copy %s from 's3://mypath/%s.csv' credentials 'aws_access_key_id=%s;aws_secret_access_key=%s' delimiter '%s';""" # the base COPY string that we'll be using
必须有凭据,还可以其他格式问题与单引号加一个;
。它是aws_secret_access_key
而不是aws_access_secrect_key
。
我建议你使用IAM-角色而不是凭据。 http://docs.aws.amazon.com/redshift/latest/dg/loading-data-access-permissions.html
2)改变copy_statements.append如下(删除到底额外%
):
copy_statements.append(base_copy_string % (tab, f, aws_key, aws_secret, delim))
改正这些问题并再试一次。
谢谢,现在我得到这个错误copy_statements.append(base_copy_string%(tab,f,aws_key,aws_secret,delim)) TypeError:并非所有在字符串格式化过程中转换的参数 –
使用'str(variable_name)'为类型变量串起来。可能aws_key和aws_secret导致错误。 'copy_statements.append(base_copy_string%(tab,f,str(aws_key),str(aws_secret),delim))' –
我试过这样也不行 –
首先,从不,从不,硬编码访问密钥和秘密密钥在您的代码。这样就排除了你的第一个查询。现在正在实现事情的正确方式。你是对的,IAM角色是正确的做法。不幸的是,我无法从你的描述中得到确切的错误和用例。据我所知,你试图从你的电脑(本地机器)运行这个python文件。因此,您需要为您的IAM用户附加权限才能访问RedShift(以及您的代码所触及的所有其他服务)。如果我的假设错误,请纠正我。
是的,那正是我想要做的。我试图从S3发送数据到Redshift –
正确的说法是从S3中提取数据。所以你需要给RedShift资源权限来访问S3(你正在做的)。如果您在IAM角色的情况下附加错误的屏幕截图,这将会很有帮助。 –
----------------------------------------------- 错误:User arn:aws:redshift:us-east-1:028810420564:dbuser:my-cluster/venkat未被授权承担IAM角色arn:aws:iam :: 028810420 564:role/redshift-s3 code:8001 上下文:IAM角色= ARN:AWS:IAM :: 028810420564:角色/红移-S3 查询:3209 位置:xen_aws_credentials_mgr.cpp:229个 过程:padbmaster [PID = 19102] -------- --------------------------------------- –
就在,如果你错过了 安装AWS CLI 情况下运行 AWS配置 把你的证书和地区 希望这有助于。
很难说出你写的内容。请考虑编辑。 – norok2
这是无关紧要的,因为他没有使用AWS CLI或boto或boto3等库。他将自己的凭据传递给副本声明本身。 –
你在你的base_copy_string中有一个错字:'aws_access_secrect_key' –