首页 文章

我可以使用CyberDuck连接到S3 Bucket,但我无法以编程方式连接

提问于
浏览
0

我试图连接到S3桶(第三方是主人,所以我无法通过AWS控制台访问) . 使用CyberDuck,我可以连接和上传文件没问题 . 但是我已经尝试了几个库连接到桶,所有这些库都返回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客户端错误:禁止访问网址:https://gsext-69qlakrroehhgr0f47bhffnwct.s3.amazonaws.com/MDA-Data-Ingest/input/accounts.csv

但是,当我使用CyberDuck时,我可以很好地连接:

enter image description here

1 回答

  • 0

    最简单的方法是使用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代码可能会失败 . )

相关问题