s3存储桶中的utf-8文件名 [英] utf-8 filename in s3 bucket

查看:125
本文介绍了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:

摘自官方文档( http://docs.aws.amazon.com/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!

来自 http://www.bennadel.com/blog/2591-embedding-foreign-characters-in-your-content-disposition-filename-header.htm

From http://www.bennadel.com/blog/2591-embedding-foreign-characters-in-your-content-disposition-filename-header.htm and http://www.bennadel.com/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 http://docs.aws.amazon.com/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 {
            //http://www.bennadel.com/blog/2696-overriding-content-type-and-content-disposition-headers-in-amazon-s3-pre-signed-urls.htm
            //http://www.bennadel.com/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-8文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆