python-3.x - AWS UNLOAD 无效凭证子句

标签 python-3.x amazon-web-services amazon-s3 amazon-redshift

我正在尝试将数据从 AWS Redshift 卸载到 s3 存储桶。 Redshift 集群还受密码保护。我设置了 aws cli 以使用适当的 key 对并成功测试。我可以使用我的凭据通过 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)

我收到以下错误
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]
  -----------------------------------------------
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"
}

最佳答案

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

您可以指定 iam_roleaccess_key_idsecret_access_key (如果使用临时凭据,则使用 session_token)。

关于python-3.x - AWS UNLOAD 无效凭证子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48532619/

相关文章:

python - 如何使用 django 2 创建适应用户数量的表单?

ruby-on-rails - 谷歌云引擎: Open port 587

python-3.x - 如何在 AWS EMR 上设置 PYTHONHASHSEED

amazon-web-services - 代码 : NoSuchBucket

node.js - S3 和 Couchbase - 如何从 Amazon 获取文件并存储它?

ruby - 如何使用 ruby​​ 更新一批 S3 对象的元数据?

python - NetworkX 图形对象不可订阅

python - 如何使用python获取文本文件中的表格格式数据

python - _curses.error : addstr() returned ERR

amazon-web-services - 模板错误:无法在条件中使用 Fn::GetAZs