gpt4 book ai didi

java - 使用 HTTP POST 将文件上传到 S3 存储桶

转载 作者:行者123 更新时间:2023-11-30 06:30:47 35 4
gpt4 key购买 nike

我正在开发一个应用程序,该应用程序将使用 Amazon 的 S3 存储。在遵循亚马逊提供的教程和示例后,我仍然发现自己无法使上传工作。这是我不断收到的消息:不支持您提供的授权机制。请使用 AWS4-HMAC-SHA256。 该存储桶名为 testbucket-10.09.2017,位于 Frankfurt (eu-central-1) 区域。我什至找到了application声称完全符合我的需要,但错误消息是相同的。下面的大部分代码取自并改编自 AWS 提供的文档和教程。任何帮助将不胜感激。

这是我正在使用的代码:

我的 HTML 表单:

<html> 
<head>
<title>S3 POST Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>
<form action="https://testbucket-10.09.2017.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="uploads/${filename}">
<input type="hidden" name="AWSAccessKeyId" value="REMOVED FOR SAFETY">
<input type="hidden" name="acl" value="private">
<input type="hidden" name="success_action_redirect" value="http://localhost/">
<input type="hidden" name="policy" value="ZXlKbGVIQnBjbUYwYVc5dUlqb2dJakl3TVRndE1ERXRNREZVTURBNk1EQTZNREJhSWl3S0lDQWlZMjl1WkdsMGFXOXVjeUk2SUZzZ0NpQWdJQ0I3SW1KMVkydGxkQ0k2SUNKMFpYTjBZblZqYTJWMExURXdMakE1TGpJd01UY2lmU3dnQ2lBZ0lDQmJJbk4wWVhKMGN5MTNhWFJvSWl3Z0lpUnJaWGtpTENBaWRYQnNiMkZrY3k4aVhTd0tJQ0FnSUhzaVlXTnNJam9nSW5CeWFYWmhkR1VpZlN3S0lDQWdJSHNpYzNWalkyVnpjMTloWTNScGIyNWZjbVZrYVhKbFkzUWlPaUFpYUhSMGNEb3ZMMnh2WTJGc2FHOXpkQzhpZlN3S0lDQWdJRnNpYzNSaGNuUnpMWGRwZEdnaUxDQWlKRU52Ym5SbGJuUXRWSGx3WlNJc0lDSWlYU3dLSUNBZ0lGc2lZMjl1ZEdWdWRDMXNaVzVuZEdndGNtRnVaMlVpTENBd0xDQXhNRFE0TlRjMlhRb2dJRjBLZlE9PQ==">
<input type="hidden" name="signature" value="REMOVED FOR SAFETY">
<input type="hidden" name="Content-Type" value="image/jpeg">
<!-- Include any additional input fields here -->

File to upload to S3:
<input name="file" type="file">
<br>
<input type="submit" value="Upload File to S3">
</form>
</body>
</html>

我的 Java 代码生成策略和签名:

public static void myAttempt() throws Exception {

String policy_document = constructPolicy();
String aws_secret_key="REMOVED FOR SAFETY";

String policy = (new BASE64Encoder()).encode(
policy_document.getBytes("UTF-8")).replaceAll("\n","").replaceAll("\r","");

String dateStamp ="20170912";
String region = "eu-central-1";
String serviceName ="s3";
System.out.println("NEW SIGNATURE: "+getSignature(getSignatureKey(aws_secret_key,dateStamp,region,serviceName)));

System.out.println("ENCODED POLICY: "+policy);
}

private static String constructPolicy() throws UnsupportedEncodingException {

String policy_document="{\"expiration\": \"2018-01-01T00:00:00Z\",\n" +
" \"conditions\": [ \n" +
" {\"bucket\": \"testbucket-10.09.2017\"}, \n" +
" [\"starts-with\", \"$key\", \"uploads/\"],\n" +
" {\"acl\": \"private\"},\n" +
" {\"success_action_redirect\": \"http://localhost/\"},\n" +
" [\"starts-with\", \"$Content-Type\", \"\"],\n" +
" [\"content-length-range\", 0, 1048576]\n" +
" ]\n" +
"}";

String policy = (new BASE64Encoder()).encode(
policy_document.getBytes("UTF-8")).replaceAll("\n","").replaceAll("\r","");
return policy;
}

private static byte[] HmacSHA256(String data, byte[] key) throws Exception {
String algorithm="HmacSHA256";
Mac mac = Mac.getInstance(algorithm);
mac.init(new SecretKeySpec(key, algorithm));
return mac.doFinal(data.getBytes("UTF8"));
}

private static byte[] getSignatureKey(String key, String dateStamp, String regionName, String serviceName) throws Exception {
byte[] kSecret = ("AWS4" + key).getBytes("UTF8");
byte[] kDate = HmacSHA256(dateStamp, kSecret);
byte[] kRegion = HmacSHA256(regionName, kDate);
byte[] kService = HmacSHA256(serviceName, kRegion);
byte[] kSigning = HmacSHA256("aws4_request", kService);
return kSigning;
}

private static String getSignature(byte[] key) throws Exception{

return base16().lowerCase().encode(HmacSHA256(constructPolicy(), key));
}

最佳答案

事实证明,由于某种原因,过时的 AWS 文档和示例是搜索时的第一个结果。几个谷歌结果页面之后,出现了一个更新的示例。基本上我使用的表格是错误的。正确的是:

<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

</head>
<body>

<form action="http://sigv4examplebucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
Key to upload:
<input type="input" name="key" value="user/user1/${filename}" /><br />
<input type="hidden" name="acl" value="public-read" />
<input type="hidden" name="success_action_redirect" value="http://sigv4examplebucket.s3.amazonaws.com/successful_upload.html" />
Content-Type:
<input type="input" name="Content-Type" value="image/jpeg" /><br />
<input type="hidden" name="x-amz-meta-uuid" value="14365123651274" />
<input type="hidden" name="x-amz-server-side-encryption" value="AES256" />
<input type="text" name="X-Amz-Credential" value="AKIAIOSFODNN7EXAMPLE/20151229/us-east-1/s3/aws4_request" />
<input type="text" name="X-Amz-Algorithm" value="AWS4-HMAC-SHA256" />
<input type="text" name="X-Amz-Date" value="20151229T000000Z" />

Tags for File:
<input type="input" name="x-amz-meta-tag" value="" /><br />
<input type="hidden" name="Policy" value='<Base64-encoded policy string>' />
<input type="hidden" name="X-Amz-Signature" value="<signature-value>" />
File:
<input type="file" name="file" /> <br />
<!-- The elements after this will be ignored -->
<input type="submit" name="submit" value="Upload to Amazon S3" />
</form>

</html>

Here是我获取表单以及可以找到其他示例的链接。

关于java - 使用 HTTP POST 将文件上传到 S3 存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46178220/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com