簡體   English   中英

如何使用 python 從公共 AWS s3 下載文件?

[英]How to download a file from public AWS s3 with python?

我正在嘗試使用 python 腳本從該網站的公共 aws s3 下載文件。 例如,鏈接上的第一個 object。 我嘗試了 boto3,但出現了 No Credentials 錯誤:

s3 = boto3.resource('s3')
bucket = s3.Bucket('oedi-data-lake')

keys = []

for obj in bucket.objects.filter(Prefix='nrel-pds-building-stock/end-use-load-profiles-for-us-building-stock/2022/resstock_tmy3_release_1/building_energy_models/upgrade=10/'):
    
    if obj.key.endswith('bldg0000001-up10.zip'):
        
        keys.append(obj.key)
        
print(keys)

我還發現了一個帖子Download file/folder from Public AWS S3 with Python, no credentials

我嘗試如下:

import requests

headers = {'Host' : 'oedi-data-lake.s3.amazonaws.com'}
url = 'https://oedi-data-lake.s3.amazonaws.com/nrel-pds-building-stock/end-use-load-profiles-for-us-building-stock/2022/resstock_tmy3_release_1/building_energy_models/upgrade=10/bldg0000001-up10.zip'
r = requests.get(url) 

但得到了 SSLCertVerificationError

請幫忙。 :)

++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++

謝謝你,jhashimoto!

但是通過執行以下操作,我仍然有 NoCredentialsError

import boto3
from botocore import UNSIGNED
from botocore.config import Config

s3 = boto3.resource("s3", config=Config(signature_version=UNSIGNED))

s3_client = boto3.client('s3')
s3_client.download_file('oedi-data-lake', 'nrel-pds-building-stock/end-use-load-profiles-for-us-building-stock/2022/resstock_tmy3_release_1/building_energy_models/upgrade=10/bldg0000001-up10.zip', 'bldg1.zip') 

我還閱讀了can-i-use-boto3-anonymously並將代碼更改如下:

import boto3
from botocore import UNSIGNED
from botocore.config import Config

client = boto3.client('s3', aws_access_key_id='', aws_secret_access_key='')
client._request_signer.sign = (lambda *args, **kwargs: None)
client.download_file('oedi-data-lake', 'nrel-pds-building-stock/end-use-load-profiles-for-us-building-stock/2022/resstock_tmy3_release_1/building_energy_models/upgrade=10/bldg0000001-up10.zip', 'bldg01.zip') 

並得到 SSLCertVerificationError。

這是公司安全政策造成的嗎?

很抱歉提出了天真的問題。 AWS 上的全新產品。

太感謝了

要訪問允許匿名訪問的存儲桶,請將其配置為不使用憑據。

import boto3
from botocore import UNSIGNED
from botocore.config import Config

s3 = boto3.resource("s3", config=Config(signature_version=UNSIGNED))

# output:
# ['nrel-pds-building-stock/end-use-load-profiles-for-us-building-stock/2022/resstock_tmy3_release_1/building_energy_models/upgrade=10/bldg0000001-up10.zip']

python - 我可以匿名使用 boto3 嗎? - 堆棧溢出

是的。 您的憑據用於對您發出的所有請求進行簽名,因此您要做的就是將客戶端配置為根本不執行簽名步驟。

注意:與主題無關,AWS Python SDK 團隊不打算向資源接口添加新功能。 您可以改用客戶端界面。

資源 — Boto3 Docs 1.26.54 文檔

AWS Python SDK 團隊不打算向 boto3 中的資源接口添加新功能。 現有接口將在 boto3 的生命周期內繼續運行。 客戶可以通過客戶端界面訪問更新的服務功能。

於 2023/01/21 12:00 添加:

這是使用客戶端界面的示例代碼。

import boto3
from botocore import UNSIGNED
from botocore.config import Config

s3_client = boto3.client('s3', config=Config(signature_version=UNSIGNED))
s3_client.download_file('oedi-data-lake', 'nrel-pds-building-stock/end-use-load-profiles-for-us-building-stock/2022/resstock_tmy3_release_1/building_energy_models/upgrade=10/bldg0000001-up10.zip', 'bldg1.zip') 

謝謝你,jhashimoto!

但是通過執行以下操作,我仍然有 NoCredentialsError

import boto3
from botocore import UNSIGNED
from botocore.config import Config

s3 = boto3.resource("s3", config=Config(signature_version=UNSIGNED))

s3_client = boto3.client('s3')
s3_client.download_file('oedi-data-lake', 'nrel-pds-building-stock/end-use-load-profiles-for-us-building-stock/2022/resstock_tmy3_release_1/building_energy_models/upgrade=10/bldg0000001-up10.zip', 'bldg1.zip') 

我還閱讀了can-i-use-boto3-anonymously並將代碼更改如下:

import boto3
from botocore import UNSIGNED
from botocore.config import Config

client = boto3.client('s3', aws_access_key_id='', aws_secret_access_key='')
client._request_signer.sign = (lambda *args, **kwargs: None)
client.download_file('oedi-data-lake', 'nrel-pds-building-stock/end-use-load-profiles-for-us-building-stock/2022/resstock_tmy3_release_1/building_energy_models/upgrade=10/bldg0000001-up10.zip', 'bldg01.zip') 

並得到 SSLCertVerificationError。

這是公司安全政策造成的嗎?

很抱歉提出了天真的問題。 AWS 上的全新產品。

太感謝了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM