python-3.x - 如何使用 python 3.8 将文件上传到 AWS S3 子文件夹?
问题描述
我想将文件上传到 AWS S3 中存储桶中的子文件夹。我正在使用下面给出的代码。
filename = '/tmp/' + 'image' + '.png'
file_obj = open(filename, 'rb')
s3_upload = s3.put_object( Bucket="aaa/Bbb/cc-cc", Key="filename.png", Body=file_obj)
return
{
'statusCode': 200,
'body': json.dumps("Executed Successfully"
}
将文件上传到 AWS S3 Bucket 的子文件夹的上述概念可以正常工作,Node.js
但在实施时python
会出现以下错误。
Parameter validation failed:\nInvalid bucket name \"aaa/Bbb/cc-cc\": Bucket name must match the regex \"^[a-zA-Z0-9.\\-_]{1,255}$\" or be an ARN matching the regex \"^arn:(aws).*:s3:[a-z\\-0-9]+:[0-9]{12}:accesspoint[/:][a-zA-Z0-9\\-]{1,63}$|^arn:(aws).*:s3-outposts:[a-z\\-0-9]+:[0-9]{12}:outpost[/:][a-zA-Z0-9\\-]{1,63}[/:]accesspoint[/:][a-zA-Z0-9\\-]{1,63}$\"
解决方案
在您的情况下,存储桶名称只是aaa
,而/Bbb/cc-cc
应该是键名的一部分:
s3_upload = s3.put_object( Bucket="aaa", Key="/Bbb/cc-cc/filename.png", Body=file_obj)
推荐阅读
- python - 在 Python Ubuntu 上更改目录时出错
- ios - WKWebview 密码 API 自动填充
- powershell - PowerShell 模块:如何强制执行最低 .NET Framework 版本?
- javascript - 如何使用 THREE.js 加载 SVGLoader
- html - CSS:在多个对象周围创建边框
- python - Cython - 有效过滤类型化的内存视图
- c - 数组布尔值如何将 Char 条目与真或假相关联?
- javascript - 不能将数组中的句子分成它们自己的索引或变量吗?从 Sharepoint 列表中提取数据
- css - -ms-grid-columns 正在堆叠列
- javascript - [a,b].reduce(f,x) 代码到 [a,b].reduce(f) 使用基于传感器 /CPS 的功能引用?