Skip to content

Commit 49b9a48

Browse files
committed
Added authorized upload/download
1 parent 26a5314 commit 49b9a48

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.gkatzioura.maven.cloud.gcs.wagon;
1+
package com.gkatzioura.maven.cloud.gcs;
22

33
import java.io.File;
44
import java.io.FileInputStream;
@@ -11,11 +11,11 @@
1111
import com.google.cloud.storage.Storage;
1212
import com.google.cloud.storage.StorageOptions;
1313

14-
class StorageFactory {
14+
public class StorageFactory {
1515

1616
private static final Logger LOGGER = Logger.getLogger(StorageFactory.class.getName());
1717

18-
Storage createWithKeyFile(String keyPath) throws IOException {
18+
public Storage createWithKeyFile(String keyPath) throws IOException {
1919
File credentialsPath = new File(keyPath);
2020
try(FileInputStream serviceAccountStream = new FileInputStream(credentialsPath)) {
2121
GoogleCredentials googleCredentials = ServiceAccountCredentials.fromStream(serviceAccountStream);
@@ -26,7 +26,7 @@ Storage createWithKeyFile(String keyPath) throws IOException {
2626
}
2727
}
2828

29-
Storage createDefault() {
29+
public Storage createDefault() {
3030
return StorageOptions.getDefaultInstance().getService();
3131
}
3232
}

GoogleStorageWagon/src/main/java/com/gkatzioura/maven/cloud/gcs/plugin/download/GCSDownloadMojo.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.gkatzioura.maven.cloud.gcs.plugin.download;
22

33
import java.io.File;
4+
import java.io.IOException;
45
import java.util.Iterator;
56
import java.util.List;
67
import java.util.logging.Level;
@@ -14,11 +15,11 @@
1415
import org.apache.maven.plugins.annotations.Parameter;
1516

1617
import com.gkatzioura.maven.cloud.KeyIteratorConcated;
18+
import com.gkatzioura.maven.cloud.gcs.StorageFactory;
1719
import com.gkatzioura.maven.cloud.gcs.plugin.PrefixKeysIterator;
1820
import com.google.cloud.storage.Blob;
1921
import com.google.cloud.storage.BlobId;
2022
import com.google.cloud.storage.Storage;
21-
import com.google.cloud.storage.StorageOptions;
2223

2324
@Mojo(name = "gcs-download")
2425
public class GCSDownloadMojo extends AbstractMojo {
@@ -32,7 +33,11 @@ public class GCSDownloadMojo extends AbstractMojo {
3233
@Parameter(property = "gcs-download.downloadPath")
3334
private String downloadPath;
3435

35-
private Storage storage = StorageOptions.getDefaultInstance().getService();
36+
@Parameter(property = "gcs-download.keyPath")
37+
private String keyPath;
38+
39+
private final StorageFactory storageFactory = new StorageFactory();
40+
private Storage storage;
3641

3742
private static final Logger LOGGER = Logger.getLogger(GCSDownloadMojo.class.getName());
3843

@@ -47,6 +52,8 @@ public GCSDownloadMojo(String bucket, List<String> keys, String downloadPath) th
4752

4853
@Override
4954
public void execute() throws MojoExecutionException, MojoFailureException {
55+
storage = initializeStorage();
56+
5057
if (keys.size()==1) {
5158
downloadSingleFile(storage,keys.get(0));
5259
return;
@@ -65,6 +72,18 @@ public void execute() throws MojoExecutionException, MojoFailureException {
6572
}
6673
}
6774

75+
private Storage initializeStorage() throws MojoExecutionException {
76+
if(keyPath==null) {
77+
return storageFactory.createDefault();
78+
} else {
79+
try {
80+
return storageFactory.createWithKeyFile(keyPath);
81+
} catch (IOException e) {
82+
throw new MojoExecutionException("Failed to set Authentication to Google Cloud");
83+
}
84+
}
85+
}
86+
6887
private void downloadSingleFile(Storage storage,String key) {
6988
File file = new File(downloadPath);
7089

GoogleStorageWagon/src/main/java/com/gkatzioura/maven/cloud/gcs/plugin/upload/GCSUploadMojo.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.apache.maven.plugins.annotations.Mojo;
1515
import org.apache.maven.plugins.annotations.Parameter;
1616

17+
import com.gkatzioura.maven.cloud.gcs.StorageFactory;
1718
import com.google.cloud.storage.BlobInfo;
1819
import com.google.cloud.storage.Storage;
1920
import com.google.cloud.storage.StorageOptions;
@@ -30,6 +31,11 @@ public class GCSUploadMojo extends AbstractMojo {
3031
@Parameter(property = "gcs-upload.key")
3132
private String key;
3233

34+
@Parameter(property = "gcs-upload.keyPath")
35+
private String keyPath;
36+
37+
private final StorageFactory storageFactory = new StorageFactory();
38+
3339
public GCSUploadMojo() {
3440
}
3541

@@ -52,7 +58,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
5258
throw new MojoExecutionException("You need to specify a bucket");
5359
}
5460

55-
Storage storage = StorageOptions.getDefaultInstance().getService();
61+
Storage storage = initializeStorage();
5662

5763
if(isDirectory()){
5864
List<String> filesToUpload = findFilesToUpload(path);
@@ -65,6 +71,18 @@ public void execute() throws MojoExecutionException, MojoFailureException {
6571
}
6672
}
6773

74+
private Storage initializeStorage() throws MojoExecutionException {
75+
if(keyPath==null) {
76+
return storageFactory.createDefault();
77+
} else {
78+
try {
79+
return storageFactory.createWithKeyFile(keyPath);
80+
} catch (IOException e) {
81+
throw new MojoExecutionException("Failed to set Authentication to Google Cloud");
82+
}
83+
}
84+
}
85+
6886
private List<String> findFilesToUpload(String filePath) {
6987
List<String> totalFiles = new ArrayList<>();
7088

GoogleStorageWagon/src/main/java/com/gkatzioura/maven/cloud/gcs/wagon/GoogleStorageRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.maven.wagon.ResourceDoesNotExistException;
3131
import org.apache.maven.wagon.authentication.AuthenticationException;
3232

33+
import com.gkatzioura.maven.cloud.gcs.StorageFactory;
3334
import com.gkatzioura.maven.cloud.resolver.KeyResolver;
3435
import com.gkatzioura.maven.cloud.wagon.PublicReadProperty;
3536
import com.google.api.gax.paging.Page;

0 commit comments

Comments
 (0)