无法在带有java sdk的aws s3上使用预先签名的url上载
我收到一个预先签名的URL,要上传到S3。当我上传下面的代码时,我得到了403状态响应。我尝试在web控制台上将bucket策略设置为public,但这并没有解决问题。关于如何解决这个问题还有其他见解吗?我还尝试将ACL添加到PublicREADWRITE无法在带有java sdk的aws s3上使用预先签名的url上载,java,amazon-web-services,amazon-s3,aws-sdk,Java,Amazon Web Services,Amazon S3,Aws Sdk,我收到一个预先签名的URL,要上传到S3。当我上传下面的代码时,我得到了403状态响应。我尝试在web控制台上将bucket策略设置为public,但这并没有解决问题。关于如何解决这个问题还有其他见解吗?我还尝试将ACL添加到PublicREADWRITE HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
OutputStream out = connection.getOutputStream();
// OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
//out.write("This text uploaded as an object via presigned URL.");
byte[] boundaryBytes = Files.readAllBytes(Paths.get(edmFile));
out.write(boundaryBytes);
out.close();
// Check the HTTP response code. To complete the upload and make the object available,
// you must interact with the connection object in some way.
int responseCode = connection.getResponseCode();
System.out.println("HTTP response code: " + responseCode);
预先签署的网址:
private URL getUrl(String bucketName, String objectKey) {
String clientRegion = "us-east-1";
java.util.Date expiration = new java.util.Date();
long expTimeMillis = expiration.getTime();
expTimeMillis += 1000 * 60 * 10;
expiration.setTime(expTimeMillis);
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new ProfileCredentialsProvider())
.withRegion(clientRegion)
.build();
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(bucketName, objectKey)
.withMethod(HttpMethod.GET)
.withExpiration(expiration);
URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
System.out.println("Pre-Signed URL: " + url.toString());
return url;
}
如前所述,签名的Url应该与您接下来要做的事情完全匹配 您的预签名Url是通过
GET
操作创建的,这就是为什么上载PUT
操作失败并出现访问被拒绝错误的原因
尝试更新要放置的withMethod
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(bucketName, objectKey)
.withMethod(HttpMethod.PUT)
.withExpiration(expiration);
添加用于生成预签名url的代码。已添加@cementblocks@alihaider您的预签名Url是使用
.withMethod(HttpMethod.GET)
创建的,您正在尝试执行PUT上载操作。这就是原因吗?。我想它们必须是同步的。@Imran如果你加上它作为答案,我会接受它-谢谢