java - S3 Java-使用预签名 URL 分段上传?

标签 java amazon-web-services amazon-s3

我有一个单独的服务来管理文件和 s3 身份验证。它生成预签名的 URL,我可以在其他服务中使用它来上传(和下载)文件。

我想利用分段上传 sdk - 目前“uploadToUrl”方法似乎大部分时间都花在 getResponseCode 上,因此很难提供用户反馈。此外,在我的测试中,分段上传似乎要快得多。

理想情况下,我希望能够使用预签名 URL 而不是临时使用的 secret key /访问 key 来创建一些 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();
    }
}

最佳答案

几年后,深入研究 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_MD5CONTENT_LENGTH。)

关于java - S3 Java-使用预签名 URL 分段上传?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23369310/

相关文章:

python - 使用 boto3 连接到 S3 兼容存储

java - 客户端服务器通信问题

java - 在 Repast J 中将数据层分配给网格

forms - 使用 Amazon CDN 的 Squarespace 网站表单提交问题

amazon-web-services - 我的 Amazon ELB 上的 408 超时

javascript - 如何使用 CORS 限制 AWS S3 访问?

java - 在具有设置菜单项的 ActionBar 上禁用弹出按钮

java - 更改数据库中用户的密码

.net - 无法在 Amazon ec2 中连接 Amazon rds 数据库?

node.js - expressjs文件下载内存泄漏