2017-09-05 45 views
0

我試圖連接到S3存儲桶(第三方是所有者,因此我無法通過AWS控制檯訪問)。使用Cyber​​Duck,我可以連接和上傳文件沒有問題。不過,我已經嘗試過幾個庫來連接桶,所有這些都返回一個403禁止。我在這裏張貼,希望有人能夠發現我做錯了什麼。我可以使用Cyber​​Duck連接到S3 Bucket,但我無法以編程方式連接

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

然而,當我用Cyber​​duck的我可以連接就好了:

enter image description here

+0

編輯:在某一時刻,通過ftp發送顯示403(我不記得它發送403時的樣子),因爲它坐在它出錯的錯誤之上是: SSHException:讀取SSH協議標題時出錯[Errno 54]通過對等方重置連接 –

回答

0

的最簡單的方法是使用AWS Command-Line Interface (CLI),其中U ses 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代碼可能會失敗。)

+0

使用幾乎相同的代碼行,我可以通過從boto更改爲boto3來使其工作。 –

相關問題