简体   繁体   English

我可以使用Cyber​​Duck连接到S3存储桶,但无法以编程方式连接

[英]I can use CyberDuck to connect to S3 Bucket, but I cannot programmatically connect

I am attempting to connect to an S3 bucket (A 3rd party is the owner, so I cannot access through AWS console). 我正在尝试连接到S3存储桶(第3方是所有者,所以我无法通过AWS控制台访问)。 Using CyberDuck, I can connect and upload files no problem. 使用Cyber​​Duck,我可以连接和上传文件。 However I have tried several libraries to connect to the bucket all of which return a 403 forbidden. 但是,我尝试了几个库来连接到存储桶,所有这些库都返回禁止的403。 I am posting here in hopes that someone can spot what I am doing incorrectly. 我在这里发布,希望有人可以发现我的错误行为。

    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(),
        )

All of these functions return a 403 forbidden as shown bellow: 所有这些函数都返回禁止的403,如下所示:

HTTPError: 403 Client Error: Forbidden for url: https://gsext-69qlakrroehhgr0f47bhffnwct.s3.amazonaws.com/MDA-Data-Ingest/input/accounts.csv HTTPError:403客户端错误:网址禁止: https : //gsext-69qlakrroehhgr0f47bhffnwct.s3.amazonaws.com/MDA-Data-Ingest/input/accounts.csv

However when I use CyberDuck I can connect just fine: 但是,当我使用Cyber​​Duck时,我可以正常连接:

在此处输入图片说明

The easiest method would be to use the AWS Command-Line Interface (CLI) , which uses boto3 to access AWS services. 最简单的方法是使用AWS命令行界面(CLI) ,它使用boto3访问AWS服务。

For example: 例如:

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

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

You would first run aws configure to provide your credentials and a default region, but the syntax above allows you to specify the particular region that the bucket is located. 首先,您将运行aws configure来提供您的凭据和默认区域,但是上面的语法允许您指定存储桶所在的特定区域。 (It is possible that your Python code failed due to calling the wrong region.) (您的Python代码可能由于调用了错误的区域而失败。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM