-
Notifications
You must be signed in to change notification settings - Fork 48
/
CloudStorage.java
236 lines (191 loc) · 6.2 KB
/
CloudStorage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package com.pliablematter.cloudstorage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.StorageScopes;
import com.google.api.services.storage.model.Bucket;
import com.google.api.services.storage.model.StorageObject;
/**
* Simple wrapper around the Google Cloud Storage API
*/
public class CloudStorage {
private static Properties properties;
private static Storage storage;
private static final String PROJECT_ID_PROPERTY = "project.id";
private static final String APPLICATION_NAME_PROPERTY = "application.name";
private static final String ACCOUNT_ID_PROPERTY = "account.id";
private static final String PRIVATE_KEY_PATH_PROPERTY = "private.key.path";
/**
* Uploads a file to a bucket. Filename and content type will be based on
* the original file.
*
* @param bucketName
* Bucket where file will be uploaded
* @param filePath
* Absolute path of the file to upload
* @throws Exception
*/
public static void uploadFile(String bucketName, String filePath)
throws Exception {
Storage storage = getStorage();
StorageObject object = new StorageObject();
object.setBucket(bucketName);
File file = new File(filePath);
InputStream stream = new FileInputStream(file);
try {
String contentType = URLConnection
.guessContentTypeFromStream(stream);
InputStreamContent content = new InputStreamContent(contentType,
stream);
Storage.Objects.Insert insert = storage.objects().insert(
bucketName, null, content);
insert.setName(file.getName());
insert.execute();
} finally {
stream.close();
}
}
public static void downloadFile(String bucketName, String fileName, String destinationDirectory) throws Exception {
File directory = new File(destinationDirectory);
if(!directory.isDirectory()) {
throw new Exception("Provided destinationDirectory path is not a directory");
}
File file = new File(directory.getAbsolutePath() + "/" + fileName);
Storage storage = getStorage();
Storage.Objects.Get get = storage.objects().get(bucketName, fileName);
FileOutputStream stream = new FileOutputStream(file);
try {
get.executeAndDownloadTo(stream);
} finally {
stream.close();
}
}
/**
* Deletes a file within a bucket
*
* @param bucketName
* Name of bucket that contains the file
* @param fileName
* The file to delete
* @throws Exception
*/
public static void deleteFile(String bucketName, String fileName)
throws Exception {
Storage storage = getStorage();
storage.objects().delete(bucketName, fileName).execute();
}
/**
* Creates a bucket
*
* @param bucketName
* Name of bucket to create
* @throws Exception
*/
public static void createBucket(String bucketName) throws Exception {
Storage storage = getStorage();
Bucket bucket = new Bucket();
bucket.setName(bucketName);
storage.buckets().insert(
getProperties().getProperty(PROJECT_ID_PROPERTY), bucket).execute();
}
/**
* Deletes a bucket
*
* @param bucketName
* Name of bucket to delete
* @throws Exception
*/
public static void deleteBucket(String bucketName) throws Exception {
Storage storage = getStorage();
storage.buckets().delete(bucketName).execute();
}
/**
* Lists the objects in a bucket
*
* @param bucketName bucket name to list
* @return Array of object names
* @throws Exception
*/
public static List<String> listBucket(String bucketName) throws Exception {
Storage storage = getStorage();
List<String> list = new ArrayList<String>();
List<StorageObject> objects = storage.objects().list(bucketName).execute().getItems();
if(objects != null) {
for(StorageObject o : objects) {
list.add(o.getName());
}
}
return list;
}
/**
* List the buckets with the project
* (Project is configured in properties)
*
* @return
* @throws Exception
*/
public static List<String> listBuckets() throws Exception {
Storage storage = getStorage();
List<String> list = new ArrayList<String>();
List<Bucket> buckets = storage.buckets().list(getProperties().getProperty(PROJECT_ID_PROPERTY)).execute().getItems();
if(buckets != null) {
for(Bucket b : buckets) {
list.add(b.getName());
}
}
return list;
}
private static Properties getProperties() throws Exception {
if (properties == null) {
properties = new Properties();
InputStream stream = CloudStorage.class
.getResourceAsStream("/cloudstorage.properties");
try {
properties.load(stream);
} catch (IOException e) {
throw new RuntimeException(
"cloudstorage.properties must be present in classpath",
e);
} finally {
stream.close();
}
}
return properties;
}
private static Storage getStorage() throws Exception {
if (storage == null) {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
List<String> scopes = new ArrayList<String>();
scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);
Credential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(
getProperties().getProperty(ACCOUNT_ID_PROPERTY))
.setServiceAccountPrivateKeyFromP12File(
new File(getProperties().getProperty(
PRIVATE_KEY_PATH_PROPERTY)))
.setServiceAccountScopes(scopes).build();
storage = new Storage.Builder(httpTransport, jsonFactory,
credential).setApplicationName(
getProperties().getProperty(APPLICATION_NAME_PROPERTY))
.build();
}
return storage;
}
}