我在與 AWS 的 s3 客戶端互動時遇到了一些問題。我只是想設定客戶端,但我收到一條奇怪的錯誤訊息。
我正在嘗試執行的步驟
第一步:匯入包
from pkg_dir.config import *
from pkg_dir.src.utils import *
import boto3
第 2 步:讀取憑證以連接到 AWS
creds = read_yaml(creds_file_path)
此步驟導致將以下 dict 保存在creds變數中:
{
'aws': {
'aws_access_key_id': 'XXX',
'aws_secret_access_key': 'YYY'
}
}
第 3 步:為 AWS 創建會話
dev = boto3.Session(
aws_access_key_id=creds['aws']['aws_access_key_id'],
aws_secret_access_key=creds['aws']['aws_secret_access_key'],
)
第 4 步:為 S3 設定客戶端
s3 = dev.client('s3')
當我運行此行時,我收到以下錯誤:
Traceback (most recent call last):
File ~/Documents/ReposRob_RobPer/personal/kaggle/kgl_space_titanic/.venv/lib/python3.9/site-packages/IPython/core/interactiveshell.py:3398 in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
Input In [6] in <cell line: 1>
dev.client('s3')
File ~/Documents/ReposRob_RobPer/personal/kaggle/kgl_space_titanic/.venv/lib/python3.9/site-packages/boto3/session.py:299 in client
return self._session.create_client(
File ~/Documents/ReposRob_RobPer/personal/kaggle/kgl_space_titanic/.venv/lib/python3.9/site-packages/botocore/session.py:948 in create_client
client = client_creator.create_client(
File ~/Documents/ReposRob_RobPer/personal/kaggle/kgl_space_titanic/.venv/lib/python3.9/site-packages/botocore/client.py:112 in create_client
cls = self._create_client_class(service_name, service_model)
File ~/Documents/ReposRob_RobPer/personal/kaggle/kgl_space_titanic/.venv/lib/python3.9/site-packages/botocore/client.py:168 in _create_client_class
self._event_emitter.emit(
File ~/Documents/ReposRob_RobPer/personal/kaggle/kgl_space_titanic/.venv/lib/python3.9/site-packages/botocore/hooks.py:412 in emit
return self._emitter.emit(aliased_event_name, **kwargs)
File ~/Documents/ReposRob_RobPer/personal/kaggle/kgl_space_titanic/.venv/lib/python3.9/site-packages/botocore/hooks.py:256 in emit
return self._emit(event_name, kwargs)
File ~/Documents/ReposRob_RobPer/personal/kaggle/kgl_space_titanic/.venv/lib/python3.9/site-packages/botocore/hooks.py:239 in _emit
response = handler(**kwargs)
File ~/Documents/ReposRob_RobPer/personal/kaggle/kgl_space_titanic/.venv/lib/python3.9/site-packages/boto3/utils.py:65 in _handler
module = import_module(module)
File ~/Documents/ReposRob_RobPer/personal/kaggle/kgl_space_titanic/.venv/lib/python3.9/site-packages/boto3/utils.py:56 in import_module
__import__(name)
File ~/Documents/ReposRob_RobPer/personal/kaggle/kgl_space_titanic/.venv/lib/python3.9/site-packages/boto3/s3/inject.py:233
return self.meta.client.upload_file(,
^
SyntaxError: invalid syntax
一件奇怪的事情是,當我運行這些其他代碼行時,我沒有得到同樣的錯誤:
dev.client('ec2')
dev.client('machinelearning')
似乎問題只發生在s3服務上
配置
安裝要求:
命令:
pip3 freeze
結果:
anyio==3.6.1
appnope==0.1.3
argon2-cffi==21.3.0
argon2-cffi-bindings==21.2.0
asttokens==2.0.5
attrs==21.4.0
awscli==1.25.10
Babel==2.10.1
backcall==0.2.0
beautifulsoup4==4.11.1
bleach==5.0.0
boto==2.49.0
boto3==1.24.10
botocore==1.27.10
certifi==2022.5.18.1
cffi==1.15.0
charset-normalizer==2.0.12
colorama==0.4.4
cycler==0.11.0
debugpy==1.6.0
decorator==5.1.1
defusedxml==0.7.1
docutils==0.16
entrypoints==0.4
et-xmlfile==1.1.0
executing==0.8.3
fastjsonschema==2.15.3
fonttools==4.33.3
idna==3.3
importlib-metadata==4.11.4
ipykernel==6.13.0
ipython==8.4.0
ipython-genutils==0.2.0
jedi==0.18.1
Jinja2==3.1.2
jmespath==1.0.0
json5==0.9.8
jsonschema==4.6.0
jupyter-client==7.3.1
jupyter-core==4.10.0
jupyter-server==1.17.0
jupyterlab==3.1.0
jupyterlab-pygments==0.2.2
jupyterlab-server==2.14.0
kaggle==1.5.12
kiwisolver==1.4.2
MarkupSafe==2.1.1
matplotlib==3.5.2
matplotlib-inline==0.1.3
mistune==0.8.4
mysql-connector-python==8.0.29
nbclassic==0.3.7
nbclient==0.6.4
nbconvert==6.5.0
nbformat==5.4.0
nest-asyncio==1.5.5
notebook==6.4.11
notebook-shim==0.1.0
numpy==1.22.4
openpyxl==3.0.10
packaging==21.3
pandas==1.4.2
pandocfilters==1.5.0
parso==0.8.3
pexpect==4.8.0
pickleshare==0.7.5
Pillow==9.1.1
prometheus-client==0.14.1
prompt-toolkit==3.0.29
protobuf==4.21.1
psutil==5.9.1
psycopg2==2.9.3
ptyprocess==0.7.0
pure-eval==0.2.2
pyasn1==0.4.8
pycparser==2.21
Pygments==2.12.0
pyparsing==3.0.9
pyrsistent==0.18.1
python-dateutil==2.8.2
python-slugify==6.1.2
pytz==2022.1
PyYAML==5.4.1
pyzmq==23.1.0
requests==2.27.1
rsa==4.7.2
s3transfer==0.6.0
scipy==1.8.1
seaborn==0.11.2
Send2Trash==1.8.0
six==1.16.0
sniffio==1.2.0
soupsieve==2.3.2.post1
stack-data==0.2.0
terminado==0.15.0
text-unidecode==1.3
tinycss2==1.1.1
tornado==6.1
tqdm==4.64.0
traitlets==5.2.2.post1
Unidecode==1.3.4
urllib3==1.26.9
wcwidth==0.2.5
webencodings==0.5.1
websocket-client==1.3.2
zipp==3.8.0
蟒蛇版本
命令:
python -V
結果:
Python 3.9.1
補充說明
- 我
virtualenv用來處理包裹
參考文獻
- 我一直在嘗試在其他帖子中找到解決此問題的方法,但沒有成功。這些是我檢查過的一些參考資料,但沒有幫助我解決問題:
- 使用 python3.7.0 從 s3 下載檔案失敗
- 無法在 boto3 中創建 s3 資源/客戶端
- 呼叫 boto3.client('s3') 時出現語法錯誤
- boto3 v1.9.244 是否支持創建“s3”資源?
- 策略中的 AWS Boto3 語法錯誤
- 這些帖子中的大多數都解決了處理名為
futures. 但是,我什至沒有安裝那個。
希望你能幫助我:)
uj5u.com熱心網友回復:
看起來boto3您安裝的版本不知何故在行尾多出了一個逗號。
有問題的行末尾沒有逗號。此外,根據 Git 歷史,這條線自 2015 年以來沒有改變。
如果我發現自己處于這種情況,我會嘗試卸載并重新安裝boto3. 我不確定是否值得手動編輯有問題的檔案并洗掉逗號,因為此檔案或boto3.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/493367.html
