Ode*_*Niv 3 python amazon-s3 amazon-web-services aws-lambda
我有一个 Python lambda 脚本,可以在图像上传到 S3 时缩小图像。当上传的文件名包含非 ASCII 字符(在我的例子中是希伯来语)时,我无法获取该对象(禁止,就好像该文件不存在一样)。
\n\n这是我的(一些)代码:
\n\ns3_client = boto3.client(\'s3\')\ndef handler(event, context):\n for record in event[\'Records\']:\n bucket = record[\'s3\'][\'bucket\'][\'name\']\n key = record[\'s3\'][\'object\'][\'key\']\n s3_client.download_file(bucket, key, "/tmp/somefile")\nRun Code Online (Sandbox Code Playgroud)\n\n这引发了An error occurred (403) when calling the HeadObject operation: Forbidden: ClientError. 我还在日志中看到该密钥包含诸如 之类的字符%D7%92。
在网络上,我还尝试根据一些来源(http://blog.rackspace.com/the-devnull-s3-bucket-hacking-with-aws-lambda-and-python/)取消引用密钥,如下所示没有运气:
\n\nkey = urllib.unquote_plus(record[\'s3\'][\'object\'][\'key\'])\nRun Code Online (Sandbox Code Playgroud)\n\n同样的错误,尽管这次日志指出我正在尝试检索包含如下字符的密钥:\xc3\x97\xc2\xa4\xc3\x97\xc3\x97\xc2\xa7\xc3\x97\xc2\xa1\xc3\x97。
请注意,该脚本经过验证可以在英文键上运行,并且测试是在没有空格的键上完成的。
\n小智 5
#This worked for me
import urllib.parse
encodedStr = 'My+name+is+Tarak'
urllib.parse.unquote_plus(encodedStr)
"My name is Tarak"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5154 次 |
| 最近记录: |