【问题标题】:I can use CyberDuck to connect to S3 Bucket, but I cannot programmatically connect我可以使用 Cyber​​Duck 连接到 S3 Bucket,但无法以编程方式连接
【发布时间】:2017-09-05 21:17:51
【问题描述】:

我正在尝试连接到 S3 存储桶(第 3 方是所有者,因此我无法通过 AWS 控制台访问)。使用 Cyber​​Duck,我可以毫无问题地连接和上传文件。但是我已经尝试了几个库来连接到存储桶,所有这些库都返回 403 禁止。我在这里发帖希望有人能发现我做错了什么。

    def send_to_s3(file_name):
        csv = open("/tmp/" + file_name, 'rb')
        conn = tinys3.Connection("SECRET",
                                 "SECRET",
                                 tls=True,
                                 endpoint="s3.amazonaws.com")
        conn.upload("MDA-Data-Ingest/input/" + file_name, csv, bucket="gsext-69qlakrroehhgr0f47bhffnwct")


    def send_via_ftp(file_name):
        cnopts = pysftp.CnOpts()
        cnopts.hostkeys = None
        srv = pysftp.Connection(host="gsext-69qlakrroehhgr0f47bhffnwct.s3.amazonaws.com",
                                username="SECRET",
                                password="SECRET",
                                port=443,
                                cnopts=cnopts)

        with srv.cd('\MDA-Data-Ingest\input'):
            srv.put('\\tmp\\'+file_name)

        # Closes the connection
        srv.close()

    def send_via_boto(file_name):
        access_key = 'SECRET'
        secret_key = 'SECRET'

        conn = boto.connect_s3(
            aws_access_key_id=access_key,
            aws_secret_access_key=secret_key,
            host='s3.amazonaws.com',
            # is_secure=False,               # uncomment if you are not using ssl
            calling_format=boto.s3.connection.OrdinaryCallingFormat(),
        )

所有这些函数都返回一个 403 禁止,如下所示:

HTTPError: 403 Client Error: Forbidden for url: https://gsext-69qlakrroehhgr0f47bhffnwct.s3.amazonaws.com/MDA-Data-Ingest/input/accounts.csv

但是,当我使用 Cyber​​Duck 时,我可以正常连接:

【问题讨论】:

  • 编辑:在某一时刻,通过 ftp 发送显示 403(我不记得它在发送 403 时的样子),因为它位于它吐出的错误之上:SSHException:错误读取 SSH 协议横幅[Errno 54] 对等方重置连接

标签: python amazon-web-services amazon-s3


【解决方案1】:

最简单的方法是使用AWS Command-Line Interface (CLI),它使用boto3 来访问AWS 服务。

例如:

aws s3 ls s3://bucket-name --region us-west-2

aws s3 cp s3://gsext-69qlakrroehhgr0f47bhffnwct/MDA-Data-Ingest/input/accounts.csv accounts.csv

您将首先运行 aws configure 以提供您的凭据和默认区域,但上述语法允许您指定存储桶所在的特定区域。 (您的 Python 代码可能由于调用了错误的区域而失败。)

【讨论】:

  • 使用几乎相同的代码行,我可以通过将 boto 更改为 boto3 来使其工作。
猜你喜欢
  • 2014-05-04
  • 1970-01-01
  • 1970-01-01
  • 2013-06-12
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
  • 2018-07-26
  • 2012-07-22
相关资源
最近更新 更多