gpt4 book ai didi

java - 我可以使用 Amazon 高级或低级 API 暂停和恢复分段上传吗?

转载 作者:行者123 更新时间:2023-11-30 09:03:08 25 4
gpt4 key购买 nike

我正在尝试开发一个上传管理器(可安装在您的 PC 上,而不是一个网络应用程序),它将使用 Java 将选定的图像文件上传到 AmazonS3。现在,我已经完成编码,它将初始化上传并使用 AmazonS3 的 TransferManager(高级 API)暂停上传。问题是,每次我恢复上传时,它都从头开始。我看过几个 aws 博客和 aws 文档,但没有直接回答这个问题。我还用低级 API 编写了代码(使用他们的示例代码),但在那里,我不知道如何暂停上传。所以,我的问题是:

  1. 如何从暂停点恢复上传?
  2. 我有两个按钮(使用 JFrames 和 Swing Worker),名为暂停和恢复。我应该怎么做才能重复暂停和恢复?如何实现代码?

    public class UploadObjectMultipartUploadUsingHighLevelAPI {

    public void pauseUploading(TransferManager tm, Upload upload) throws Exception{
    long MB = 1024 * 1024 ;
    TransferProgress progress = upload.getProgress();
    System.out.println("The pause will occur once 5 MB of data is uploaded");
    while( progress.getBytesTransferred() < 5*MB )
    Thread.sleep(2000);

    boolean forceCancel = true;
    float dataTransfered = (float) upload.getProgress().getBytesTransferred();
    System.out.println("Data Transfered until now: " + dataTransfered);
    PauseResult<PersistableUpload> pauseResult = ((Upload) upload).tryPause(forceCancel);
    System.out.println("The upload has been paused. The code that we've got is " + pauseResult.getPauseStatus());
    pauseResult = ((Upload) upload).tryPause(forceCancel);
    PersistableUpload persistableUpload = (PersistableUpload) pauseResult.getInfoToResume();
    System.out.println("Storing information into file");
    File f = new File("D:\\Example\\resume-upload");
    if( !f.exists() )
    f.createNewFile();

    FileOutputStream fos = new FileOutputStream(f);
    persistableUpload.serialize(fos);
    fos.close();
    }

    public void resumeUploading(TransferManager tm) throws Exception{
    FileInputStream fis = new FileInputStream(new File("D:\\Example\\resume-upload"));
    System.out.println("Reading information from the file");
    PersistableUpload persistableUpload;
    persistableUpload = PersistableTransfer.deserializeFrom(fis);
    System.out.println("Reading information completed");
    System.out.println("The system will resume upload now");
    tm.resumeUpload(persistableUpload);
    fis.close();
    // System.out.println("Upload complete.");
    }

    public static void main(String[] args) throws Exception {
    String existingBucketName = "Business.SkySquirrel.RawImages/Test";
    String keyName = "Pictures1.zip";
    String filePath = "D:\\Pictures1.zip";
    TransferManagerConfiguration configuration = new TransferManagerConfiguration();
    TransferManager tm = new TransferManager(new ProfileCredentialsProvider());
    configuration.setMultipartUploadThreshold(1024 * 1024);
    tm.setConfiguration(configuration);
    System.out.println("************* Upload Manager *************");
    try {
    Upload upload = tm.upload(existingBucketName, keyName, new File(filePath));
    System.out.println("Upload Started");
    System.out.println("Transfer: " + upload.getDescription());

    UploadObjectMultipartUploadUsingHighLevelAPI multipartPause =
    new UploadObjectMultipartUploadUsingHighLevelAPI();
    multipartPause.pauseUploading(tm, upload);

    UploadObjectMultipartUploadUsingHighLevelAPI multipartResume =
    new UploadObjectMultipartUploadUsingHighLevelAPI();
    multipartResume.resumeUploading(tm);
    }
    catch (AmazonClientException amazonClientException) {
    System.out.println("Unable to upload file, upload was aborted.");
    amazonClientException.printStackTrace();
    }
    }
    }

我会很感激使用 AmazonS3 的高级或低级 API 的示例代码。

我使用的是 1.8.9.1 版本的 SDK。我还在使用以下代码初始化上传、暂停和恢复时添加了进度。

long MB = 1024 * 1024;
TransferProgress progress = upload.getProgress();
float dataTransfered = progress.getBytesTransferred();

while(!upload.isDone()){
dataTransfered = progress.getBytesTransferred();
System.out.println("Data Transfered: " + dataTransfered/MB + " MB");
Thread.sleep(2000);
}

我得到了以下结果:

密码匹配选择了以下文件:

D:\Pictures3\DSC02247 - Copy.JPG
Writing 'D:\Pictures3\DSC02247 - Copy.JPG' to zip file
D:\Pictures3\DSC02247.JPG
Writing 'D:\Pictures3\DSC02247.JPG' to zip file
D:\Pictures3\DSC02248.JPG
Writing 'D:\Pictures3\DSC02248.JPG' to zip file
************* Upload Manager *************
Upload Started
Transfer: Uploading to *******/****/****.zip
Data Transfered: 0.0 MB
Data Transfered: 0.0703125 MB
Data Transfered: 0.21875 MB
Data Transfered: 0.3203125 MB
Data Transfered: 0.4140625 MB
Data Transfered: 0.515625 MB
....
....
Data Transfered: 0.9609375 MB
Data Transfered: 1.0546875 MB
Pause Commencing
The pause will occur once 5 MB of data is uploaded
Data Transfered: 1.09375 MB
Data Transfered: 1.1640625 MB
Data Transfered: 1.265625 MB
Data Transfered: 1.359375 MB
....
....
Data Transfered: 4.734375 MB
Data Transfered: 4.8359375 MB
Data Transfered: 4.9296875 MB
The upload has been paused. The code that we've got is SUCCESS
Storing information into file
Upload Paused
Resume Commencing
Reading information from the file
Reading information completed
The system will resume upload now
Data Transfered: 0.0 MB
Data Transfered: 0.171875 MB
Data Transfered: 0.265625 MB
Data Transfered: 0.359375 MB
Data Transfered: 0.421875 MB
....
....
Data Transfered: 9.58182 MB
Data Transfered: 9.683382 MB
Data Transfered: 9.753695 MB
Upload Complete

最佳答案

使用 REST API,这非常简单……您可以通过不再发送任何部分来“暂停”,然后通过发送下一部分来“恢复”。

“低级 API”与 REST 接口(interface)紧密对应,因此功能应该相同。 S3 在内部没有“暂停”分段上传的概念。它只是无限期地等待您上传更多部分并完成请求,或者中止请求。它将存储您发送的部分(存储费用),直到整个操作完成或中止...它实际上会等待个月(我已经看到了...并且据推测,它将永远等待您“恢复”。

但是没有低级别的暂停/恢复调用——您只需执行即可。

要注意的是,您必须在本地保留每个部分的 etag,并且必须将它们与完成分段上传的请求一起发送。

如果您从未完成或中止一个多部分操作,您发送的部分将由 S3 存储,等待您的下一步行动。

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

关于java - 我可以使用 Amazon 高级或低级 API 暂停和恢复分段上传吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25756687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com