ValueError:将文件上载到s3存储桶时,文件名必须是字符串

2022-01-18 23:53:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我发送了一个excel文件作为邮递员的请求,需要上传到s3。 我通过请求访问该文件并将其发送到s3

@api_view(['POST'])
def excel_upload(request):
    print("request", request)
    excel_file = request.FILES['file'] 
    print("excel_file", excel_file) // this prints the name of the excel file i am sending in request
    upload_to_aws(excel_file,'X1excelsheets','s3_file_name')

下面是将文件上传到s3的函数

def upload_to_aws(local_file, bucket, s3_file):
    s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,
                      aws_secret_access_key=SECRET_KEY)

    try:
        s3.upload_file(local_file, bucket, s3_file)
        print("Upload Successful")
        return True
    except FileNotFoundError:
        print("The file was not found")
        return False
    except NoCredentialsError:
        print("Credentials not available")
        return False


uploaded = upload_to_aws('local_file', 'bucket_name', 's3_file_name')

我正在尝试使用这个特殊的帖子

https://medium.com/bilesanmiahmad/how-to-upload-a-file-to-amazon-s3-in-python-68757a1867c6

把事情做完。 错误:ValueError:文件名必须是字符串