S3Java-使用预先指定的URL进行多部分上载?
我有一个单独的服务来管理文件和s3身份验证。它生成预先签名的URL,我可以在其他服务中使用这些URL上载(和下载)文件 我想利用Multipart upload sdk——目前“uploadToUrl”方法似乎将大部分时间花在getResponseCode上,因此很难提供用户反馈。而且,在我的测试中,多部分上传似乎要快得多 理想情况下,我希望能够使用预先指定的URL创建一些AWSCredentials,而不是临时使用的密钥/访问密钥。那只是白日梦吗S3Java-使用预先指定的URL进行多部分上载?,java,amazon-web-services,amazon-s3,Java,Amazon Web Services,Amazon S3,我有一个单独的服务来管理文件和s3身份验证。它生成预先签名的URL,我可以在其他服务中使用这些URL上载(和下载)文件 我想利用Multipart upload sdk——目前“uploadToUrl”方法似乎将大部分时间花在getResponseCode上,因此很难提供用户反馈。而且,在我的测试中,多部分上传似乎要快得多 理想情况下,我希望能够使用预先指定的URL创建一些AWSCredentials,而不是临时使用的密钥/访问密钥。那只是白日梦吗 //s3 service public URL
//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();
}
}
目前,我正在探索返回由受信任代码返回的临时凭据,而不是经过签名的url 使用PHP,您可以
$command = $this->s3client->getCommand ('CreateMultipartUpload', array (
'Bucket' => $this->rootBucket,
'Key' => $objectName
));
$signedUrl = $command->createPresignedUrl ('+5 minutes');
但到目前为止,我还没有找到用Java实现这一点的方法。
对于单个PUT(或GET)操作,可以使用
generatePresignDurl
,但我不知道如何将其应用于多部分上传,就像PHPgetCommand('createMultipartPload'/'UploadPart'/'CompleteMultipartUpload')
方法一样,但是深入研究AWS Java SDK发现,将以下内容添加到generateDesignedUrlRequest
中效果非常好:
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\u MD5
还是CONTENT\u LENGTH
)!希望能有更好的记录。