我有一个单独的服务,即管理文件和s3身份验证。它产生预签名的URL,我可以在其他服务中使用它来上传(和下载)文件。
我想利用Multipart上传sdk-目前的' uploadToUrl'方法似乎将大部分时间花在getResponseCode上,因此很难提供用户反馈。此外,我的测试中的分段上传似乎要快得多。
理想情况下,我希望能够使用预先签名的URL而不是秘密密钥/访问密钥来创建一些AWSCredentials以供临时使用。这只是一个梦想吗?
//s3 service
public URL getUrl(String bucketName, String objectKey, Date expiration, AmazonS3 s3Client, HttpMethod method, String contentType) {
GeneratePresignedUrlRequest generatePresignedUrlRequest;
generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey);
generatePresignedUrlRequest.setMethod(method);
generatePresignedUrlRequest.setExpiration(expiration);
generatePresignedUrlRequest.setContentType(contentType);
URL s = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
System.out.println(String.format("Generated Presigned URL: %n %S", s.toString()));
return s;
}
//Upload service
Override
public void uploadToUrl(URL url, File file) {
HttpURLConnection connection;
try {
InputStream inputStream = new FileInputStream(file);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
OutputStream out =
connection.getOutputStream();
byte[] buf = new byte[1024];
int count;
int total = 0;
long fileSize = file.length();
while ((count =inputStream.read(buf)) != -1)
{
if (Thread.interrupted())
{
throw new InterruptedException();
}
out.write(buf, 0, count);
total += count;
int pctComplete = new Double(new Double(total) / new Double(fileSize) * 100).intValue();
System.out.print("\r");
System.out.print(String.format("PCT Complete: %d", pctComplete));
}
System.out.println();
out.close();
inputStream.close();
System.out.println("Finishing...");
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
System.out.printf("Successfully uploaded.");
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
目前我正在探索返回由我信任的代码而不是签名网址返回的临时凭证。
http://docs.aws.amazon.com/AmazonS3/latest/dev/AuthUsingTempSessionTokenJava.html
答案 1 :(得分:0)
使用PHP,你可以
$command = $this->s3client->getCommand ('CreateMultipartUpload', array (
'Bucket' => $this->rootBucket,
'Key' => $objectName
));
$signedUrl = $command->createPresignedUrl ('+5 minutes');
但到目前为止,我发现如何用Java实现这一目标。
对于单个PUT(或GET)操作,可以使用generatePresignedUrl
,但我不知道如何将其应用于多部分上传,就像使用PHP getCommand ('CreateMultipartUpload'/'UploadPart'/'CompleteMultipartUpload')
方法一样。
答案 2 :(得分:0)
几年后,但深入研究AWS Java SDK,发现在GeneratePresignedUrlRequest
中添加以下内容效果很好:
AmazonS3Client amazonS3Client = /* ... */;
GeneratePresignedUrlRequest request = /* ... */;
// the following are required to trigger the multipart upload API
request.addRequestParameter("uploadId", uploadIdentifier);
request.addRequestParameter("partNumber", Integer.toString(partNumber));
// the following may be optional but are recommended to validate data integrity during upload
request.putCustomRequestHeader(Headers.CONTENT_MD5, md5Hash);
request.putCustomRequestHeader(Headers.CONTENT_LENGTH, Long.toString(contentLength));
URL presignedURL = amazonS3Client.generatePresignedUrl(request);
(我还没有深入研究以确定是否需要CONTENT_MD5
或CONTENT_LENGTH
。)