gpt4 book ai didi

android - 在 S3 服务器上并行上传图片

转载 作者:太空狗 更新时间:2023-10-29 13:56:18 24 4
gpt4 key购买 nike

我正在使用 TransferUtility 在 Amazon S3 服务器上上传图像。我正在尝试一张一张地上传 3 张图片,但它是并行上传的。我想一张一张上传图片。我应该怎么做?我正在使用以下代码进行上传:

private void uploadToAWS(TransferUtility transferUtility,final ArrayList<File> imageFileArrayList){

final ArrayList<String> uploadedFileArrayList = new ArrayList<>();
final ArrayList<String> failedFileArrayList = new ArrayList<>();

for(int i = 0; i < imageFileArrayList.size(); i++) {

final File file = imageFileArrayList.get(i);
final TransferObserver observer = transferUtility.upload(
Constants.S3_BUCKET_NAME, /* The bucket to upload to */
"Test_Images" + file.getAbsolutePath(), /* The key for the uploaded object */
file /* The file where the data to upload exists */
);
observer.setTransferListener(new TransferListener() {

@Override
public void onStateChanged(int id, TransferState state) {
System.out.println("Id:- " + id);
if (state == TransferState.COMPLETED) {
uploadedFileArrayList.add(observer.getAbsoluteFilePath());
} else if (state == TransferState.FAILED) {
failedFileArrayList.add(file.getAbsolutePath());
}
}

@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
int percent = (int) ((bytesCurrent * 100) / bytesTotal);
System.out.println("percent:- " + percent);
}

@Override
public void onError(int id, Exception ex) {
ex.printStackTrace();
System.out.println("Id:- " + id + " in exception");
}
});

}

}

我在 Asynctask 中调用这个函数。

@Override                           
protected Void doInBackground(Void... params) {

AmazonS3 amazonS3 = new AmazonS3Client(getAWSCredentials());

TransferUtility transferUtility = new TransferUtility(amazonS3, context);

uploadToAWS(transferUtility, imageFileArrayList);

return null;

}

请帮忙。

最佳答案

好的,问题来了:您正在从 uploadToAWS() 的循环中调用 transferUtility.upload()。现在您调用第一个文件上传但无需等待完成(因为您已将其放入循环中)您遍历循环并调用下一个文件上传,依此类推...

现在要避免这种情况,您需要做的就是等待 TransferState.COMPLETED 事件回调,一旦完成,您应该调用下一个文件上传(通过另一个调用 transferUtility.upload()).

您可能还想添加一些重试代码,以防在 TransferState.FAILED 回调中上传失败。

所以你的代码大致变成这样:

private void uploadToAWS(TransferUtility transferUtility,final ArrayList<File> imageFileArrayList) {

final File file = imageFileArrayList.get(i);
final TransferObserver observer = transferUtility.upload(
Constants.S3_BUCKET_NAME, /* The bucket to upload to */
"Test_Images" + file.getAbsolutePath(), /* The key for the uploaded object */
file /* The file where the data to upload exists */
);
observer.setTransferListener(new TransferListener() {

@Override
public void onStateChanged(int id, TransferState state) {
System.out.println("Id:- " + id);
if (state == TransferState.COMPLETED) {
uploadedFileArrayList.add(observer.getAbsoluteFilePath());

failedFileArrayList.remove(file.getAbsolutePath());
i++;
uploadToAWS(transferUtility, imageFileArrayList);
} else if (state == TransferState.FAILED) {
failedFileArrayList.add(file.getAbsolutePath());
uploadToAWS(transferUtility, imageFileArrayList);
}
}

@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
int percent = (int) ((bytesCurrent * 100) / bytesTotal);
System.out.println("percent:- " + percent);
}

@Override
public void onError(int id, Exception ex) {
failedFileArrayList.add(file.getAbsolutePath());
ex.printStackTrace();
System.out.println("Id:- " + id + " in exception");
}
});

}

}

这是您的代码可能的非常粗略的实现。您可能想要更改整个流程,这当然更好。

关于android - 在 S3 服务器上并行上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39387704/

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