python - 如何将文件夹中的多张图片上传到 S3 Bucket?
问题描述
我正在尝试在 python 中实现代码以将多个图像上传到 S3 存储桶中。只有一张图片我可以正常做,但是当我实现这个for循环时,开始出现以下错误:
FileNotFoundError: [Errno 2] No such file or directory: 'filename.jpg'
这是迄今为止我在 AWS 文档的帮助下能够开发的功能:
def upload_file(path, bucket, object_name=None):
"""
Upload files to an S3 bucket
:param bucket: Bucket to upload to
:param path: Path of the folder with files to upload
:param object_name: S3 object name. If not specified, then filename is used
:return: True if file was uploaded, else False
"""
# S3 bucket connection
s3_client = boto3.client('s3')
# List files from a folder
files = [f for f in listdir(path) if isfile(join(path, f))]
try:
# Upload the image
for file in files:
s3_client.upload_file(file, bucket, object_name)
except ClientError as e:
logging.error(e)
return False
return True
如果有人有任何想法来解决这个问题,我很感激。
解决方案
换行
files = [f for f in listdir(path) if isfile(join(path, f))]
到
files = [join(path, f) for f in listdir(path) if isfile(join(path, f))]
应该可以解决您的问题。
推荐阅读
- html - 如何为溢出的文本提供最少 2 行的空间?
- android - 如何使用 sqlLite 全外连接
- ubuntu - 如何链接到 LibZip?
- javascript - Javascript Prototype based class in default based class 可以使用吗?
- pytest - 在哪里放置 pytest_generate_tests 钩子
- sql - 如何在SSIS中的每一行中生成具有不同数据类型的固定宽度文件
- html - 用一些空白将它们垂直分开
- ibm-blockchain - 通过 VSCode 扩展连接到本地版本的 Hyperledger Fabric
- python - Qt 错误:发出信号 QQmlEngine::quit(),但没有连接接收器来处理它
- java - 我无法从禁用的文本中获取文本值