I’m trying to implement code in python for uploading multiple images into an S3 bucket. With only one image I can do it normally, but when I implemented this for loop, the following error started to appear:
FileNotFoundError: [Errno 2] No such file or directory: 'filename.jpg'
And this is the function I’ve been able to develop so far with the help of AWS documentation:
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
If anyone has any ideas to solve this problem, I appreciate it.
Advertisement
Answer
Change the line
files = [f for f in listdir(path) if isfile(join(path, f))]
to
files = [join(path, f) for f in listdir(path) if isfile(join(path, f))]
should resolve your issue.