batch download

This commit is contained in:
Page Asgardius 2022-12-29 07:44:41 -07:00
parent a8b6a2d019
commit c1342adcee
7 changed files with 71 additions and 9 deletions

View file

@ -163,6 +163,10 @@ public class BucketSelect extends AppCompatActivity {
//Toast.makeText(BucketSelect.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show(); //Toast.makeText(BucketSelect.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show();
upload(Name.get(position).toString()); upload(Name.get(position).toString());
} else if (menuItem.getTitle() == getResources().getString(R.string.download_bucket)) {
//Toast.makeText(BucketSelect.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show();
download(Name.get(position).toString());
} else if (menuItem.getTitle() == getResources().getString(R.string.create_bucket)) { } else if (menuItem.getTitle() == getResources().getString(R.string.create_bucket)) {
//upload(); //upload();
newBucket(); newBucket();
@ -325,6 +329,20 @@ public class BucketSelect extends AppCompatActivity {
startActivity(intent); startActivity(intent);
} }
private void download(String bucket) {
Intent intent = new Intent(this, Downloader.class);
intent.putExtra("endpoint", endpoint);
intent.putExtra("username", username);
intent.putExtra("password", password);
intent.putExtra("prefix", prefix);
intent.putExtra("region", location);
intent.putExtra("style", style);
intent.putExtra("bucket", bucket);
intent.putExtra("isfolder", true);
startActivity(intent);
}
private void corsConfig(String bucket) { private void corsConfig(String bucket) {
Intent intent = new Intent(this, CorsConfig.class); Intent intent = new Intent(this, CorsConfig.class);
intent.putExtra("endpoint", endpoint); intent.putExtra("endpoint", endpoint);

View file

@ -26,11 +26,16 @@ import com.amazonaws.regions.Region;
import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions; import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3Object; import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class Downloader extends AppCompatActivity { public class Downloader extends AppCompatActivity {
String username, password, endpoint, bucket, filename, prefix, location; String username, password, endpoint, bucket, filename, prefix, location;
@ -40,6 +45,7 @@ public class Downloader extends AppCompatActivity {
AWSCredentials myCredentials; AWSCredentials myCredentials;
AmazonS3 s3client; AmazonS3 s3client;
ProgressBar simpleProgressBar; ProgressBar simpleProgressBar;
ListObjectsRequest orequest;
Intent intent; Intent intent;
Button fileDownload; Button fileDownload;
Thread downloadFile, downloadProgress; Thread downloadFile, downloadProgress;
@ -47,9 +53,9 @@ public class Downloader extends AppCompatActivity {
DocumentFile document; DocumentFile document;
boolean started = false; boolean started = false;
boolean cancel = false; boolean cancel = false;
boolean isfolder = false; boolean style, isfolder;
boolean style;
long filesize = 0; long filesize = 0;
long objectsize;
long transfered = 0; long transfered = 0;
private WifiManager.WifiLock mWifiLock; private WifiManager.WifiLock mWifiLock;
private PowerManager.WakeLock mWakeLock; private PowerManager.WakeLock mWakeLock;
@ -70,6 +76,7 @@ public class Downloader extends AppCompatActivity {
bucket = getIntent().getStringExtra("bucket"); bucket = getIntent().getStringExtra("bucket");
location = getIntent().getStringExtra("region"); location = getIntent().getStringExtra("region");
style = getIntent().getBooleanExtra("style", false); style = getIntent().getBooleanExtra("style", false);
isfolder = getIntent().getBooleanExtra("isfolder", false);
prefix = getIntent().getStringExtra("prefix"); prefix = getIntent().getStringExtra("prefix");
simpleProgressBar = (ProgressBar) findViewById(R.id.simpleProgressBar); simpleProgressBar = (ProgressBar) findViewById(R.id.simpleProgressBar);
fileDownload = (Button)findViewById(R.id.filedownload); fileDownload = (Button)findViewById(R.id.filedownload);
@ -121,12 +128,44 @@ public class Downloader extends AppCompatActivity {
//s3client.createBucket(bucket, location); //s3client.createBucket(bucket, location);
//System.out.println(fkey); //System.out.println(fkey);
document = DocumentFile.fromTreeUri(getApplicationContext(), fileuri); document = DocumentFile.fromTreeUri(getApplicationContext(), fileuri);
object = s3client.getObject(bucket, prefix+filename);
filesize = (object.getObjectMetadata().getContentLength())/1024;
if (isfolder) { if (isfolder) {
filepath = document.createFile(null, filename).getUri(); if (object == null) {
writeContentToFile(filepath, object); orequest = new ListObjectsRequest().withBucketName(bucket).withMaxKeys(1000);
}
ArrayList<String> objectlist = new ArrayList<String>();
ObjectListing result = s3client.listObjects(orequest);
List<S3ObjectSummary> objects = result.getObjectSummaries();
for (S3ObjectSummary os : objects) {
objectlist.add(os.getKey());
objectsize = os.getSize();
if (objectsize%1024 == 0) {
filesize = filesize+(objectsize/1024);
} else {
filesize = filesize+(objectsize/1024)+1;
}
}
while (result.isTruncated()) {
result = s3client.listNextBatchOfObjects (result);
objects = result.getObjectSummaries();
for (S3ObjectSummary os : objects) {
objectlist.add(os.getKey());
objectsize = os.getSize();
if (objectsize%1024 == 0) {
filesize = filesize+(objectsize/1024);
} else {
filesize = filesize+(objectsize/1024)+1;
}
}
}
for (String os : objectlist) {
object = s3client.getObject(bucket, os);
filepath = document.createFile(null, os).getUri();
writeContentToFile(filepath, object);
}
} else { } else {
object = s3client.getObject(bucket, prefix+filename);
filesize = (object.getObjectMetadata().getContentLength())/1024;
writeContentToFile(fileuri, object); writeContentToFile(fileuri, object);
} }
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {

View file

@ -80,7 +80,7 @@ public class ObjectInfo extends AppCompatActivity {
//Your code goes here //Your code goes here
if (object == null) { if (object == null) {
isobject = false; isobject = false;
orequest = new ListObjectsRequest().withBucketName(bucket).withMaxKeys(8000); orequest = new ListObjectsRequest().withBucketName(bucket).withMaxKeys(1000);
} else { } else {
isobject = true; isobject = true;
if (object.endsWith("/")) { if (object.endsWith("/")) {
@ -88,7 +88,7 @@ public class ObjectInfo extends AppCompatActivity {
} else { } else {
isfolder = false; isfolder = false;
} }
orequest = new ListObjectsRequest().withBucketName(bucket).withPrefix(object).withMaxKeys(8000); orequest = new ListObjectsRequest().withBucketName(bucket).withPrefix(object).withMaxKeys(1000);
} }
ObjectListing result = s3client.listObjects(orequest); ObjectListing result = s3client.listObjects(orequest);
List<S3ObjectSummary> objects = result.getObjectSummaries(); List<S3ObjectSummary> objects = result.getObjectSummaries();

View file

@ -655,7 +655,6 @@ public class ObjectSelect extends AppCompatActivity {
intent.putExtra("endpoint", endpoint); intent.putExtra("endpoint", endpoint);
intent.putExtra("username", username); intent.putExtra("username", username);
intent.putExtra("password", password); intent.putExtra("password", password);
intent.putExtra("prefix", prefix);
intent.putExtra("region", location); intent.putExtra("region", location);
intent.putExtra("style", style); intent.putExtra("style", style);
intent.putExtra("bucket", bucket); intent.putExtra("bucket", bucket);

View file

@ -5,6 +5,10 @@
android:id="@+id/upload_file" android:id="@+id/upload_file"
android:title="@string/upload_file_tobucket" /> android:title="@string/upload_file_tobucket" />
<item
android:id="@+id/download_bucket"
android:title="@string/download_bucket" />
<item <item
android:id="@+id/newbucket" android:id="@+id/newbucket"
android:title="@string/create_bucket" /> android:title="@string/create_bucket" />

View file

@ -106,4 +106,5 @@
<string name="delete_cors">Eliminar CORS</string> <string name="delete_cors">Eliminar CORS</string>
<string name="cors_error">No se pudo cambiar la política CORS</string> <string name="cors_error">No se pudo cambiar la política CORS</string>
<string name="cors_ok">Política CORS cambiada exitosamente</string> <string name="cors_ok">Política CORS cambiada exitosamente</string>
<string name="download_bucket">Descargar bucket</string>
</resources> </resources>

View file

@ -26,6 +26,7 @@
<string name="file_share">Share file</string> <string name="file_share">Share file</string>
<string name="file_external">Open in</string> <string name="file_external">Open in</string>
<string name="download_file">Download file</string> <string name="download_file">Download file</string>
<string name="download_bucket">Download bucket</string>
<string name="cancel_download">Cancel download</string> <string name="cancel_download">Cancel download</string>
<string name="download_success">File downloaded successfully</string> <string name="download_success">File downloaded successfully</string>
<string name="upload_file_tobucket">Upload file to this bucket</string> <string name="upload_file_tobucket">Upload file to this bucket</string>