12

このコードを使用してリクエストを生成し、ダウンロード用のファイル名を設定しています。

var request = new GetPreSignedUrlRequest()
    .WithBucketName(S3BucketName)
    .WithExpires(requestExpirationTime)
    .WithKey(file.S3Key)
    .WithResponseHeaderOverrides(
        new ResponseHeaderOverrides()
            .WithContentDisposition("attachment; filename=\"Unicode FileName ᗩ Test.txt\""));

これにより、次のリンクが生成されます。

/s3path?AWSAccessKeyId=xxxx&Expires=1377199946&response-content-disposition=attachment%3B%20filename%3D"Unicode%20FileName%20ᗩ%20Test.txt"&Signature=xxxxx

このエラーが発生します:

<Error>
    <Code>InvalidArgument</Code>
    <Message>
        Header value cannot be represented using ISO-8859-1.
    </Message>
    <ArgumentValue>attachment; filename="Unicode ᗩ filename.txt"</ArgumentValue>
    <ArgumentName>response-content-disposition</ArgumentName>
    <RequestId>368BD60502854514</RequestId>
    <HostId>
        BiUUYp4d9iXfK68jKVxWZEp25m5je166M0ZY1VmoPk9pN9A69HLHcff6WIVLWk1B
    </HostId>
</Error>

response-content-disposition ヘッダーで、Unicode などの非 ISO-8859-1 文字を使用するにはどうすればよいですか?

4

2 に答える 2

6

この問題があり、ユニコード文字列を正しくエンコードすることで解決しました。

私はpython boto landにいました:

>>> import urllib
>>> encoded = urllib.quote('Unicode FileName ᗩ Test.txt')
>>> print encoded

"Unicode%20%E1%97%A9%20filename.txt"

次に、このエンコードされた文字列を response-content-disposition ヘッダーの値として使用します。

Java では、次の方法で同じ結果が得られると思います。

URLEncoder.encode(original_string, "UTF-8")

これがいつか他の誰かに役立つことを願っています!

于 2014-02-21T12:08:21.670 に答える
4

この StackOverflow answerで述べたように、Content-Disposition で非 ASCII 名をエンコードする相互運用可能な方法はありません。ブラウザの互換性はめちゃくちゃです。

最終的にすべてのブラウザで機能するようにする方法は、ISO-8859-1 以外のすべての文字を「-」に置き換えることです。コードは次のとおりです。

private static readonly Encoding ContentDispositionHeaderEncoding = Encoding.GetEncoding("ISO-8859-1");

public static string GetWebSafeFileName(string fileName)
{
    // We need to convert the file name to ISO-8859-1 due to browser compatibility problems with the Content-Disposition Header (see: https://stackoverflow.com/a/216777/1038611)
    var webSafeFileName = Encoding.Convert(Encoding.Unicode, ContentDispositionHeaderEncoding, Encoding.Unicode.GetBytes(fileName));

    // Furthermore, any characters not supported by ISO-8859-1 will be replaced by « ? », which is not an acceptable file name character. So we replace these as well.
    return ContentDispositionHeaderEncoding.GetString(webSafeFileName).Replace('?', '-');
}

Alex Couper の回答に続いて、.net で HttpEncoder の内部メソッドを呼び出して、ASCII 以外の文字のみをエンコードする方法を見つけました。

フレームワークの将来のバージョンで変更される可能性があるため、内部関数の呼び出しはお勧めしません。さらに、上記のように、これはすべてのブラウザーで機能するとは限りません。誰かが絶対にこれを行う必要がある場合に備えて、ここに残しておきます。

var type = typeof(System.Web.Util.HttpEncoder);
var methodInfo = type.GetMethod("UrlEncodeNonAscii", BindingFlags.NonPublic | BindingFlags.Instance, null, new [] { typeof(string), typeof(Encoding) }, null);
object[] parameters = {fileName, Encoding.UTF8};

var encoder = new System.Web.Util.HttpEncoder();

var encodedFileName = (string) methodInfo.Invoke(encoder, parameters);
于 2014-02-21T17:03:14.943 に答える