【问题标题】:S3 Java- Multipart upload using a presigned URL?S3 Java- 使用预签名 URL 进行分段上传?
【发布时间】:2014-04-29 15:43:29
【问题描述】:

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

我想利用 Multipart upload 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();
    }
}

【问题讨论】:

    标签: java amazon-web-services amazon-s3


    【解决方案1】:

    几年后,但在 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。)

    【讨论】:

    • 这很好用!希望它有更好的记录。
    【解决方案2】:

    使用 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') 方法。

    【讨论】:

      【解决方案3】:

      现在我正在探索返回由我的受信任代码返回的临时凭据,而不是签名的 url。

      http://docs.aws.amazon.com/AmazonS3/latest/dev/AuthUsingTempSessionTokenJava.html

      【讨论】:

        猜你喜欢
        • 2022-01-07
        • 2021-10-25
        • 1970-01-01
        • 1970-01-01
        • 2018-12-24
        • 2021-08-26
        • 1970-01-01
        • 2020-12-01
        • 2018-07-19
        相关资源
        最近更新 更多