AWS UNLOAD 无效凭证子句

Yan*_*mer 4 amazon-s3 amazon-web-services python-3.x amazon-redshift

我正在尝试将数据从 AWS Redshift 卸载到 s3 存储桶。Redshift 集群还受到密码保护。我设置了 aws cli 以使用适当的密钥对并进行了成功测试。我可以使用我的凭据通过 DataGrip 访问 Redshift 集群,但现在当我尝试在 python3 中使用以下脚本卸载时

import json
import os
import psycopg2


def run(config_json, sql_query):

    conn = psycopg2.connect(**config_json['db'])
    cursor = conn.cursor()

    query = """
        UNLOAD ($${}$$)
        to \'{}\'
        parallel off
        delimiter ','
        allowoverwrite;
        """.format(sql_query, config_json['s3bucket_path_to_file'])
    print("The following UNLOAD query is being run: \n" + query)
    cursor.execute(query)
    print('Completed write to {}'.format(config_json['s3bucket_path_to_file']))


if __name__ == '__main__':
    config_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config.json')
    with open(config_path, 'r') as f:
        config = json.loads(f.read())

    query_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'query.sql')
    with open(query_path, 'r') as f:
        query_sql = f.read()

    run(config, query_sql)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

psycopg2.InternalError: invalid CREDENTIALS clause
DETAIL:  
  -----------------------------------------------
  error:  invalid CREDENTIALS clause
  code:      8001
  context:   
  query:     2820791
  location:  aws_credentials_parser.cpp:62
  process:   padbmaster [pid=25330]
  -----------------------------------------------
Run Code Online (Sandbox Code Playgroud)

config.json文件具有以下格式:

{
  "db": {
    "dbname": "dbname",
    "user": "user1",
    "host": "someip",
    "password": "very secret psw",
    "port": "1111"
  },
  "s3bucket_path_to_file": "s3://bucket-name/path/to/file.csv"
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*ley 5

您尚未在查询语句中指定 CREDENTIALS 部分。为了让 Redshift 写入您的 S3 存储桶,您需要提供 Redshift 将使用的有效凭据。

您可以指定iam_roleORaccess_key_idsecret_access_key(如果使用临时凭证,还可以指定 session_token)。