python - ValueError:将文件上传到 s3 存储桶时,文件名必须是字符串
问题描述
我在邮递员中发送一个 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:文件名必须是字符串
解决方案
我使用AWSCLI进行了配置
S3_BUCKET_NAME = 'YOUR_BUCKET'
s3 = boto3.client('s3')
with open(file, 'rb') as f:
s3.upload_fileobj(f, S3_BUCKET_NAME, file)
我遇到了同样的错误,所以我只使用了upload_fileobj而不是upload_file。这对我来说很好,你可以试试。
推荐阅读
- javascript - 如何将此类扩展到 redux?
- r - 无法在谷歌云平台上使用 Rstudio 部署 R 模型
- arrays - 如何在 Delphi 中将 Byte 数组转换为 PByte?
- bootstrap-4 - 有人可以在这里解决我的引导弹出问题吗?
- python-3.x - 如何使用线程同时处理python列表的所有数据?
- swift - 如何将可可应用程序嵌入另一个可可应用程序并将其作为子进程启动
- java - PMD 工具是否可以选择显示违规计数的差异(代码更改前后)?
- dialogflow-es - webhook 请求超时 dialogflow v2 迁移(webhook 代码:4)?
- .net - 如何使用经典的 VS csproj 定位 .NET Core?
- java - Delphi 10.2 TNetHTTPClient 在其时间之前超时