简体   繁体   English

Boto3在将文件上传到S3存储桶时遇到问题

[英]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. 我编写了一个简单的脚本,在其中将文件上传到s3存储桶中。 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. 因此,我想实现的目标是,如果任何人都不会传递密钥名称或密钥名称为None,那么filename就会成为密钥名称并上传到s3存储桶中。

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 如果我通过密钥名,上面的代码可以正常工作,但是当我在密钥名中通过None时,它将无法工作,并且我调试了代码,我发现了

-> 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. 最后,文件不会在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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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