s3存储桶中的utf

编程入门 行业动态 更新时间:2024-10-28 10:37:15
本文介绍了s3存储桶中的utf-8文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以使用utf-8编码的名称(例如åøæ.jpg")向s3添加密钥?

Is it possible to add a key to s3 with an utf-8 encoded name like "åøæ.jpg"?

使用boto上传时出现以下错误:

I'm getting the following error when uploading with boto:

<Error><Code>InvalidURI</Code><Message>Couldn't parse the specified URI.</Message>

推荐答案

@ 2083:这是一个古老的问题,但是如果您还没有找到解决方案,那么对于像我这样来这里的其他所有人,我都在寻找答案:

@2083: This is a bit of an old question, but if you haven't found the solution, and for everyone else that comes here like me looking for an answer:

摘自官方文档( docs.aws.amazon/AmazonS3/latest/dev/UsingMetadata.html ):

尽管您可以在对象键名称中使用任何UTF-8字符,但是遵循关键的命名最佳做法有助于确保最大的兼容性与其他应用程序.每个应用程序都可以解析特殊字符不同.以下准则可帮助您最大限度地提高符合DNS,网络安全字符,XML解析器和其他API.

Although you can use any UTF-8 characters in an object key name, the following key naming best practices help ensure maximum compatibility with other applications. Each application may parse special characters differently. The following guidelines help you maximize compliance with DNS, web safe characters, XML parsers, and other APIs.

安全字符

以下字符集通常可以安全地用于键名:

The following character sets are generally safe for use in key names:

字母数字字符[0-9a-zA-Z]

Alphanumeric characters [0-9a-zA-Z]

特殊字符!,-,_,.,*,',(和)

Special characters !, -, _, ., *, ', (, and )

以下是有效对象键名的示例:

The following are examples of valid object key names:

4my-organization

4my-organization

my.great_photos-2014/jan/myvacation.jpg

my.great_photos-2014/jan/myvacation.jpg

videos/2014/生日/video1.wmv

videos/2014/birthday/video1.wmv

但是,如果像我一样,您真正想要的是一个文件名,该文件名可以使用UTF-8字符(请注意,该名称可以与键名不同).您有办法做到!

However, if what you really want, like me, is a filename that allows UTF-8 characters (note that this can be different from the key name). You have a way to do it!

来自 www.bennadel/blog/2591-embedding-foreign-characters-in-your-content-disposition-filename-header.htm 和 www.bennadel/blog/2696-overrideing-content-type-and-content-disposition-headers-amazon-s3-pre-signed-urls.htm (对Ben Nadal表示敬意)可以通过确保下载文件后,S3将覆盖 Content-Disposition 标头.

From www.bennadel/blog/2591-embedding-foreign-characters-in-your-content-disposition-filename-header.htm and www.bennadel/blog/2696-overriding-content-type-and-content-disposition-headers-in-amazon-s3-pre-signed-urls.htm (Kudos to Ben Nadal) you can do that by making sure that when downloading the file, S3 will override the Content-Disposition header.

正如我在Java中所做的那样,我在此处包括了代码,我相信您将能够轻松将其转换为Python :):

As I have done it in java, I include here the code, I'm sure you'll be able to easily translate it to Python :) :

AmazonS3 s3 = S3Controller.getS3Client(); //as per docs.aws.amazon/AmazonS3/latest/dev/UsingMetadata.html String key = fileName.substring(fileName.indexOf("-")).replaceAll("[^a-zA-Z0-9._]", ""); PutObjectRequest putObjectRequest = new PutObjectRequest( S3Controller.bucketNameForBucket(S3Controller.Bucket.EXPORT_BUCKET), key, file); // we can always regenerate these files, so we can used reduced redundancy storage putObjectRequest.setStorageClass(StorageClass.Standard); String urlEncodedUTF8Filename = key; try { //www.bennadel/blog/2696-overriding-content-type-and-content-disposition-headers-in-amazon-s3-pre-signed-urls.htm //www.bennadel/blog/2591-embedding-foreign-characters-in-your-content-disposition-filename-header.htm //Issue#179 urlEncodedUTF8Filename = URLEncoder.encode(fileName.substring(fileName.indexOf("-")), "UTF-8"); } catch (UnsupportedEncodingException e) { LOG.warn("Could not URLEncode a filename. Original Filename: " + fileName, e ); } ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentDisposition("attachment; filename=\"" + key + "\"; filename*=UTF-8''"+ urlEncodedUTF8Filename); putObjectRequest.setMetadata(metadata); s3.putObject(putObjectRequest);

它应该可以帮助:)

更多推荐

s3存储桶中的utf

本文发布于:2023-10-23 15:56:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521312.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:utf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!