gpt4 book ai didi

android - 如何自动下载整个 S3 bucket

转载 作者:行者123 更新时间:2023-11-30 01:19:01 24 4
gpt4 key购买 nike

我能够从 aws s3 存储桶中成功下载所有文件,因此当我单击特定列表项时,列表项将被下载。但是,我希望在 Activity 启动时自动下载所有 s3 存储桶项目。

/**

* DownloadSelectionActivity 显示存储桶中的文件列表。用户可以 * 选择要下载的文件。 */公共(public)类 DownloadSelectionActivity 扩展 ListActivity {

// The S3 client used for getting the list of objects in the bucket
private AmazonS3Client s3;
// An adapter to show the objects
private SimpleAdapter simpleAdapter;
private ArrayList<HashMap<String, Object>> transferRecordMaps;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download_selection);
initData();
initUI();
}

@Override
protected void onResume() {
super.onResume();
// Refresh the file list.
new GetFileListTask().execute();
}

private void initData() {
// Gets the default S3 client.
s3 = Util.getS3Client(DownloadSelectionActivity.this);
transferRecordMaps = new ArrayList<HashMap<String, Object>>();
}

private void initUI() {
simpleAdapter = new SimpleAdapter(this, transferRecordMaps,
R.layout.bucket_item, new String[] {
"key"
},
new int[] {
R.id.key
});
simpleAdapter.setViewBinder(new ViewBinder() {
@Override
public boolean setViewValue(View view, Object data,
String textRepresentation) {
switch (view.getId()) {
case R.id.key:
TextView fileName = (TextView) view;
fileName.setText((String) data);
return true;
}
return false;
}
});

// When an item is selected, finish the activity and pass back the S3
// key associated with the object selected
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> adapterView, View view, int pos, long id) {
Intent intent = new Intent();
intent.putExtra("key", (String) transferRecordMaps.get(pos).get("key"));
setResult(RESULT_OK, intent);
finish();


}
});

}






/**
* This async task queries S3 for all files in the given bucket so that they
* can be displayed on the screen
*/
private class GetFileListTask extends AsyncTask<Void, Void, Void> {
// The list of objects we find in the S3 bucket
private List<S3ObjectSummary> s3ObjList;
// A dialog to let the user know we are retrieving the files
private ProgressDialog dialog;

@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(DownloadSelectionActivity.this,
getString(R.string.refreshing),
getString(R.string.please_wait));
}

@Override
protected Void doInBackground(Void... inputs) {
// Queries files in the bucket from S3.
s3ObjList = s3.listObjects(Constants.BUCKET_NAME).getObjectSummaries();
transferRecordMaps.clear();
for (S3ObjectSummary summary : s3ObjList) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("key", summary.getKey());
transferRecordMaps.add(map);
}
return null;
}

@Override
protected void onPostExecute(Void result) {
dialog.dismiss();
simpleAdapter.notifyDataSetChanged();
}
}

最佳答案

您已经有了存储桶中的对象列表,您可以遍历列表并在列表操作完成后下载每个对象。使用传输实用程序可能更容易处理传输(允许暂停/恢复、检查状态等...),传输实用程序是标准 S3 客户端之上的高级实用程序。

可以在此处找到 Transfer Utility 的指南 (http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/s3transferutility.html)

关于android - 如何自动下载整个 S3 bucket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37392194/

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