我有一个 Python lambda 脚本,可以在图像上传到 S3 时缩小图像。当上传的文件名包含非 ASCII 字符(在我的例子中是希伯来语)时,我无法获取该对象(禁止,就好像该文件不存在一样)。
这是我的(一些)代码:
s3_client = boto3.client('s3')
def handler(event, context):
for record in event['Records']:
bucket = record['s3']['bucket']['name']
key = record['s3']['object']['key']
s3_client.download_file(bucket, key, "/tmp/somefile")
这引发了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/ http://blog.rackspace.com/the-devnull-s3-bucket-hacking-with-aws-lambda-and-python/)就像这样,但没有运气:
key = urllib.unquote_plus(record['s3']['object']['key'])
同样的错误,尽管这次日志指出我正在尝试检索带有如下字符的密钥:פ×קס×
.
请注意,该脚本经过验证可以在英文键上运行,并且测试是在没有空格的键上完成的。
#This worked for me
import urllib.parse
encodedStr = 'My+name+is+Tarak'
urllib.parse.unquote_plus(encodedStr)
"My name is Tarak"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)