简体   繁体   中英

Facing issue with boto3 in uploading file to s3 bucket

I have made a simple script in which I am uploading a file on the s3 bucket. And my code is as follows which is very straight forward.

bucket = "my-bucket"
file_name = "/my-file-path/download.jpeg"
key_name = None
s3 = boto3.client("s3")

if key_name is None:
    key_name = file_name.split('/')[-1]
s3.upload_file(file_name, bucket, key_name)

So what I want to achieve is like if anyone will not pass the key name or key name is None then filename becomes the key name and uploaded on the s3 bucket.

Above code works fine if I pass key name but when I pass None in key name then it won't work and I debug my code and I found this

-> key_name = file_name.split('/')[-1]
(Pdb) key_name
'download.jpeg'
(Pdb) next
--Call--
> /usr/lib/python3.6/threading.py(1279)_shutdown()
-> def _shutdown():
(Pdb) 
> /usr/lib/python3.6/threading.py(1285)_shutdown()
-> tlock = _main_thread._tstate_lock

And at the end file won't upload on s3. If anyone have any idea regarding this then please help. Your help will be appreciated for sure.

I would put that in a function and test. For example:

def upload(file_name, bucket="my-bucket",key_name=None):
    if not key_name:
        key_name = file_name.split('/')[-1]
    s3 = boto3.client("s3")
    s3.upload_file(file_name, bucket, key_name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM