diff --git a/README.md b/README.md index e1c5a8d..123c92d 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,11 @@ s3java is a java edition [s3cli](https://github.com/shvc/s3cli) Inspired by [awssdk](https://github.com/awsdocs/aws-doc-sdk-examples) examples -#### Download prebuild [binary](https://github.com/shvc/s3java/releases) +#### Download prebuilt [binary](https://github.com/shvc/s3java/releases) #### Or build fat jar from source ``` +# need gradle7 or above git clone https://github.com/shvc/s3java cd s3java gradle jar @@ -14,61 +15,62 @@ gradle jar ## Usage #### Create alias ``` -s3java='java -jar /path/to/s3java-1.1.jar' +alias s3java="java -jar `pwd`/build/libs/s3java-1.2.jar" s3java -h ``` + #### Bucket ```shell # create Bucket -java -jar s3java-1.1.jar -e http://192.168.56.3:9000 -a root -s ChangeMe create-bucket bucket-name +s3java -e http://127.0.0.1:9000 -a root -s ChangeMe create-bucket bucket-name # list(ls) all my Buckets -java -jar s3java-1.1.jar -e http://192.168.56.3:9000 -a root -s ChangeMe ls +s3java -e http://127.0.0.1:9000 -a root -s ChangeMe ls # head Bucket -java -jar s3java-1.1.jar -e http://192.168.56.3:9000 -a root -s ChangeMe head bucket-name +s3java -e http://127.0.0.1:9000 -a root -s ChangeMe head bucket-name # delete Bucket -java -jar s3java-1.1.jar -e http://192.168.56.3:9000 -a root -s ChangeMe delete bucket-name +s3java -e http://127.0.0.1:9000 -a root -s ChangeMe delete bucket-name ``` #### Object -- upload(put) Objcet(s) +- upload(put) Object(s) ```shell # upload file(s) -java -jar s3java-1.1.jar upload bucket-name/k0 --data KKKK # upload a Object(k0) with content KKKK -java -jar s3java-1.1.jar upload bucket-name/k1 /etc/hosts # upload a file and specify Key(k1) -java -jar s3java-1.1.jar --v2sign upload bucket-name/k2 /etc/hosts # upload(V2 sign) a file and specify Key(k2) -java -jar s3java-1.1.jar upload bucket-name /etc/hosts # upload a file and use filename(hosts) as Key -java -jar s3java-1.1.jar upload bucket-name *.txt # upload files and use filename as Key -java -jar s3java-1.1.jar upload bucket-name/dir/ *.txt # upload files and set Prefix(dir/) to all uploaded Object -java -jar s3java-1.1.jar --presign put bucket-name/k3 file # presign(V4) a PUT Object URL -java -jar s3java-1.1.jar --presign --v2sign put bucket-name/k4 file # presign(V2) a PUT Object URL +s3java upload bucket-name/k0 --data KKKK # upload a Object(k0) with content KKKK +s3java upload bucket-name/k1 /etc/hosts # upload a file and specify Key(k1) +s3java --v2sign upload bucket-name/k2 /etc/hosts # upload(V2 sign) a file and specify Key(k2) +s3java upload bucket-name /etc/hosts # upload a file and use filename(hosts) as Key +s3java upload bucket-name *.txt # upload files and use filename as Key +s3java upload bucket-name/dir/ *.txt # upload files and set Prefix(dir/) to all uploaded Object +s3java --presign put bucket-name/k3 file # presign(V4) a PUT Object URL +s3java --presign --v2sign put bucket-name/k4 file # presign(V2) a PUT Object URL ``` -- download(get) Object(s) +- download(get) Object(s) ```shell # download Object(s) -java -jar s3java-1.1.jar download bucket-name/k0 # download Object(k0) to current dir -java -jar s3java-1.1.jar --v2sign download bucket-name/k2 # download(V2 sign) Object(k2) to current dir -java -jar s3java-1.1.jar download bucket-name/k1 k2 k3 # download Objects(k1, k2 and k3) to current dir -java -jar s3java-1.1.jar --presign download bucket-name/k1 # presign(V4) a GET Object URL -java -jar s3java-1.1.jar --presign --v2sign download bucket-name/k2 # presign(V2) a GET Object URL +s3java download bucket-name/k0 # download Object(k0) to current dir +s3java --v2sign download bucket-name/k2 # download(V2 sign) Object(k2) to current dir +s3java download bucket-name/k1 k2 k3 # download Objects(k1, k2 and k3) to current dir +s3java --presign download bucket-name/k1 # presign(V4) a GET Object URL +s3java --presign --v2sign download bucket-name/k2 # presign(V2) a GET Object URL ``` -- list(ls) Objects +- list(ls) Objects ```shell # list Objects -java -jar s3java-1.1.jar list bucket-name # list -java -jar s3java-1.1.jar list bucket-name/prefix # list Objects with specified prefix -java -jar s3java-1.1.jar list-v2 bucket-name # listObjects v2 -java -jar s3java-1.1.jar list-v2 bucket-name/prefix # listObjects v2 with specified prefix +s3java list bucket-name # list +s3java list bucket-name/prefix # list Objects with specified prefix +s3java list-v2 bucket-name # listObjects v2 +s3java list-v2 bucket-name/prefix # listObjects v2 with specified prefix ``` -- delete(rm) Object(s) +- delete(rm) Object(s) ```shell # delete Object(s) -java -jar s3java-1.1.jar delete bucket-name/k0 # delete an Object -java -jar s3java-1.1.jar delete bucket-name/k1 k2 k3 # delete Objects -java -jar s3java-1.1.jar --presign delete bucket-name/hosts # presign(V4) an DELETE Object URL -java -jar s3java-1.1.jar --presign --v2sign delete bucket-name/k4 # presign(V2) an DELETE Object URL +s3java delete bucket-name/k0 # delete an Object +s3java delete bucket-name/k1 k2 k3 # delete Objects +s3java --presign delete bucket-name/hosts # presign(V4) an DELETE Object URL +s3java --presign --v2sign delete bucket-name/k4 # presign(V2) an DELETE Object URL ``` diff --git a/src/main/java/org/shvc/s3java/Main.java b/src/main/java/org/shvc/s3java/Main.java index f6bbaf7..1f6fdbc 100644 --- a/src/main/java/org/shvc/s3java/Main.java +++ b/src/main/java/org/shvc/s3java/Main.java @@ -37,7 +37,7 @@ public class Main implements Runnable { "--endpoint" }, showDefaultValue = CommandLine.Help.Visibility.ALWAYS, description = "S3 endpoint") private String endpoint = DEFAULT_ENDPOINT; - @Option(names = { "-r", + @Option(names = { "-R", "--region" }, showDefaultValue = CommandLine.Help.Visibility.ALWAYS, description = "S3 endpoint") private String region = Region.CN_Beijing.toString(); @@ -281,13 +281,10 @@ void delete( String key = valueInStr(bucketKey, '/'); if (keys == null && key.equals("")) { cli.deleteBucket(bucket); - } else { + } else if (keys == null) { cli.deleteObject(bucket, key); - if (keys != null) { - for (String k : keys) { - cli.deleteObject(bucket, k); - } - } + } else { + cli.deleteObjects(bucket, keys, key); } } diff --git a/src/main/java/org/shvc/s3java/S3Cli.java b/src/main/java/org/shvc/s3java/S3Cli.java index 620862e..01f0a6a 100644 --- a/src/main/java/org/shvc/s3java/S3Cli.java +++ b/src/main/java/org/shvc/s3java/S3Cli.java @@ -14,16 +14,20 @@ import com.amazonaws.services.s3.transfer.TransferManager; import com.amazonaws.services.s3.transfer.TransferManagerBuilder; import com.amazonaws.services.s3.transfer.Upload; +import com.amazonaws.services.s3.model.DeleteObjectsRequest; +import com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.InputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.net.URL; import java.time.Instant; + public class S3Cli { private AmazonS3 s3; private boolean presign; @@ -98,7 +102,7 @@ public void listObjectsV2(String bucket, String prefix, boolean all) { public void listObjects(String bucketName, String prefix, boolean all) { ListObjectsRequest lsReq = new ListObjectsRequest(); lsReq.setBucketName(bucketName); - if(!prefix.equals("")) { + if (!prefix.equals("")) { lsReq.setPrefix(prefix); } ObjectListing result = s3.listObjects(lsReq); @@ -342,6 +346,34 @@ public void deleteObject(String bucketName, String key) { } } + public void deleteObjects(String bucketName, String[] keys, String key) { + try { + if (this.presign) { + // Set the presigned URL + System.out.println("not ready"); + return; + } + + ArrayList objects = new ArrayList(); + for (int i = 0; i < keys.length; i++) { + objects.add(new KeyVersion(keys[i])); + } + if (key != null) { + objects.add(new KeyVersion(key)); + } + + DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName) + .withKeys(objects) + .withQuiet(true); + + s3.deleteObjects(multiObjectDeleteRequest); + + System.out.println(java.time.Clock.systemUTC().instant() + " delete Objects"); + } catch (AmazonServiceException e) { + System.exit(1); + } + } + public void deleteBucket(String bucket) { try { s3.deleteBucket(bucket);