如何转换 unicode 键名以便 amazon-s3 能够识别它?
在 django 应用程序中,我收到一个 POST 变量,其中包含上传到 amazon s3 的文件名。
但我收到 unicode 形式的密钥,当我将密钥传递给 boto 和 amazon s3 时,没有返回任何内容。但是,如果我用 boto 列出存储桶,并从列表中获取密钥,那么 amazon s3 将返回预期的对象。
所以我的问题是,如何将我的 unicode 键名转换为 boto 所具有的键的相同编码,以便亚马逊给我该对象。
我如何知道 boto 密钥采用什么编码?
非常感谢您的任何见解,这让我发疯。在我们添加正斜杠之前一切都运行良好,我不明白为什么这会产生影响。
这是来自终端的代码:
>>> unicode_key_name
u'missionuploads/tom-mission-4d8f9da9122ac21c17000002-4_4_11_9_38_PM.jpg'
>>> boto_key_name
'missionuploads/tom-mission-4d8f9da9122ac21c17000002-4_4_11_9_37_PM.jpg'
>>> len(unicode_key_name)
70
>>> len(boto_key_name)
70
>>> unicode_key = bucket.get_key(unicode_key_name.encode('utf-8'))
>>> unicode_key
>>> unicode_key = bucket.get_key(unicode_key_name.encode('ascii'))
>>> unicode_key
>>> boto_key = bucket.get_key(boto_key_name)
>>> boto_key
<Key: user.media.eu,missionuploads/tom-mission-4d8f9da9122ac21c17000002-4_4_11_9_37_PM.jpg>
-------- 编辑 ----------
传递 unicode 字符串工作正常,无需转换它,只需确保您正在搜索正确的关键名称!
由于我刚刚在服务器端收到密钥名称,我认为它是正确的,但错误似乎是在应用程序端保存一个名称,然后将新创建的名称(一分钟不同)发送到服务器。
In a django application, I am receiving a POST variable with a name of a file uploaded into amazon s3.
But I receive the key in unicode, and when I pass the key to boto and amazon s3, nothing is returned. However if I list the bucket with boto, and get the key from the list, then amazon s3 returns the object expected.
So my question is, how can I convert my unicode keyname into the same encoding of the key that boto has so that amazon will give me the object.
How can I know what encoding the boto key is in?
Thanks a lot for any insight, this one is driving me crazy. It all worked fine before we added the forward slash and I can't see why that would make a difference.
Here is the code from the terminal:
>>> unicode_key_name
u'missionuploads/tom-mission-4d8f9da9122ac21c17000002-4_4_11_9_38_PM.jpg'
>>> boto_key_name
'missionuploads/tom-mission-4d8f9da9122ac21c17000002-4_4_11_9_37_PM.jpg'
>>> len(unicode_key_name)
70
>>> len(boto_key_name)
70
>>> unicode_key = bucket.get_key(unicode_key_name.encode('utf-8'))
>>> unicode_key
>>> unicode_key = bucket.get_key(unicode_key_name.encode('ascii'))
>>> unicode_key
>>> boto_key = bucket.get_key(boto_key_name)
>>> boto_key
<Key: user.media.eu,missionuploads/tom-mission-4d8f9da9122ac21c17000002-4_4_11_9_37_PM.jpg>
-------- EDIT ----------
Passing the unicode string works fine there is no need to convert it, just make sure you are searching for the right key name!
Since I just received the key name on the server side I assumed it was correct, but the error appears to be in the app side saving with one name and then sending a newly created name (one minute different) to the server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
'37'
和'38'
是不同的字符串。当您使用正确的值时,它可能会正常工作。'37'
and'38'
are different strings. It will probably work fine when you use the correct value.